From e63c159b80f6779afa9b8dd6dc21878758aec164 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 3 Oct 2020 15:10:10 +0100 Subject: [PATCH] my.body.exercise: add more annotations & ci check --- doc/example_config/my/config/__init__.py | 5 ++++- my/body/exercise/cardio.py | 10 ++++------ my/body/exercise/cross_trainer.py | 20 ++++++++++---------- tox.ini | 21 +++++++++++++-------- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/doc/example_config/my/config/__init__.py b/doc/example_config/my/config/__init__.py index 7aba5ca..ff7d1ed 100644 --- a/doc/example_config/my/config/__init__.py +++ b/doc/example_config/my/config/__init__.py @@ -2,7 +2,7 @@ Feel free to remove this if you don't need it/add your own custom settings and use them """ -from my.core import Paths +from my.core import Paths, PathIsh class hypothesis: # expects outputs from https://github.com/karlicoss/hypexport @@ -23,3 +23,6 @@ class reddit: class endomondo: export_path: Paths = '' + +class exercise: + workout_log: PathIsh = '/some/path.org' diff --git a/my/body/exercise/cardio.py b/my/body/exercise/cardio.py index 2c6a7e5..1879814 100644 --- a/my/body/exercise/cardio.py +++ b/my/body/exercise/cardio.py @@ -1,9 +1,7 @@ ''' Cardio data, filtered from Endomondo and inferred from other data sources ''' -from ...core.pandas import check_dataframe as cdf - -import pandas as pd # type: ignore +from ...core.pandas import DataFrameT, check_dataframe as cdf CARDIO = { @@ -24,10 +22,10 @@ NOT_CARDIO = { @cdf -def endomondo_cardio() -> pd.DataFrame: +def endomondo_cardio() -> DataFrameT: assert len(CARDIO.intersection(NOT_CARDIO)) == 0, (CARDIO, NOT_CARDIO) - from ..endomondo import dataframe as EDF + from ...endomondo import dataframe as EDF df = EDF() # not sure... @@ -46,5 +44,5 @@ def endomondo_cardio() -> pd.DataFrame: return df -def dataframe(): +def dataframe() -> DataFrameT: return endomondo_cardio() diff --git a/my/body/exercise/cross_trainer.py b/my/body/exercise/cross_trainer.py index 3457412..3fe2112 100644 --- a/my/body/exercise/cross_trainer.py +++ b/my/body/exercise/cross_trainer.py @@ -8,7 +8,7 @@ For now it's worth keeping it here as an example and perhaps utility functions m from datetime import datetime, timedelta from typing import Optional -from ...core.pandas import check_dataframe as cdf +from ...core.pandas import DataFrameT, check_dataframe as cdf from my.config import exercise as config @@ -59,7 +59,7 @@ def cross_trainer_data(): for k, v in row.items(): # todo have something smarter... e.g. allow pandas to infer the type?? mapper = mappers.get(k, maybe(float)) - d[k] = mapper(v) + d[k] = mapper(v) # type: ignore[operator] yield d except Exception as e: # todo add parsing context @@ -70,11 +70,11 @@ def cross_trainer_data(): @cdf -def cross_trainer_manual_dataframe(): +def cross_trainer_manual_dataframe() -> DataFrameT: ''' Only manual org-mode entries ''' - import pandas as pd + import pandas as pd # type: ignore[import] df = pd.DataFrame(cross_trainer_data()) return df @@ -83,11 +83,11 @@ _DELTA = timedelta(hours=10) # todo check error handling by introducing typos (e.g. especially dates) in org-mode @cdf -def dataframe(): +def dataframe() -> DataFrameT: ''' Attaches manually logged data (which Endomondo can't capture) and attaches it to Endomondo ''' - import pandas as pd + import pandas as pd # type: ignore[import] from ...endomondo import dataframe as EDF edf = EDF() @@ -99,7 +99,7 @@ def dataframe(): # now for each manual entry, find a 'close enough' endomondo entry # ideally it's a 1-1 (or 0-1) relationship, but there might be errors rows = [] - idxs = [] + idxs = [] # type: ignore[var-annotated] NO_ENDOMONDO = 'no endomondo matches' for i, row in mdf.iterrows(): rd = row.to_dict() @@ -159,12 +159,12 @@ def dataframe(): # TODO wtf?? where is speed coming from?? -def stats(): - from ...core import stat +from ...core import stat, Stats +def stats() -> Stats: return stat(cross_trainer_data) -def compare_manual(): +def compare_manual() -> None: df = dataframe() df = df.set_index('start_time') diff --git a/tox.ini b/tox.ini index ab4f0a2..9df7a47 100644 --- a/tox.ini +++ b/tox.ini @@ -37,15 +37,20 @@ commands = pip install git+https://github.com/karlicoss/pockexport pip install git+https://github.com/karlicoss/rexport pip install git+https://github.com/karlicoss/endoexport + + pip install git+https://github.com/karlicoss/porg + # todo I guess use a script... - python3 -m mypy -p my.github.ghexport \ - -p my.hypothesis \ - -p my.instapaper \ - -p my.pocket \ - -p my.reddit \ - -p my.endomondo \ - --txt-report .mypy-coverage \ - --html-report .mypy-coverage \ + python3 -m mypy -p my.github.ghexport \ + -p my.hypothesis \ + -p my.instapaper \ + -p my.pocket \ + -p my.reddit \ + -p my.endomondo \ + -p my.body.exercise.cardio \ + -p my.body.exercise.cross_trainer \ + --txt-report .mypy-coverage \ + --html-report .mypy-coverage \ {posargs} # txt report is a bit more convenient to view on CI