48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
|
|
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())
|