Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single Currency #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions src-py2/money/money.py
Expand Up @@ -11,7 +11,7 @@
InvalidOperandType)


__all__ = ['Money', 'XMoney']
__all__ = ['Money', 'XMoney', 'Single']

BABEL_AVAILABLE = False
BABEL_VERSION = None
Expand Down Expand Up @@ -317,8 +317,24 @@ def __divmod__(self, other):
return super(XMoney, self).__divmod__(other)


def Single(single):
"""Money subclass bound to a single fixed currency.

Example:

>>> from money import Single
>>>
>>> class BTC(Single('BTC')):
... pass
...
>>> BTC('0')
BTC 0

"""
class SingleCurrency(Money):
def __init__(self, amount=0, currency=single):
if currency != single:
raise ValueError("invalid currency value: '{}', must be '{}'".format(currency, single))
super().__init__(amount, currency)


return SingleCurrency
20 changes: 18 additions & 2 deletions src/money/money.py
Expand Up @@ -10,7 +10,7 @@
InvalidOperandType)


__all__ = ['Money', 'XMoney']
__all__ = ['Money', 'XMoney', 'Single']

BABEL_AVAILABLE = False
BABEL_VERSION = None
Expand Down Expand Up @@ -307,8 +307,24 @@ def __divmod__(self, other):
return super().__divmod__(other)


def Single(single):
"""Money subclass bound to a single fixed currency.

Example:

>>> from money import Single
>>>
>>> class BTC(Single('BTC')):
... pass
...
>>> BTC('0')
BTC 0

"""
class SingleCurrency(Money):
def __init__(self, amount=0, currency=single):
if currency != single:
raise ValueError("invalid currency value: '{}', must be '{}'".format(currency, single))
super().__init__(amount, currency)


return SingleCurrency