Coverage for src/shephex/experiment/meta.py: 89%
28 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-06-20 14:13 +0200
« prev ^ index » next coverage.py v7.6.1, created at 2025-06-20 14:13 +0200
1import json
2from pathlib import Path
3from typing import Self
6class Meta(dict):
7 def __init__(self, name: str ='meta.json', **kwargs) -> None:
8 super().__init__(**kwargs)
9 self.name = name
11 def dump(self, directory: Path) -> None:
12 with open(directory / self.name, 'w') as f:
13 json.dump(self, f, indent=4)
15 def load(self, directory: Path) -> None:
16 with open(directory / self.name, 'r') as f:
17 data = json.load(f)
18 for key, value in data.items():
19 self.update(key, value)
21 def update(self, key: str, value: str) -> None:
22 self[key] = value
24 @classmethod
25 def from_file(cls, directory: Path, retries: int = 0) -> Self:
26 meta = cls()
27 try:
28 meta.load(directory)
29 except: # noqa: E722 - should fix but..
30 print(f"Directory: {directory} was unreadable, check the job. Exiting")
31 exit()
32 return meta
34 def get_dict(self) -> dict:
35 return dict(self.copy())