Gorcupt

2-2

Mar 28th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import json
  2. import collections
  3. import threading
  4. import multiprocessing.dummy as multiprocessing
  5.  
  6.  
  7. def mostSell_Car():
  8. sellArray = loadJsonSell()
  9. dictCarAndCount = collections.Counter()
  10. for d in sellArray:
  11. dictCarAndCount[d['car']] += 1
  12. c = collections.Counter(dictCarAndCount).most_common()
  13. print('\nСамый продаваемый автомобиль')
  14. for elem in c[:1]:
  15. print(elem[0])
  16.  
  17.  
  18. def mostEffective_Employee():
  19. sellArray = loadJsonSell()
  20. dictEmpAndCount = collections.Counter()
  21. for d in sellArray:
  22. dictEmpAndCount[d['employee']] += 1
  23. c = collections.Counter(dictEmpAndCount).most_common()
  24. print('\nСамый эффективный сотрудник')
  25. for elem in c[:1]:
  26. print(elem[0])
  27.  
  28.  
  29. def top5Spare_high_price():
  30. spareArray = loadJsonSpare()
  31. dictSpareAndPrice = collections.Counter()
  32. for d in spareArray:
  33. dictSpareAndPrice[d['name']] += d['price']
  34. print("\nТоп 5 запчастей по стоимости: ")
  35. for spare in dictSpareAndPrice.most_common()[:5]:
  36. print(spare)
  37.  
  38.  
  39. def linearSolution():
  40. mostSell_Car()
  41. top5Spare_high_price()
  42. mostEffective_Employee()
  43.  
  44.  
  45. def timerSolution():
  46. timer = threading.Timer(0, mostSell_Car)
  47. timer1 = threading.Timer(1, top5Spare_high_price)
  48. timer2 = threading.Timer(2, mostEffective_Employee)
  49. timer.start()
  50. timer1.start()
  51. timer2.start()
  52.  
  53.  
  54. def processSolution():
  55. pool = multiprocessing.Pool()
  56. pool.map(lambda f: f(), [mostSell_Car])
  57. pool.map(lambda f: f(), [top5Spare_high_price])
  58. pool.map(lambda f: f(), [mostEffective_Employee])
  59. pool.close()
  60. pool.join()
  61.  
  62.  
  63. def addSellRecord():
  64. sellArray = loadJsonSell()
  65. employee = input('Введите сотрудника\n')
  66. car = input('Введите название машины\n')
  67. newSell = {'employee': employee, 'car': car}
  68. sellArray.append(newSell)
  69. newJson = {"sell": sellArray, "spare": loadJsonSpare()}
  70. with open("json.json", "w") as write_file:
  71. json.dump(newJson, write_file)
  72.  
  73.  
  74. def addSpareRecord():
  75. spareArray = loadJsonSpare()
  76. name = input('Введите название детали\n')
  77. price = input('Введите цену детали\n')
  78. newSpare = {'name': name, 'price': price}
  79. spareArray.append(newSpare)
  80. newJson = {"sell": loadJsonSell(), "spare": spareArray}
  81. with open("json.json", "w") as write_file:
  82. json.dump(newJson, write_file)
  83.  
  84.  
  85. def loadJsonSpare():
  86. jDict = json.load(open('json.json', 'r'))
  87. return jDict.get('spare')
  88.  
  89.  
  90. def loadJsonSell():
  91. jDict = json.load(open('json.json', 'r'))
  92. return jDict.get('sell') # возвращает список со словарями
  93.  
  94.  
  95. n = input('\n1-линейное решение\n2-решение с событиями таймера\n3-решение с процессами\n4-добавить запись о продаже в '
  96. 'json\n5-добавить новую запчасть\nexit-выход\nВвод ')
  97.  
  98. while n != 'exit':
  99. if n == '1':
  100. linearSolution()
  101. if n == '2':
  102. timerSolution()
  103. if n == '3':
  104. processSolution()
  105. if n == '4':
  106. addSellRecord()
  107. if n == '5':
  108. addSpareRecord()
  109. if n == 'exit':
  110. break
  111. n = input('\n1-линейное решение\n2-решение с потоками\n3-решение с процессами\n4-добавить запись о продаже в '
  112. 'json\n5-добавить новую запчасть\nexit-выход\nВвод ')
  113.  
Advertisement
Add Comment
Please, Sign In to add comment