Skip to content

Commit

Permalink
my.bluemaestro: make config construction lazy
Browse files Browse the repository at this point in the history
following the discussions here: #46 (comment)
  • Loading branch information
karlicoss committed Aug 25, 2024
1 parent 9f017fb commit d154825
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
51 changes: 36 additions & 15 deletions my/bluemaestro.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,58 @@
"""

# 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 dataclasses import dataclass
from datetime import datetime, timedelta
from pathlib import Path
import re
import sqlite3
from typing import Iterable, Sequence, Set, Optional
from typing import Iterable, Optional, Protocol, Sequence, Set

import pytz

from my.core import (
Paths,
Res,
Stats,
get_files,
make_logger,
Res,
stat,
Stats,
influxdb,
unwrap,
)
from my.core.cachew import mcachew
from my.core.error import unwrap
from my.core.pandas import DataFrameT, as_dataframe
from my.core.sqlite import sqlite_connect_immutable

from my.config import bluemaestro as config

class config(Protocol):
@property
@abstractmethod
def export_path(self) -> Paths:
raise NotImplementedError

@property
def tz(self) -> pytz.BaseTzInfo:
# fixme: later, rely on the timezone provider
# NOTE: the timezone should be set with respect to the export date!!!
return pytz.timezone('Europe/London')
# TODO when I change tz, check the diff


def make_config() -> config:
from my.config import bluemaestro as user_config

class combined_config(user_config, config): ...

return combined_config()


logger = make_logger(__name__)


def inputs() -> Sequence[Path]:
return get_files(config.export_path)
cfg = make_config()
return get_files(cfg.export_path)


Celsius = float
Expand All @@ -50,12 +72,6 @@ class Measurement:
dewpoint: Celsius


# fixme: later, rely on the timezone provider
# NOTE: the timezone should be set with respect to the export date!!!
tz = pytz.timezone('Europe/London')
# TODO when I change tz, check the diff


def is_bad_table(name: str) -> bool:
# todo hmm would be nice to have a hook that can patch any module up to
delegate = getattr(config, 'is_bad_table', None)
Expand All @@ -64,6 +80,9 @@ def is_bad_table(name: str) -> bool:

@mcachew(depends_on=inputs)
def measurements() -> Iterable[Res[Measurement]]:
cfg = make_config()
tz = cfg.tz

# todo ideally this would be via arguments... but needs to be lazy
paths = inputs()
total = len(paths)
Expand Down Expand Up @@ -211,6 +230,8 @@ def dataframe() -> DataFrameT:


def fill_influxdb() -> None:
from my.core import influxdb

influxdb.fill(measurements(), measurement=__name__)


Expand Down
8 changes: 2 additions & 6 deletions tests/bluemaestro.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from pathlib import Path
from typing import TYPE_CHECKING, Iterator, Any
from typing import Iterator

from more_itertools import one

import pytest


if TYPE_CHECKING:
from my.bluemaestro import Measurement
else:
Measurement = Any
from my.bluemaestro import measurements, Measurement


def ok_measurements() -> Iterator[Measurement]:
from my.bluemaestro import measurements
for m in measurements():
assert not isinstance(m, Exception)
yield m
Expand Down

0 comments on commit d154825

Please sign in to comment.