Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def change_several_books(self, start_list_dictionaries):
- changed_list_dictionaries = start_list_dictionaries.copy()
- shortest_dict = min(changed_list_dictionaries, key=len)
- dict_for_change = shortest_dict.copy()
- for key, value in dict_for_change.items():
- if isinstance(value, dict):
- for desc, type_value in value.items():
- new_value = input(f"--------<{desc}>: ")
- if new_value in ["del", "<del>"]:
- dict_for_change[key] = "<del>"
- elif new_value == "":
- dict_for_change[key] = "<continue>"
- else:
- if isinstance(type_value, (str, int, dict)):
- changed_dictionary[key_in_initial_dict] = {desc: new_value}
- elif isinstance(type_value, list):
- try:
- changed_dictionary[key_in_initial_dict] =\
- {desc: json.loads(new_value)}
- except json.decoder.JSONDecodeError:
- changed_dictionary[key_in_initial_dict] =\
- {desc: [new_value]}
- for dictionary in changed_list_dictionaries:
- for initial_key in dictionary.copy().keys():
- for key_for_change, value_for_change in dict_for_change.items():
- if initial_key == key_for_change:
- if value_for_change == "<continue>":
- pass
- elif value_for_change == "<del>":
- del dictionary[initial_key]
- elif isinstance(value_for_change, dict):
- dictionary[initial_key] = value_for_change
- with open(self.full_path, 'r') as file:
- file_contents = json.load(file)
- file_contents = [
- element
- for element in file_contents
- if element not in start_list_dictionaries
- ]
- file_contents.extend(changed_list_dictionaries)
- with open(self.full_path, 'w') as file:
- json.dump(file_contents, file)
- print("\nSuccess!")
Add Comment
Please, Sign In to add comment