fix json decoding when loading from db

This commit is contained in:
Valentin Boettcher 2022-11-29 10:57:26 -05:00
parent 7eb02405d7
commit 3c5d373e0f
No known key found for this signature in database
GPG key ID: E034E12B7AF56ACE
2 changed files with 6 additions and 5 deletions

View file

@ -42,9 +42,11 @@ def model_db(data_path: str = "./.data"):
db_path.touch(exist_ok=True) db_path.touch(exist_ok=True)
with db_path.open("r+") as f: with db_path.open("r+") as f:
data = f.read() data = f.read()
db = ( db = JSONEncoder.loads(data) if len(data) > 0 else {}
JSONEncoder.loads(data, object_hook=model_hook) if len(data) > 0 else {} db = {
) key: model_hook(value) if isinstance(value, dict) else value
for key, value in db.items()
}
yield db yield db
@ -76,7 +78,7 @@ def model_hook(dct: dict[str, Any]):
if model == "OttoEngine": if model == "OttoEngine":
return OttoEngine.from_dict(treated_vals) return OttoEngine.from_dict(treated_vals)
return object_hook(dct) return dct
def integrate_multi(models: Sequence[Model], *args, **kwargs): def integrate_multi(models: Sequence[Model], *args, **kwargs):

View file

@ -63,7 +63,6 @@ class JSONEncoder(json.JSONEncoder):
@default.register(tuple) @default.register(tuple)
def _(self, obj: tuple): def _(self, obj: tuple):
print("ho")
return { return {
"type": "tuple", "type": "tuple",
"value": list(*obj), "value": list(*obj),