DacCum

json

May 16th, 2022 (edited)
999
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2.  
  3.  
  4. def clear():
  5.     print("\n"*47)
  6.  
  7.  
  8. def json_print(json_filename):
  9.     with open(json_filename, "r") as json_name:
  10.         for i, j in dict(json.load(json_name)).items():
  11.             print(f"{i}: ")
  12.             print(f"\tNumber of copies: {j[0]}")
  13.             print(f"\tPrice: {j[1]}")
  14.  
  15.  
  16. def json_add(json_filename):
  17.     with open(json_filename, "r") as json_name:
  18.         tmp_dict = dict(json.load(json_name))
  19.  
  20.     new_magazine_name = input("Enter magazine name: ")
  21.     tmp = tmp_dict.get(new_magazine_name, -1)
  22.     while tmp != -1:
  23.         print("ERROR: this name was used.")
  24.         print("Please, try again")
  25.         new_magazine_name = input("Enter magazine name: ")
  26.         tmp = tmp_dict.get(new_magazine_name, -1)
  27.  
  28.     new_number_of_copies = int(input("Enter number of copies magazine: "))
  29.     new_price = int(input("Enter price: "))
  30.     tmp_dict.update({new_magazine_name: (new_number_of_copies, new_price)})
  31.  
  32.     with open(json_filename, "w") as json_name:
  33.         json.dump(tmp_dict, json_name)
  34.  
  35.  
  36. def json_del_elem(json_filename):
  37.     with open(json_filename, "r") as json_name:
  38.         tmp_dict = dict(json.load(json_name))
  39.  
  40.     del_name = input("Enter name you want to delete: ")
  41.     tmp = tmp_dict.get(del_name, -1)
  42.     while tmp == -1:
  43.         print("ERROR: this name not found.")
  44.         print("Please, try again")
  45.         del_name = input("Enter name you want to delete: ")
  46.         tmp = tmp_dict.get(del_name, -1)
  47.  
  48.     tmp_dict.pop(del_name)
  49.  
  50.     with open(json_filename, "w") as json_name:
  51.         json.dump(tmp_dict, json_name)
  52.  
  53.  
  54. def json_search(json_filename):
  55.     with open(json_filename, "r") as json_name:
  56.         tmp_dict = dict(json.load(json_name))
  57.  
  58.     del_name = input("Enter name you want to search: ")
  59.     tmp = tmp_dict.get(del_name, -1)
  60.     if tmp == -1:
  61.         print("This name not found.")
  62.     else:
  63.         print(f"\tNumber of copies: {tmp[0]}")
  64.         print(f"\tPrice: {tmp[1]}")
  65.  
  66.     return tmp
  67.  
  68.  
  69. def json_calc(json_filename):
  70.     with open(json_filename, "r") as json_name:
  71.         tmp_dict = dict(json.load(json_name))
  72.  
  73.     res = 0
  74.     count = 0
  75.     for i, j in tmp_dict.items():
  76.         if j[0] < 10000:
  77.             count += 1
  78.             res += j[1]
  79.     res /= count
  80.     return res
  81.  
  82.  
  83. def menuPrint():
  84.         clear()
  85.         print("1 - Show All element")
  86.         print("2 - Search element")
  87.         print("3 - Delete element")
  88.         print("4 - Add new element or updates")
  89.         print("5 - Average price of magazines with a circulation of less than 10000")
  90.         print("0 - Exit")
  91.         pressKey = input("Enter a selection -> ")
  92.         clear()
  93.         return pressKey
  94.  
  95.  
  96. def json_menu(json_filename):
  97.     pressKey = menuPrint()
  98.     if pressKey == '1':
  99.         json_print(file_name)
  100.     elif pressKey == '2':
  101.         json_search(json_filename)
  102.     elif pressKey == '3':
  103.         json_del_elem(file_name)
  104.         json_print(file_name)
  105.     elif pressKey == '4':
  106.         json_add(file_name)
  107.         json_print(file_name)
  108.     elif pressKey == '5':
  109.         average_price = json_calc(file_name)
  110.         print(f"Average price of magazines with a circulation of less than 10000: {average_price}")
  111.     elif pressKey == '0':
  112.         exit(0)
  113.     else:
  114.         print("ERROR: menu under this number does not exist.")
  115.     input("Enter any key to continue -> ")
  116.     json_menu(json_filename)
  117.  
  118.  
  119. name_magazine = ("eqwdfszx", "bgfdvscz", "bvvtgf", "sbtygfdfvsd", "asdf")
  120. number_of_copies = [8000, 14000, 20000, 9000, 16000]
  121. price = [1100, 1000, 1200, 500, 1300]
  122.  
  123. magazine_dict = {k: v for k, v in zip(name_magazine, {k: v for k, v in zip(number_of_copies, price)}.items())}
  124.  
  125. file_name = "file_name.json"
  126. with open(file_name, "w") as write_json:
  127.     json.dump(magazine_dict, write_json)
  128.  
  129. json_menu(file_name)
  130.  
RAW Paste Data Copied