diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb66980 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__cache__ diff --git a/total_cost.py b/total_cost.py index 19fda7c..3dfb7f6 100644 --- a/total_cost.py +++ b/total_cost.py @@ -1,5 +1,7 @@ -import sys +from json.decoder import JSONDecodeError +import json import re +import sys ITEMS = {} ################################################################################################################################ # file operations # @@ -7,6 +9,33 @@ ITEMS = {} # || || || || || || || || || || || # # \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ # ################################################################################################################################ +def to_dict(obj): + return obj.__dict_repr__ + +def from_dict(d): + name = d['name'] + type = d.pop('type') + cls = globals()[type] + instance = cls(**d) + instance.__dict__.update(d) + global ITEMS + ITEMS[name] = instance + +def to_json(): + global ITEMS + if ITEMS: + ret = list(map(to_dict,ITEMS.values())) + with open('__cache__','w') as file: + json.dump(ret,file) + else: + with open('__cache__','w') as file: + json.dump([],file) +def from_json(): + global ITEMS + with open('__cache__') as file: + data = json.load(file) + for item in data: + from_dict(item) ################################################################################################################################ # user input # @@ -18,7 +47,7 @@ class user_input: def __init__(self,comp_name): valid = ('raw','compound') error = True - while not error: + while error: try: type = int(input("~{}~\ntype (0. {} | 1. {}): ".format(comp_name,*valid))) error = False @@ -52,8 +81,6 @@ def recursive_fill_in(comp_name): for sub in res.subs.keys(): recursive_fill_in(sub) - - ################################################################################################################################ # complexity # # this section is dedicated to operations that involve all processing and understanding of the input data # @@ -63,17 +90,22 @@ def recursive_fill_in(comp_name): class item: def __init__(self,name): self.name = name - global ITEMS - ITEMS[name] = self + self.__dict_repr__ = { + 'name':name, + 'type':type(self).__name__ + } class raw_material(item): - def __init__(self,name): + def __init__(self,name,**placeholder): super().__init__(name) self.complexity = 0 + class complex_item(item): - def __init__(self,name,sub_components): + def __init__(self,name,sub_components,**placeholder): super().__init__(name) self.sub_components = sub_components + self.__dict_repr__['sub_components'] = sub_components self.naive_complexity = True + self.__dict_repr__['naive_complexity'] = True self.all_sub = set(self.sub_components.keys()) def recalculate_total_completixy(self): @@ -112,14 +144,23 @@ class complex_item(item): return ret def get_build_info(self): - component_view = self.get_raw_material_cost() - material_view = self.get_component_cost() + material_view = self.get_raw_material_cost() + component_view = self.get_component_cost() build_order = component_view.items() build_order = sorted(build_order,key = lambda item: ITEMS[item[0]].complexity) return material_view,build_order def main(): + import os + if not os.path.exists('__cache__'): + to_json() + try: + from_json() + except JSONDecodeError: + to_json() + from_json() name = sys.argv[1] recursive_fill_in(name) - print(ITEMS[name].get_build_info()) + print(*ITEMS[name].get_build_info(),sep='\n') + to_json() if __name__ == "__main__": main() \ No newline at end of file