From 320a7204e30ae8adef04cc51696a92da60e852e5 Mon Sep 17 00:00:00 2001 From: justuser31 Date: Sat, 4 May 2024 18:53:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20update=5Fdb=20=D0=B8=20=D0=BC=D0=B5=D0=BB?= =?UTF-8?q?=D0=BA=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reworked/calc.py | 8 +++++++ reworked/db.py | 17 +++++++++----- reworked/precalc__.py | 7 ++++++ reworked/update_db.py | 52 ++++++++++++++++++++++++++++++------------- 4 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 reworked/precalc__.py diff --git a/reworked/calc.py b/reworked/calc.py index 5bfdbe4..ca3e81b 100644 --- a/reworked/calc.py +++ b/reworked/calc.py @@ -1,3 +1,5 @@ +from precalc__ import precalc__ + global expanded def expand_recipe(recipe, recipes, main = False): global expanded @@ -48,3 +50,9 @@ def calc(element, amount, recipes): return expanded, vol_in, vol_out + +def calc_all(recipes, amount): + precalc = {} + for el in recipes: + precalc[el] = precalc__(calc(el, amount, recipes)) + return precalc diff --git a/reworked/db.py b/reworked/db.py index 0a7263c..6fe6817 100644 --- a/reworked/db.py +++ b/reworked/db.py @@ -1,13 +1,14 @@ import os import json from reag__ import reag__ +from precalc__ import precalc__ -if not os.path.exists('db.json'): +if not os.path.exists('precalc.json'): db = {} js = json.dumps(db, indent=2) - with open('db.json', 'w') as outfile: + with open('precalc.json', 'w') as outfile: outfile.write(js) - print('Created new db.json') + print('Created new precalc.json') if not os.path.exists('raw_db.json'): db = {} js = json.dumps(db, indent=2) @@ -35,9 +36,13 @@ def save(db, file): raw[el] = class_data write_db(raw, file) -def load(file): +def load(file, type = 'raw'): raw = read_db(file) db = {} - for el in raw: - db[el] = reag__(raw[el][0], raw[el][1], raw[el][2]) + if type == 'raw': + for el in raw: + db[el] = reag__(raw[el][0], raw[el][1], raw[el][2]) + elif type == 'precalc': + for el in raw: + db[el] = precalc__(raw[el]) return db diff --git a/reworked/precalc__.py b/reworked/precalc__.py new file mode 100644 index 0000000..50a3211 --- /dev/null +++ b/reworked/precalc__.py @@ -0,0 +1,7 @@ +class precalc__: + def __init__(self, els): + self.recipe = els[0] + self.vol_in = els[1] + self.vol_out = els[2] + def get_all(self): + return [self.recipe, self.vol_in, self.vol_out] diff --git a/reworked/update_db.py b/reworked/update_db.py index bda15c2..1efac9a 100644 --- a/reworked/update_db.py +++ b/reworked/update_db.py @@ -1,23 +1,43 @@ from parse import * -from calc import calc +from calc import * from db import * -''' -# Загружаем локализацию -locales_url = ['medicine', 'chemicals'] -locales = load_locales(locales_url) +print('''1. Обновить всё. +2. Пересчитать рецепты. +''') +inp = input(">> ") +print('\n') -# Загружаем сырые рецепты -recipes_url = ['medicine'] -raw_recipes = load_recipes(recipes_url) +vols = [30, 50, 100] +if inp == '1': + print('Парсим и обрабатываем данные...') + # Загружаем локализацию + locales_url = ['medicine', 'chemicals'] + locales = load_locales(locales_url) -# Локализируем -recipes = localize(raw_recipes, locales) -# Сохранаяем данные -save(recipes, 'raw_db.json') -''' + # Загружаем сырые рецепты + recipes_url = ['medicine'] + raw_recipes = load_recipes(recipes_url) -rec = load('raw_db.json') + # Локализируем + recipes = localize(raw_recipes, locales) -# Делаем предрасчёты -#calculated = + save(recipes, 'raw_db.json') + print('Сохранены минимальные рецепты в raw_db.json') + print('Выполняем предрасчёты...') + + for i in vols: + precalc = calc_all(recipes, i) + save(precalc, f'{i}_calc.json') + print(f'Данные сохранены в {i}_calc.json') +elif inp == '2': + print('Выполняем расчёты...') + recipes = load('raw_db.json') + for i in vols: + precalc = calc_all(recipes, i) + save(precalc, f'{i}_calc.json') + print(f'Данные сохранены в {i}_calc.json') +else: + exit() + +print("ГОТОВО.")