mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-09 02:42:36 +00:00
* remove python 3.9 support and code * black * more cli lint * more linting * more lint * fix for tests * docstring grammar police * add lock to to build function to avoid async import race conditions * grammar police * lots more linting * relock
82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
"""FRED Treasury Constant Maturity Model."""
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
from typing import Any
|
|
|
|
from openbb_core.provider.abstract.fetcher import Fetcher
|
|
from openbb_core.provider.standard_models.tmc import (
|
|
TreasuryConstantMaturityData,
|
|
TreasuryConstantMaturityQueryParams,
|
|
)
|
|
from pydantic import field_validator
|
|
|
|
TMC_PARAMETER_TO_FRED_ID = {
|
|
"3m": "T10Y3M",
|
|
"2y": "T10Y2Y",
|
|
}
|
|
|
|
|
|
class FREDTreasuryConstantMaturityQueryParams(TreasuryConstantMaturityQueryParams):
|
|
"""FRED Treasury Constant Maturity Query."""
|
|
|
|
|
|
class FREDTreasuryConstantMaturityData(TreasuryConstantMaturityData):
|
|
"""FRED Treasury Constant Maturity Data."""
|
|
|
|
__alias_dict__ = {"rate": "value"}
|
|
|
|
@field_validator("rate", mode="before", check_fields=False)
|
|
@classmethod
|
|
def value_validate(cls, v):
|
|
"""Validate rate."""
|
|
try:
|
|
return float(v)
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
class FREDTreasuryConstantMaturityFetcher(
|
|
Fetcher[
|
|
FREDTreasuryConstantMaturityQueryParams,
|
|
list[FREDTreasuryConstantMaturityData],
|
|
]
|
|
):
|
|
"""Transform the query, extract and transform the data from the FRED endpoints."""
|
|
|
|
@staticmethod
|
|
def transform_query(
|
|
params: dict[str, Any],
|
|
) -> FREDTreasuryConstantMaturityQueryParams:
|
|
"""Transform query."""
|
|
return FREDTreasuryConstantMaturityQueryParams(**params)
|
|
|
|
@staticmethod
|
|
def extract_data(
|
|
query: FREDTreasuryConstantMaturityQueryParams,
|
|
credentials: dict[str, str] | None,
|
|
**kwargs: Any
|
|
) -> list:
|
|
"""Extract data."""
|
|
# pylint: disable=import-outside-toplevel
|
|
from openbb_fred.utils.fred_base import Fred
|
|
|
|
key = credentials.get("fred_api_key") if credentials else ""
|
|
fred = Fred(key)
|
|
|
|
data = fred.get_series(
|
|
series_id=TMC_PARAMETER_TO_FRED_ID[query.maturity], # type: ignore
|
|
start_date=query.start_date,
|
|
end_date=query.end_date,
|
|
**kwargs,
|
|
)
|
|
|
|
return data
|
|
|
|
@staticmethod
|
|
def transform_data(
|
|
query: FREDTreasuryConstantMaturityQueryParams, data: list, **kwargs: Any
|
|
) -> list[FREDTreasuryConstantMaturityData]:
|
|
"""Transform data."""
|
|
return [FREDTreasuryConstantMaturityData.model_validate(d) for d in data]
|