Repository navigation
Expand file tree
/
Copy pathtracker.py
More file actions
73 lines (57 loc) · 1.99 KB
/
Copy pathtracker.py
File metadata and controls
73 lines (57 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Local storage and data helpers for the Experiment Tracker."""
from __future__ import annotations
import json
from dataclasses import asdict, dataclass
from datetime import date
from pathlib import Path
from uuid import uuid4
STATUSES = ("Planned", "Running", "Completed", "Paused")
@dataclass
class Experiment:
id: str
name: str
hypothesis: str
independent_variable: str
dependent_variable: str
controls: str
results: str
status: str
start_date: str
end_date: str
@classmethod
def new(cls, **values: str) -> "Experiment":
return cls(id=str(uuid4()), **values)
class ExperimentStore:
"""Keeps experiments in a readable JSON file on this computer."""
def __init__(self, path: str | Path):
self.path = Path(path)
def load(self) -> list[Experiment]:
if not self.path.exists():
return []
try:
raw_items = json.loads(self.path.read_text(encoding="utf-8"))
return [Experiment(**item) for item in raw_items]
except (json.JSONDecodeError, OSError, TypeError, KeyError) as error:
raise ValueError("Your experiment data file could not be read.") from error
def save(self, experiments: list[Experiment]) -> None:
self.path.parent.mkdir(parents=True, exist_ok=True)
self.path.write_text(
json.dumps([asdict(experiment) for experiment in experiments], indent=2),
encoding="utf-8",
)
def matches_search(experiment: Experiment, query: str) -> bool:
"""Return true when a query occurs in any useful experiment field."""
searchable = " ".join(
[
experiment.name,
experiment.hypothesis,
experiment.independent_variable,
experiment.dependent_variable,
experiment.controls,
experiment.results,
experiment.status,
]
).lower()
return query.strip().lower() in searchable
def today() -> str:
return date.today().isoformat()