Skip to content

Instantly share code, notes, and snippets.

@osantana
Last active May 8, 2024 13:36
Show Gist options
  • Save osantana/2a3485032cbc6523c1958f40ba54c8f4 to your computer and use it in GitHub Desktop.
Save osantana/2a3485032cbc6523c1958f40ba54c8f4 to your computer and use it in GitHub Desktop.
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