Skip to content

Commit

Permalink
Merge pull request #17 from kalaspuff/feature/currency-to-money
Browse files Browse the repository at this point in the history
Added money() method to currency objects
  • Loading branch information
kalaspuff authored Nov 28, 2019
2 parents 0c7516e + b3274e8 commit 7d0ac29
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
48 changes: 48 additions & 0 deletions stockholm/currency.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
from typing import Any, Dict, List, Optional, Set, Tuple, Type, Union, cast

from decimal import Decimal


class MetaCurrency(type):
ticker: str
Expand Down Expand Up @@ -31,6 +33,27 @@ def __new__(cls, name: str, bases: Tuple[type, ...], attributedict: Dict) -> "Me
result: Type[BaseCurrency] = type.__new__(cls, name, bases, attributedict)
return result

def money(
self,
amount: Optional[Union["Money", Decimal, int, float, str, object]] = None,
from_sub_units: Optional[bool] = None,
units: Optional[int] = None,
nanos: Optional[int] = None,
currency_code: Optional[str] = None,
**kwargs: Any,
) -> "Money":
kwargs.pop("currency", None)

return Money(
amount,
currency=cast(BaseCurrency, self),
from_sub_units=from_sub_units,
units=units,
nanos=nanos,
currency_code=currency_code,
**kwargs,
)

def __setattr__(self, *args: Any) -> None:
raise AttributeError("Attributes of currencies cannot be changed")

Expand Down Expand Up @@ -127,6 +150,28 @@ def __init__(
object.__setattr__(self, "_meta", False)
object.__setattr__(self, "as_string", lambda: str(self))
object.__setattr__(self, "as_str", lambda: str(self))
object.__setattr__(self, "money", lambda *args, **kwargs: self._money(*args, **kwargs))

def _money(
self,
amount: Optional[Union["Money", Decimal, int, float, str, object]] = None,
from_sub_units: Optional[bool] = None,
units: Optional[int] = None,
nanos: Optional[int] = None,
currency_code: Optional[str] = None,
**kwargs: Any,
) -> "Money":
kwargs.pop("currency", None)

return Money(
amount,
currency=self,
from_sub_units=from_sub_units,
units=units,
nanos=nanos,
currency_code=currency_code,
**kwargs,
)

def __setattr__(self, *args: Any) -> None:
raise AttributeError("Attributes of currencies cannot be changed")
Expand Down Expand Up @@ -1476,3 +1521,6 @@ def _load_currencies(currencies: List[str]) -> None:


Currency._load_currencies(all_currencies())


from stockholm.money import Money # noqa
11 changes: 11 additions & 0 deletions tests/test_currency.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decimal import Decimal
import pytest

import stockholm
from stockholm import BaseCurrency, Currency, Money
from stockholm.currency import Bitcoin, CLF, DOGE, DogeCoin, Ethereum, IQD, JPY, USD, XBT, get_currency

Expand Down Expand Up @@ -292,6 +293,16 @@ def test_currency_types() -> None:
assert str(Money.from_sub_units(Money("133742", Currency.SEK, from_sub_units=True).to_sub_units())) == "1337.42 SEK"


def test_currency_to_money():
currency = Currency("NEW")

assert str(currency.money(100)) == "100.00 NEW"
assert str(Currency.SEK.money(100)) == "100.00 SEK"
assert str(Currency.JPY.money(100)) == "100 JPY"
assert str(JPY.money(100)) == "100 JPY"
assert str(stockholm.currency.Bitcoin.money("1.523")) == "1.523 BTC"


def test_currency_constructors():
currency = Currency("NEW")
assert currency
Expand Down

0 comments on commit 7d0ac29

Please sign in to comment.