general: migrate modules to use 3.9 features

This commit is contained in:
Dima Gerasimov 2024-10-19 22:10:40 +01:00
parent d3f9a8e8b6
commit d915c848e9
125 changed files with 889 additions and 739 deletions

View file

@ -1,10 +1,11 @@
from __future__ import annotations
from typing import Dict, Any, List, Iterable
import json
from collections.abc import Iterable
from datetime import date, datetime, time, timedelta
from functools import lru_cache
from datetime import datetime, date, time, timedelta
from pathlib import Path
from typing import Any
import pytz
@ -14,7 +15,6 @@ logger = make_logger(__name__)
from my.config import jawbone as config # type: ignore[attr-defined]
BDIR = config.export_dir
PHASES_FILE = BDIR / 'phases.json'
SLEEPS_FILE = BDIR / 'sleeps.json'
@ -24,7 +24,7 @@ GRAPHS_DIR = BDIR / 'graphs'
XID = str # TODO how to shared with backup thing?
Phases = Dict[XID, Any]
Phases = dict[XID, Any]
@lru_cache(1)
def get_phases() -> Phases:
return json.loads(PHASES_FILE.read_text())
@ -89,7 +89,7 @@ class SleepEntry:
# TODO might be useful to cache these??
@property
def phases(self) -> List[datetime]:
def phases(self) -> list[datetime]:
# TODO make sure they are consistent with emfit?
return [self._fromts(i['time']) for i in get_phases()[self.xid]]
@ -100,12 +100,13 @@ class SleepEntry:
return str(self)
def load_sleeps() -> List[SleepEntry]:
def load_sleeps() -> list[SleepEntry]:
sleeps = json.loads(SLEEPS_FILE.read_text())
return [SleepEntry(js) for js in sleeps]
from ..core.error import Res, set_error_datetime, extract_error_datetime
from ..core.error import Res, extract_error_datetime, set_error_datetime
def pre_dataframe() -> Iterable[Res[SleepEntry]]:
from more_itertools import bucket
@ -129,9 +130,9 @@ def pre_dataframe() -> Iterable[Res[SleepEntry]]:
def dataframe():
dicts: List[Dict[str, Any]] = []
dicts: list[dict[str, Any]] = []
for s in pre_dataframe():
d: Dict[str, Any]
d: dict[str, Any]
if isinstance(s, Exception):
dt = extract_error_datetime(s)
d = {
@ -181,7 +182,7 @@ def plot_one(sleep: SleepEntry, fig, axes, xlims=None, *, showtext=True):
print(f"{sleep.xid} span: {span}")
# pip install imageio
from imageio import imread # type: ignore
from imageio import imread # type: ignore
img = imread(sleep.graph)
# all of them are 300x300 images apparently
@ -260,8 +261,8 @@ def predicate(sleep: SleepEntry):
# TODO move to dashboard
def plot() -> None:
from matplotlib.figure import Figure # type: ignore[import-not-found]
import matplotlib.pyplot as plt # type: ignore[import-not-found]
from matplotlib.figure import Figure # type: ignore[import-not-found]
# TODO FIXME melatonin data
melatonin_data = {} # type: ignore[var-annotated]

View file

@ -1,10 +1,11 @@
#!/usr/bin/env python3
# TODO this should be in dashboard
from pathlib import Path
# from kython.plotting import *
from csv import DictReader
from pathlib import Path
from typing import Any, NamedTuple
from typing import Dict, Any, NamedTuple
import matplotlib.pylab as pylab # type: ignore
# sleep = []
# with open('2017.csv', 'r') as fo:
@ -12,16 +13,14 @@ from typing import Dict, Any, NamedTuple
# for line in islice(reader, 0, 10):
# sleep
# print(line)
import matplotlib.pyplot as plt # type: ignore
import matplotlib.pyplot as plt # type: ignore
from numpy import genfromtxt
import matplotlib.pylab as pylab # type: ignore
pylab.rcParams['figure.figsize'] = (32.0, 24.0)
pylab.rcParams['font.size'] = 10
jawboneDataFeatures = Path(__file__).parent / 'features.csv' # Data File Path
featureDesc: Dict[str, str] = {}
featureDesc: dict[str, str] = {}
for x in genfromtxt(jawboneDataFeatures, dtype='unicode', delimiter=','):
featureDesc[x[0]] = x[1]
@ -52,7 +51,7 @@ class SleepData(NamedTuple):
quality: float # ???
@classmethod
def from_jawbone_dict(cls, d: Dict[str, Any]):
def from_jawbone_dict(cls, d: dict[str, Any]):
return cls(
date=d['DATE'],
asleep_time=_safe_mins(_safe_float(d['s_asleep_time'])),
@ -75,7 +74,7 @@ class SleepData(NamedTuple):
def iter_useful(data_file: str):
with open(data_file) as fo:
with Path(data_file).open() as fo:
reader = DictReader(fo)
for d in reader:
dt = SleepData.from_jawbone_dict(d)
@ -95,6 +94,7 @@ files = [
]
from kython import concat, parse_date # type: ignore
useful = concat(*(list(iter_useful(str(f))) for f in files))
# for u in useful:
@ -108,6 +108,7 @@ dates = [parse_date(u.date, yearfirst=True, dayfirst=False) for u in useful]
# TODO don't need this anymore? it's gonna be in dashboards package
from kython.plotting import plot_timestamped # type: ignore
for attr, lims, mavg, fig in [
('light', (0, 400), 5, None),
('deep', (0, 600), 5, None),