From 0ced06bfc43db3136b351835198ca30ea17b3b64 Mon Sep 17 00:00:00 2001 From: JJJHolscher Date: Thu, 14 Nov 2024 11:50:48 +0100 Subject: [PATCH] use load_or_create more functionally --- pyproject.toml | 2 +- src/store.py | 58 +++++++++++++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 540ba49..fa54c8e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "jo3util" -version = "0.0.18" +version = "0.0.19" description = "" dependencies = [] dynamic = ["readme"] diff --git a/src/store.py b/src/store.py index 3650ee5..30064a2 100644 --- a/src/store.py +++ b/src/store.py @@ -69,36 +69,15 @@ class load_or_create: self.save_args=save_args self.save_json=save_json self.plain_text=plain_text + self.obj_to_args = {} + + # To be initialied by Loc. + self.fn = lambda x: x + self.arg_names = [] def __call__(self, fn): - return inner(self, fn) - + return Loc(self, fn) -class inner(load_or_create): - def __init__(self, parent, fn): - self.__dict__.update(parent.__dict__) - self.fn = fn - self.arg_names = [p.name for p in inspect.signature(fn).parameters.values()] - self.obj_to_args = dict() - - def __call__(self, *args, **kwargs): - # Store the keyword arguments into json and hash it to get the storage path. - path = self.path(*args, **kwargs) - merged_args = self.args_to_kwargs(args, kwargs, path=path) - - obj = self.load_wrapper(**merged_args) - if obj is not None: - if "file" in self.save_arg_names: self.hash_obj({"path": path} | kwargs) - return obj - - obj = self.fn(*args, **kwargs) - if obj is None: return obj - - self.save_wrapper(obj, *args, **{"path": path} | kwargs) - if "file" in self.save_arg_names: self.hash_obj({"path": path} | kwargs) - if self.save_json: path.with_suffix(".kwargs.json").write_bytes(self.to_json(**kwargs)) - - return obj def args_to_kwargs(self, args, kwargs, **extra): return extra | kwargs | {self.arg_names[i]: a for i, a in enumerate(args)} @@ -241,5 +220,30 @@ class inner(load_or_create): from loading or creating that object.. """ return LoadOrCreateCFG(*args, **kwargs) + +class Loc(load_or_create): + def __init__(self, parent, fn): + self.__dict__.update(parent.__dict__) + self.fn = fn + self.arg_names = [p.name for p in inspect.signature(fn).parameters.values()] + + def __call__(self, *args, **kwargs): + # Store the keyword arguments into json and hash it to get the storage path. + path = self.path(*args, **kwargs) + merged_args = self.args_to_kwargs(args, kwargs, path=path) + + obj = self.load_wrapper(**merged_args) + if obj is not None: + if "file" in self.save_arg_names: self.hash_obj({"path": path} | kwargs) + return obj + + obj = self.fn(*args, **kwargs) + if obj is None: return obj + + self.save_wrapper(obj, *args, **{"path": path} | kwargs) + if "file" in self.save_arg_names: self.hash_obj({"path": path} | kwargs) + if self.save_json: path.with_suffix(".kwargs.json").write_bytes(self.to_json(**kwargs)) + + return obj