dahlia/iso4217

Static Method to Validate Currency code

vicmattos opened this issue · 1 comments

Hi,

I missed the functionality of calling a method from the package that would validate if the currency code (e.g. "USD") is valid or not.

I managed to make it work for my necessity, but I wanted to give this improvement proposal.

An example would be: https://github.com/xheuz/iso4217

Best Regards,

Currency() constructor raises ValueError on invalid (i.e., not listed) code:

>>> from iso4217 import Currency
>>> Currency('USD')  # valid
<Currency.usd: 'USD'>
>>> Currency('INV')  # invalid
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../python3.9/enum.py", line 384, in __call__
    return cls.__new__(cls, value)
  File "/.../python3.9/enum.py", line 702, in __new__
    raise ve_exc
ValueError: 'INV' is not a valid Currency

If you find using try-except to validate codes bother, you could __members__ attribute instead (which is provided by Python's standard enum module):

>>> 'usd' in Currency.__members__  # valid
True
>>> 'inv' in Currency.__members__  # invalid
False