Сохранение рецептов, изменение структуры класса reag__

main
justuser31 6 months ago
parent 5cce77aa10
commit 0ef9fc1de9

@ -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

@ -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

@ -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]

@ -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 =
Loading…
Cancel
Save