Skip to content

Commit

Permalink
general: migrate modules to use 3.9 features
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Oct 19, 2024
1 parent d3f9a8e commit 069792a
Show file tree
Hide file tree
Showing 125 changed files with 886 additions and 736 deletions.
21 changes: 13 additions & 8 deletions my/arbtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
[[https://github.com/nomeata/arbtt#arbtt-the-automatic-rule-based-time-tracker][Arbtt]] time tracking
'''

from __future__ import annotations

REQUIRES = ['ijson', 'cffi']
# NOTE likely also needs libyajl2 from apt or elsewhere?


from collections.abc import Iterable, Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import Sequence, Iterable, List, Optional


def inputs() -> Sequence[Path]:
Expand Down Expand Up @@ -55,7 +57,7 @@ def dt(self) -> datetime_aware:
return fromisoformat(ds)

@property
def active(self) -> Optional[str]:
def active(self) -> str | None:
# NOTE: WIP, might change this in the future...
ait = (w for w in self.json['windows'] if w['active'])
a = next(ait, None)
Expand All @@ -74,17 +76,18 @@ def active(self) -> Optional[str]:
def entries() -> Iterable[Entry]:
inps = list(inputs())

base: List[PathIsh] = ['arbtt-dump', '--format=json']
base: list[PathIsh] = ['arbtt-dump', '--format=json']

cmds: List[List[PathIsh]]
cmds: list[list[PathIsh]]
if len(inps) == 0:
cmds = [base] # rely on default
else:
# otherwise, 'merge' them
cmds = [[*base, '--logfile', f] for f in inps]

import ijson.backends.yajl2_cffi as ijson # type: ignore
from subprocess import Popen, PIPE
from subprocess import PIPE, Popen

import ijson.backends.yajl2_cffi as ijson # type: ignore
for cmd in cmds:
with Popen(cmd, stdout=PIPE) as p:
out = p.stdout; assert out is not None
Expand All @@ -93,8 +96,8 @@ def entries() -> Iterable[Entry]:


def fill_influxdb() -> None:
from .core.influxdb import magic_fill
from .core.freezer import Freezer
from .core.influxdb import magic_fill
freezer = Freezer(Entry)
fit = (freezer.freeze(e) for e in entries())
# TODO crap, influxdb doesn't like None https://github.com/influxdata/influxdb/issues/7722
Expand All @@ -106,6 +109,8 @@ def fill_influxdb() -> None:
magic_fill(fit, name=f'{entries.__module__}:{entries.__name__}')


from .core import stat, Stats
from .core import Stats, stat


def stats() -> Stats:
return stat(entries)
11 changes: 7 additions & 4 deletions my/bluemaestro.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
[[https://bluemaestro.com/products/product-details/bluetooth-environmental-monitor-and-logger][Bluemaestro]] temperature/humidity/pressure monitor
"""

from __future__ import annotations

# todo most of it belongs to DAL... but considering so few people use it I didn't bother for now
import re
import sqlite3
from abc import abstractmethod
from collections.abc import Iterable, Sequence
from dataclasses import dataclass
from datetime import datetime, timedelta
from pathlib import Path
from typing import Iterable, Optional, Protocol, Sequence, Set
from typing import Protocol

import pytz

Expand Down Expand Up @@ -87,17 +90,17 @@ def measurements() -> Iterable[Res[Measurement]]:
total = len(paths)
width = len(str(total))

last: Optional[datetime] = None
last: datetime | None = None

# tables are immutable, so can save on processing..
processed_tables: Set[str] = set()
processed_tables: set[str] = set()
for idx, path in enumerate(paths):
logger.info(f'processing [{idx:>{width}}/{total:>{width}}] {path}')
tot = 0
new = 0
# todo assert increasing timestamp?
with sqlite_connect_immutable(path) as db:
db_dt: Optional[datetime] = None
db_dt: datetime | None = None
try:
datas = db.execute(
f'SELECT "{path.name}" as name, Time, Temperature, Humidity, Pressure, Dewpoint FROM data ORDER BY log_index'
Expand Down
36 changes: 19 additions & 17 deletions my/body/blood.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@
Blood tracking (manual org-mode entries)
"""

from datetime import datetime
from typing import Iterable, NamedTuple, Optional

from ..core.error import Res
from ..core.orgmode import parse_org_datetime, one_table
from __future__ import annotations

from collections.abc import Iterable
from datetime import datetime
from typing import NamedTuple

import pandas as pd
import orgparse

import pandas as pd

from my.config import blood as config # type: ignore[attr-defined]

from ..core.error import Res
from ..core.orgmode import one_table, parse_org_datetime


class Entry(NamedTuple):
dt: datetime

ketones : Optional[float]=None
glucose : Optional[float]=None
ketones : float | None=None
glucose : float | None=None

vitamin_d : Optional[float]=None
vitamin_b12 : Optional[float]=None
vitamin_d : float | None=None
vitamin_b12 : float | None=None

hdl : Optional[float]=None
ldl : Optional[float]=None
triglycerides: Optional[float]=None
hdl : float | None=None
ldl : float | None=None
triglycerides: float | None=None

source : Optional[str]=None
extra : Optional[str]=None
source : str | None=None
extra : str | None=None


Result = Res[Entry]


def try_float(s: str) -> Optional[float]:
def try_float(s: str) -> float | None:
l = s.split()
if len(l) == 0:
return None
Expand Down Expand Up @@ -105,6 +106,7 @@ def blood_tests_data() -> Iterable[Result]:

def data() -> Iterable[Result]:
from itertools import chain

from ..core.error import sort_res_by
datas = chain(glucose_ketones_data(), blood_tests_data())
return sort_res_by(datas, key=lambda e: e.dt)
Expand Down
6 changes: 3 additions & 3 deletions my/body/exercise/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
@check_dataframe
def dataframe() -> DataFrameT:
# this should be somehow more flexible...
from ...endomondo import dataframe as EDF
from ...runnerup import dataframe as RDF

import pandas as pd

from ...endomondo import dataframe as EDF
from ...runnerup import dataframe as RDF
return pd.concat([
EDF(),
RDF(),
Expand Down
1 change: 0 additions & 1 deletion my/body/exercise/cardio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'''
from ...core.pandas import DataFrameT, check_dataframe


CARDIO = {
'Running',
'Running, treadmill',
Expand Down
16 changes: 10 additions & 6 deletions my/body/exercise/cross_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
For now it's worth keeping it here as an example and perhaps utility functions might be useful for other HPI modules.
'''

from __future__ import annotations

from datetime import datetime, timedelta
from typing import Optional

from ...core.pandas import DataFrameT, check_dataframe as cdf
from ...core.orgmode import collect, Table, parse_org_datetime, TypedTable
import pytz

from my.config import exercise as config

from ...core.orgmode import Table, TypedTable, collect, parse_org_datetime
from ...core.pandas import DataFrameT
from ...core.pandas import check_dataframe as cdf

import pytz
# FIXME how to attach it properly?
tz = pytz.timezone('Europe/London')

Expand Down Expand Up @@ -114,7 +116,7 @@ def dataframe() -> DataFrameT:
rows.append(rd) # presumably has an error set
continue

idx: Optional[int]
idx: int | None
close = edf[edf['start_time'].apply(lambda t: pd_date_diff(t, mdate)).abs() < _DELTA]
if len(close) == 0:
idx = None
Expand Down Expand Up @@ -163,7 +165,9 @@ def dataframe() -> DataFrameT:
# TODO wtf?? where is speed coming from??


from ...core import stat, Stats
from ...core import Stats, stat


def stats() -> Stats:
return stat(cross_trainer_data)

Expand Down
5 changes: 3 additions & 2 deletions my/body/sleep/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ...core import stat, Stats
from ...core.pandas import DataFrameT, check_dataframe as cdf
from ...core import Stats, stat
from ...core.pandas import DataFrameT
from ...core.pandas import check_dataframe as cdf


class Combine:
Expand Down
5 changes: 2 additions & 3 deletions my/body/sleep/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from ... import jawbone
from ... import emfit

from ... import emfit, jawbone
from .common import Combine

_combined = Combine([
jawbone,
emfit,
Expand Down
6 changes: 3 additions & 3 deletions my/body/weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Weight data (manually logged)
'''

from collections.abc import Iterator
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Iterator
from typing import Any

from my import orgmode
from my.core import make_logger
from my.core.error import Res, extract_error_datetime, set_error_datetime

from my import orgmode

config = Any


Expand Down
7 changes: 3 additions & 4 deletions my/books/kobo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from ..core import warnings
from my.core import warnings

warnings.high('my.books.kobo is deprecated! Please use my.kobo instead!')

from ..core.util import __NOT_HPI_MODULE__

from ..kobo import * # type: ignore[no-redef]
from my.core.util import __NOT_HPI_MODULE__
from my.kobo import * # type: ignore[no-redef]
8 changes: 5 additions & 3 deletions my/browser/active_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ class config(user_config.active_browser):
export_path: Paths


from collections.abc import Iterator, Sequence
from pathlib import Path
from typing import Sequence, Iterator

from my.core import get_files, Stats, make_logger
from browserexport.merge import read_visits, Visit
from browserexport.merge import Visit, read_visits
from sqlite_backup import sqlite_backup

from my.core import Stats, get_files, make_logger

logger = make_logger(__name__)

from .common import _patch_browserexport_logs

_patch_browserexport_logs(logger.level)


Expand Down
6 changes: 3 additions & 3 deletions my/browser/all.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Iterator
from collections.abc import Iterator

from browserexport.merge import Visit, merge_visits

from my.core import Stats
from my.core.source import import_source
from browserexport.merge import merge_visits, Visit


src_export = import_source(module_name="my.browser.export")
src_active = import_source(module_name="my.browser.active_browser")
Expand Down
9 changes: 5 additions & 4 deletions my/browser/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

REQUIRES = ["browserexport"]

from collections.abc import Iterator, Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import Iterator, Sequence

import my.config
from browserexport.merge import Visit, read_and_merge

from my.core import (
Paths,
Stats,
Expand All @@ -18,10 +19,10 @@
)
from my.core.cachew import mcachew

from browserexport.merge import read_and_merge, Visit

from .common import _patch_browserexport_logs

import my.config # isort: skip


@dataclass
class config(my.config.browser.export):
Expand Down
Loading

0 comments on commit 069792a

Please sign in to comment.