Python mode for managing tank number validation

advertisements

I have a case where I want to validate an entered vat number. Each European country has a pre-defined format and I can create a regex pattern to validate an entered number.

My question is what would be the most "pythonic" way to handle this for 20 countries. Should I create a dictionary with each of the country and its pattern

Example

VAT_PATTERNS = {
   'ES': '([A-Z0-9][0-9]{7}[A-Z0-9]$)',
   'DE': '([0-9]{9}$)',
}

or is there a more pythonic way?

There are cases where several countries have the same pattern. Some countries might also have more complex patterns.

I could create a VAT base class from where each country's class inherits and handle it that way but that seems a bit out of place.


Part of the "Zen of Python" (type import this into an interpreter!) is that "Explicit is better than implicit" and "Simple is better than complex".

What you've described looks both simple and explicit, and so I would consider it pretty pythonic.