Browse Source

Fixed bugs and added caching feature

master
Raphael Roberts 7 years ago
parent
commit
eca5c21590
  1. 1
      .gitignore
  2. 63
      total_cost.py

1
.gitignore

@ -0,0 +1 @@
__cache__

63
total_cost.py

@ -1,5 +1,7 @@
import sys
from json.decoder import JSONDecodeError
import json
import re import re
import sys
ITEMS = {} ITEMS = {}
################################################################################################################################ ################################################################################################################################
# file operations # # 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 # # user input #
@ -18,7 +47,7 @@ class user_input:
def __init__(self,comp_name): def __init__(self,comp_name):
valid = ('raw','compound') valid = ('raw','compound')
error = True error = True
while not error:
while error:
try: try:
type = int(input("~{}~\ntype (0. {} | 1. {}): ".format(comp_name,*valid))) type = int(input("~{}~\ntype (0. {} | 1. {}): ".format(comp_name,*valid)))
error = False error = False
@ -52,8 +81,6 @@ def recursive_fill_in(comp_name):
for sub in res.subs.keys(): for sub in res.subs.keys():
recursive_fill_in(sub) recursive_fill_in(sub)
################################################################################################################################ ################################################################################################################################
# complexity # # complexity #
# this section is dedicated to operations that involve all processing and understanding of the input data # # 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: class item:
def __init__(self,name): def __init__(self,name):
self.name = name self.name = name
global ITEMS
ITEMS[name] = self
self.__dict_repr__ = {
'name':name,
'type':type(self).__name__
}
class raw_material(item): class raw_material(item):
def __init__(self,name):
def __init__(self,name,**placeholder):
super().__init__(name) super().__init__(name)
self.complexity = 0 self.complexity = 0
class complex_item(item): class complex_item(item):
def __init__(self,name,sub_components):
def __init__(self,name,sub_components,**placeholder):
super().__init__(name) super().__init__(name)
self.sub_components = sub_components self.sub_components = sub_components
self.__dict_repr__['sub_components'] = sub_components
self.naive_complexity = True self.naive_complexity = True
self.__dict_repr__['naive_complexity'] = True
self.all_sub = set(self.sub_components.keys()) self.all_sub = set(self.sub_components.keys())
def recalculate_total_completixy(self): def recalculate_total_completixy(self):
@ -112,14 +144,23 @@ class complex_item(item):
return ret return ret
def get_build_info(self): 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 = component_view.items()
build_order = sorted(build_order,key = lambda item: ITEMS[item[0]].complexity) build_order = sorted(build_order,key = lambda item: ITEMS[item[0]].complexity)
return material_view,build_order return material_view,build_order
def main(): 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] name = sys.argv[1]
recursive_fill_in(name) recursive_fill_in(name)
print(ITEMS[name].get_build_info())
print(*ITEMS[name].get_build_info(),sep='\n')
to_json()
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Loading…
Cancel
Save