fetching and writing rates
This commit is contained in:
parent
7012cbec97
commit
41eae45f68
5 changed files with 140 additions and 43 deletions
47
xexe/exchange_rates.py
Normal file
47
xexe/exchange_rates.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import datetime
|
||||
from typing import Iterable, Union
|
||||
|
||||
from money.currency import Currency
|
||||
from money.money import Money
|
||||
|
||||
|
||||
class ExchangeRate:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
from_currency: Currency,
|
||||
to_currency: Currency,
|
||||
rate: Money,
|
||||
rate_date: datetime.date,
|
||||
) -> None:
|
||||
self.from_currency = from_currency
|
||||
self.to_currency = to_currency
|
||||
self.rate = rate
|
||||
self.rate_date = rate_date
|
||||
|
||||
@property
|
||||
def descriptor(self) -> str:
|
||||
return (
|
||||
str(self.from_currency.value)
|
||||
+ str(self.to_currency.value)
|
||||
+ str(self.rate_date.strformat("%Y-%m-%d"))
|
||||
)
|
||||
|
||||
|
||||
class ExchangeRates:
|
||||
|
||||
def __init__(self, rates: Union[Iterable[ExchangeRate], None] = None):
|
||||
|
||||
self._rate_index = {}
|
||||
|
||||
if rates is not None:
|
||||
for rate in rates:
|
||||
if not isinstance(rate, ExchangeRate):
|
||||
raise TypeError("ExchangeRates can only hold Rates.")
|
||||
self._rate_index[rate.descriptor] = rate
|
||||
|
||||
def add_rate(self, new_rate: ExchangeRate) -> None:
|
||||
self._rate_index[new_rate.descriptor] = new_rate
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._rate_index.values())
|
||||
Loading…
Add table
Add a link
Reference in a new issue