dahlia/iso4217

[Question] How to get Currency type to be used for pandas schema validation?

miriam-z opened this issue · 1 comments

Is there such attribute? Thank you

Since Currency is of type enum 'Currency', so if I needed to use it for validating:

Id Currency

  1. USD passed

Id Currency

  1. XYZ failed

Although I have no experience in pandas, determining if a given three-letter string is a valid ISO 4217 currency ticker is quite straightforward:

>>> from iso4217 import Currency
>>> Currency('USD')
<Currency.usd: 'USD'>
>>> Currency('XYZ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
ValueError: 'XYZ' is not a valid Currency

Here's some example:

from iso4217 import Currency

inputs = ['ABC', 'DEF', 'EUR', 'KRW', 'USD', 'XYZ']

for input in inputs:
    try:
        currency = Currency(input.upper())
    except ValueError:
        print(f'{input}: invalid')
    else:
        print(f'{input}: valid ({currency.currency_name})')

The above code prints:

ABC: invalid
DEF: invalid
EUR: valid (Euro)
KRW: valid (Won)
USD: valid (US Dollar)
XYZ: invalid