diff --git a/reworked/db.py b/reworked/db.py index 92630b3..0a7263c 100644 --- a/reworked/db.py +++ b/reworked/db.py @@ -1,20 +1,43 @@ import os import json +from reag__ import reag__ if not os.path.exists('db.json'): db = {} js = json.dumps(db, indent=2) - with open("db.json", "w") as outfile: + with open('db.json', 'w') as outfile: outfile.write(js) print('Created new db.json') +if not os.path.exists('raw_db.json'): + db = {} + js = json.dumps(db, indent=2) + with open('raw_db.json', 'w') as outfile: + outfile.write(js) + print('Created new raw_db.json') -def read_db(file = 'db.json'): - with open(file, "r", encoding="utf-8") as openfile: +def read_db(file): + with open(file, 'r', encoding='utf-8') as openfile: db = json.load(openfile) return db -def write_db(db, file = 'db.json'): +def write_db(db, file): js = json.dumps(db, indent=2, ensure_ascii=False) - with open(file, "w", encoding="utf-8") as outfile: + with open(file, 'w', encoding='utf-8') as outfile: outfile.write(js) + + + +def save(db, file): + raw = {} + for el in db: + class_data = db[el].get_all() + raw[el] = class_data + write_db(raw, file) + +def load(file): + raw = read_db(file) + db = {} + for el in raw: + db[el] = reag__(raw[el][0], raw[el][1], raw[el][2]) + return db diff --git a/reworked/parse.py b/reworked/parse.py index 6e37f62..dcb67b6 100644 --- a/reworked/parse.py +++ b/reworked/parse.py @@ -40,7 +40,7 @@ def load_recipes(recipes_url, prefix = 'https://raw.githubusercontent.com/Serbia comps[elem] = element["reactants"][elem]["amount"] for id, value in element["products"].items(): out = value - recipes[product] = reag__(category=el, comps=comps, out=out) + recipes[product] = reag__(comps=comps, out=out, category=el) return recipes diff --git a/reworked/reag__.py b/reworked/reag__.py index 19e8419..ddabbfd 100644 --- a/reworked/reag__.py +++ b/reworked/reag__.py @@ -1,11 +1,11 @@ class reag__: - def __init__(self, category: str = '-', comps = {}, out: int = 0): - # medicine - self.category = category + def __init__(self, comps, out, category = '-'): # {'инапровалин': 1, 'углерод': 1} self.comps = comps # 2 self.out = out + # medicine + self.category = category def get_all(self): - return [self.category, self.comps, self.out] + return [self.comps, self.out, self.category] diff --git a/reworked/update_db.py b/reworked/update_db.py new file mode 100644 index 0000000..bda15c2 --- /dev/null +++ b/reworked/update_db.py @@ -0,0 +1,23 @@ +from parse import * +from calc import calc +from db import * + +''' +# Загружаем локализацию +locales_url = ['medicine', 'chemicals'] +locales = load_locales(locales_url) + +# Загружаем сырые рецепты +recipes_url = ['medicine'] +raw_recipes = load_recipes(recipes_url) + +# Локализируем +recipes = localize(raw_recipes, locales) +# Сохранаяем данные +save(recipes, 'raw_db.json') +''' + +rec = load('raw_db.json') + +# Делаем предрасчёты +#calculated =