more methods and tests
This commit is contained in:
parent
aec65e5364
commit
d382c332e3
2 changed files with 145 additions and 12 deletions
|
|
@ -3,7 +3,7 @@ from decimal import Decimal
|
|||
from numbers import Number
|
||||
from typing import Iterable, Set, Union
|
||||
|
||||
from money.currency import Currency
|
||||
from money.currency import Currency, CurrencyHelper
|
||||
from money.money import Money
|
||||
|
||||
|
||||
|
|
@ -13,7 +13,7 @@ class ExchangeRate:
|
|||
self,
|
||||
from_currency: Currency,
|
||||
to_currency: Currency,
|
||||
rate: Union[Money, Number],
|
||||
rate: Union[Money, Number, str],
|
||||
rate_date: datetime.date,
|
||||
) -> None:
|
||||
self.from_currency = from_currency
|
||||
|
|
@ -66,26 +66,64 @@ class ExchangeRates:
|
|||
self._rate_index[new_rate.descriptor] = new_rate
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._rate_index.values())
|
||||
return iter(list(self._rate_index.values()))
|
||||
|
||||
def __len__(self):
|
||||
return len(self._rate_index)
|
||||
|
||||
def __contains__(self, rate) -> bool:
|
||||
if not isinstance(rate, ExchangeRate):
|
||||
raise TypeError("ExchangeRates can only hold Rates.")
|
||||
|
||||
def add_equal_rates(rates: ExchangeRates) -> ExchangeRates:
|
||||
if rate.descriptor in self._rate_index:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def is_rate_present(self, rate: ExchangeRate) -> bool:
|
||||
if rate.descriptor in self._rate_index:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def __getitem__(self, rate_descriptor) -> ExchangeRate:
|
||||
return self._rate_index[rate_descriptor]
|
||||
|
||||
|
||||
def add_equal_rates(rates: ExchangeRates, overwrite: bool = False) -> ExchangeRates:
|
||||
|
||||
present_currencies = rates.present_currencies
|
||||
present_dates = rates.present_dates
|
||||
|
||||
for date in present_dates:
|
||||
for currency in present_currencies:
|
||||
rates.add_rate(
|
||||
ExchangeRate(
|
||||
from_currency=currency,
|
||||
to_currency=currency,
|
||||
rate=Money(1, currency),
|
||||
rate_date=date,
|
||||
)
|
||||
new_rate = ExchangeRate(
|
||||
from_currency=currency,
|
||||
to_currency=currency,
|
||||
rate=Money(1, currency),
|
||||
rate_date=date,
|
||||
)
|
||||
if new_rate in rates and not overwrite:
|
||||
continue
|
||||
rates.add_rate(new_rate)
|
||||
|
||||
return rates
|
||||
|
||||
|
||||
def add_inverse_rates(rates: ExchangeRates) -> ExchangeRates:
|
||||
|
||||
# Hey, I haven't thought properly what happens here if the inverse rate is
|
||||
# *already* present in the rates set. It's probably going to be fucky. I
|
||||
# would advise only running this against sets where you don't have inverse
|
||||
# rates already present.
|
||||
|
||||
for rate in rates:
|
||||
inverse_rate = ExchangeRate(
|
||||
from_currency=rate.to_currency,
|
||||
to_currency=rate.from_currency,
|
||||
rate_date=rate.rate_date,
|
||||
rate=f"{1 / rate.amount:.{CurrencyHelper.decimal_precision_for_currency(rate.from_currency)}f}",
|
||||
)
|
||||
rates.add_rate(inverse_rate)
|
||||
|
||||
return rates
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue