Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1.  
  2. # set works, build, testing,
  3.  
  4. # jobs:
  5. # { 2: [3, 4, 5], 3: [4], 4: [5]}
  6.  
  7.  
  8. class Job:
  9. def __init__(self, name, depends=[]):
  10. self.name = name
  11. self.depends = depends
  12. def do(self):
  13. print("{} do something".format(self.name))
  14.  
  15. def depend_order(lst):
  16. res = []
  17.  
  18. dict_lst = {}
  19. for item in lst:
  20. dict_lst[item.name] = item.depends
  21.  
  22. print(dict_lst)
  23.  
  24.  
  25. flag = False
  26. for item in dict_lst.keys():
  27. print('new dict {} list: {}'.format(item, dict_lst[item]))
  28. if len(dict_lst[item]) == 0:
  29. flag = True
  30. res.append(item)
  31. print(res)
  32. if flag:
  33. for other_item in dict_lst.keys():
  34. if item in dict_lst[other_item]:
  35. # print(dict_lst[other_item])
  36. dict_lst[other_item].remove(item)
  37. print('removed {} from {}'.format(item, other_item))
  38. flag = False
  39.  
  40. return res
  41.  
  42. def execute(lst):
  43. ordered_lst = depend_order(lst)
  44. print('ordered list is {}'.format(ordered_lst))
  45. # for item in ordered_lst:
  46. # item.do()
  47.  
  48.  
  49. if __name__ == '__main__':
  50. B = Job('B')
  51. C = Job('C', ['B'])
  52. D = Job('D', ['C'])
  53. A = Job('A', ['B', 'C', 'D'])
  54.  
  55. # B->C->D->A
  56.  
  57. #input = { 2: [3, 4, 5], 3: [4], 4: [5]}
  58. execute([A, B, C, D])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement