Help me implement this concept. [code] rel_path means relative to the root of MyDir.…
- User
Help me implement this concept.
pythonimport json from pathlib import Path from typing import Any import numpy as np def load_json(path) -> dict[str, Any]: path = Path(path) return json.loads(path.read_text()) def save_json(path, data) -> None: path = Path(path) return path.write_text(json.dumps(data)) # pseudo-code for a declarative custom file type that is stored as multiple files in a directory class MyDir: config: MyFile(load_fun=load_json, save_fun=save_json, rel_path="config.json") data: MyFile(load_fun=np.load, save_fun=np.save, rel_path="data.json") ts1: MyFile(load_fun=np.load, save_fun=np.save, rel_path="ts1/") # rel_path needs to be a folder now. load and save are for the files inside this folder. maybe better use a different class than Myfile? but should also work with the same I think. # everthing below is FIXED API. Everything above is just a concept and can be changed! mydir = MyDir("/tmp/testdir1") mydir.config.save({"a": 2, "b": "test"}) config = mydir.config.load() print(f"Config is saved at {mydir.config.path}") for i in range(10): ts_data = np.random.uniform(0, 1, 5) mydir.ts1.save(ts_data, key=i) all_ts_keys, all_ts_data = mydir.ts1.load_all(sorted=False) # returns tuple[list, list] example_ts_data = mydir.ts1.load(key=8) dynamic_ts_1 = mydir.new_ts("dynamic_ts_1") for i in range(20): ts_data = np.random.uniform(0, 1, 5) dynamic_ts_1.save(ts_data, key=i) dynamic_ts_1_mean = np.mean(dynamic_ts_1.load_all(sorted=False), axis=0)rel_pathmeans relative to the root of . - Assistant