Last active
May 8, 2024 13:36
-
-
Save osantana/2a3485032cbc6523c1958f40ba54c8f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Currency: | |
_currencies = {} | |
def __init__(self, code, symbol, name): | |
self.code = code.upper() | |
self.symbol = symbol | |
self.name = name | |
Currency.register(self) | |
@classmethod | |
def register(cls, currency): | |
cls._currencies[currency.code] = currency | |
@classmethod | |
def get(cls, currency): | |
if isinstance(currency, cls): | |
return currency | |
return cls._currencies[currency.upper()] | |
USD = Currency('USD', '$', 'Dollar') | |
ZERO = Decimal('0.00') | |
class Money: | |
def __init__(self, amount=ZERO, currency=USD): | |
if not isinstance(amount, Decimal): | |
amount = Decimal(str(amount)) | |
self.amount = amount | |
self.currency = Currency.get(currency) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment