From 0bace157b1f133d98ffdfe6b58030b2979c54fa1 Mon Sep 17 00:00:00 2001 From: Chase Sterling Date: Mon, 7 Feb 2022 12:29:53 -0500 Subject: [PATCH] Add new 'power_adjust' config option. Warn about old X_pace_adjust options not working if still in config. --- config.yaml.example | 6 +----- pyproject.toml | 2 +- trainaspower/models.py | 37 +++++++++++++++++++++++++++++-------- trainaspower/trainasone.py | 2 ++ 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index 541b16f..de7064d 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -4,9 +4,5 @@ trainasone_email: my@email.com trainasone_password: my_password finalsurge_email: my@email.com finalsurge_password: my_password -recovery_pace_adjust: [0, 0] -very_easy_pace_adjust: [0, 0] -easy_pace_adjust: [0, 0] -fast_pace_adjust: [0, 0] -extreme_pace_adjust: [0, 0] +power_adjust: [0, 0] number_of_workouts: 1 diff --git a/pyproject.toml b/pyproject.toml index 98e596d..500a398 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "trainaspower" -version = "0.7.2" +version = "0.7.3" description = "Convert TrainAsOne plans to power and upload to Final Surge for use with Stryd pod." authors = ["Chase Sterling "] license = "MIT" diff --git a/trainaspower/models.py b/trainaspower/models.py index ed9b3d3..5ed47f7 100644 --- a/trainaspower/models.py +++ b/trainaspower/models.py @@ -1,10 +1,11 @@ +import builtins import datetime from dataclasses import dataclass -from typing import List, NamedTuple, Union, Tuple +from typing import List, NamedTuple, Union, Tuple, Any +from loguru import logger from pint import UnitRegistry, Quantity -from pydantic import BaseModel - +from pydantic import BaseModel, Field, validator ureg = UnitRegistry() mile = ureg.mile @@ -21,12 +22,24 @@ class Config(BaseModel): trainasone_password: str finalsurge_email: str finalsurge_password: str - recovery_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0) - very_easy_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0) - easy_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0) - fast_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0) - extreme_pace_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0) + power_adjust: Tuple[Union[float, int], Union[float, int]] = (0, 0) number_of_workouts: int = 1 + # Old config values + recovery_pace_adjust: Any = Field(removed='Field `power_adjust` has been added instead') + very_easy_pace_adjust: Any = Field(removed='Field `power_adjust` has been added instead') + easy_pace_adjust: Any = Field(removed='Field `power_adjust` has been added instead') + fast_pace_adjust: Any = Field(removed='Field `power_adjust` has been added instead') + extreme_pace_adjust: Any = Field(removed='Field `power_adjust` has been added instead.') + + @validator('*') + def warn_removed(cls, v, field): + removed = field.field_info.extra.get('removed') + if removed: + message = f'Config field `{field.name}` was removed.' + if isinstance(removed, str): + message += f' {removed}' + logger.warning(message) + return v class Config: extra = 'forbid' @@ -41,6 +54,14 @@ def __init__(self, min_val, max_val): self.min = max(0, min_val) self.max = max_val + def __add__(self, other): + new = PowerRange(self.min, self.max) + if not len(other) == 2: + raise ValueError(f'Cannot add {repr(other)} to PowerRange') + new.min = builtins.max(0, self.min + other[0]) + new.max += other[1] + return new + class PaceRange(NamedTuple): min: Quantity diff --git a/trainaspower/trainasone.py b/trainaspower/trainasone.py index 66ab860..c6e73d6 100644 --- a/trainaspower/trainasone.py +++ b/trainaspower/trainasone.py @@ -183,6 +183,8 @@ def convert_steps(steps, config: models.Config, perceived_effort: bool) -> Gener else: out_step.power_range = convert_pace_range_to_power( out_step.pace_range) + # Add adjustment from config + out_step.power_range += config.power_adjust yield out_step