Country specific validators for use with FormEncode.
Contents
Note
To use CountryValidator and LanguageValidator, install either pycountry or TurboGears Version 1.x.
Abstraction of common postal code formats, such as 55555, 55-555 etc. With constant amount of digits. By providing a single digit as partition you can obtain a trivial ‘x digits’ postal code validator.
>>> german = DelimitedDigitsPostalCode(5)
>>> german.to_python('55555')
'55555'
>>> german.to_python('5555')
Traceback (most recent call last):
...
Invalid: Please enter a zip code (5 digits)
>>> polish = DelimitedDigitsPostalCode([2, 3], '-')
>>> polish.to_python('55555')
'55-555'
>>> polish.to_python('55-555')
'55-555'
>>> polish.to_python('5555')
Traceback (most recent call last):
...
Invalid: Please enter a zip code (nn-nnn)
>>> nicaragua = DelimitedDigitsPostalCode([3, 3, 1], '-')
>>> nicaragua.to_python('5554443')
'555-444-3'
>>> nicaragua.to_python('555-4443')
'555-444-3'
>>> nicaragua.to_python('5555')
Traceback (most recent call last):
...
Invalid: Please enter a zip code (nnn-nnn-n)
Messages
US Postal codes (aka Zip Codes).
>>> uspc = USPostalCode()
>>> uspc.to_python('55555')
'55555'
>>> uspc.to_python('55555-5555')
'55555-5555'
>>> uspc.to_python('5555')
Traceback (most recent call last):
...
Invalid: Please enter a zip code (5 digits)
Argentinian Postal codes.
>>> ArgentinianPostalCode.to_python('C1070AAM')
'C1070AAM'
>>> ArgentinianPostalCode.to_python('c 1070 aam')
'C1070AAM'
>>> ArgentinianPostalCode.to_python('5555')
Traceback (most recent call last):
...
Invalid: Please enter a zip code (LnnnnLLL)
Messages
Canadian Postal codes.
>>> CanadianPostalCode.to_python('V3H 1Z7')
'V3H 1Z7'
>>> CanadianPostalCode.to_python('v3h1z7')
'V3H 1Z7'
>>> CanadianPostalCode.to_python('5555')
Traceback (most recent call last):
...
Invalid: Please enter a zip code (LnL nLn)
Messages
UK Postal codes. Please see BS 7666.
>>> UKPostalCode.to_python('BFPO 3')
'BFPO 3'
>>> UKPostalCode.to_python('LE11 3GR')
'LE11 3GR'
>>> UKPostalCode.to_python('l1a 3gr')
'L1A 3GR'
>>> UKPostalCode.to_python('5555')
Traceback (most recent call last):
...
Invalid: Please enter a valid postal code (for format see BS 7666)
Messages
Will convert a country’s name into its ISO-3166 abbreviation for unified storage in databases etc. and return a localized country name in the reverse step.
@See http://www.iso.org/iso/country_codes/iso_3166_code_lists.htm
>>> CountryValidator.to_python('Germany')
u'DE'
>>> CountryValidator.to_python('Finland')
u'FI'
>>> CountryValidator.to_python('UNITED STATES')
u'US'
>>> CountryValidator.to_python('Krakovia')
Traceback (most recent call last):
...
Invalid: That country is not listed in ISO 3166
>>> CountryValidator.from_python('DE')
u'Germany'
>>> CountryValidator.from_python('FI')
u'Finland'
Messages
Makes sure the postal code is in the country’s format by chosing postal code validator by provided country code. Does convert it into the preferred format, too.
>>> fs = PostalCodeInCountryFormat('country', 'zip')
>>> sorted(fs.to_python(dict(country='DE', zip='30167')).items())
[('country', 'DE'), ('zip', '30167')]
>>> fs.to_python(dict(country='DE', zip='3008'))
Traceback (most recent call last):
...
Invalid: Given postal code does not match the country's format.
>>> sorted(fs.to_python(dict(country='PL', zip='34343')).items())
[('country', 'PL'), ('zip', '34-343')]
>>> fs = PostalCodeInCountryFormat('staat', 'plz')
>>> sorted(fs.to_python(dict(staat='GB', plz='l1a 3gr')).items())
[('plz', 'L1A 3GR'), ('staat', 'GB')]
Messages
Valid state or province code (two-letter).
Well, for now I don’t know the province codes, but it does state codes. Give your own states list to validate other state-like codes; give extra_states to add values without losing the current state values.
>>> s = USStateProvince('XX')
>>> s.to_python('IL')
'IL'
>>> s.to_python('XX')
'XX'
>>> s.to_python('xx')
'XX'
>>> s.to_python('YY')
Traceback (most recent call last):
...
Invalid: That is not a valid state code
Messages
Validates, and converts to ###-###-####, optionally with extension (as ext.##...). Only support US phone numbers. See InternationalPhoneNumber for support for that kind of phone number.
>>> p = USPhoneNumber()
>>> p.to_python('333-3333')
Traceback (most recent call last):
...
Invalid: Please enter a number, with area code, in the form ###-###-####, optionally with "ext.####"
>>> p.to_python('555-555-5555')
'555-555-5555'
>>> p.to_python('1-393-555-3939')
'1-393-555-3939'
>>> p.to_python('321.555.4949')
'321.555.4949'
>>> p.to_python('3335550000')
'3335550000'
Messages
Validates, and converts phone numbers to +##-###-#######. Adapted from RFC 3966
>>> c = InternationalPhoneNumber(default_cc=lambda: 49)
>>> c.to_python('0555/8114100')
'+49-555-8114100'
>>> p = InternationalPhoneNumber(default_cc=49)
>>> p.to_python('333-3333')
Traceback (most recent call last):
...
Invalid: Please enter a number, with area code, in the form +##-###-#######.
>>> p.to_python('0555/4860-300')
'+49-555-4860-300'
>>> p.to_python('0555-49924-51')
'+49-555-49924-51'
>>> p.to_python('0555 / 8114100')
'+49-555-8114100'
>>> p.to_python('0555/8114100')
'+49-555-8114100'
>>> p.to_python('0555 8114100')
'+49-555-8114100'
>>> p.to_python(' +49 (0)555 350 60 0')
'+49-555-35060-0'
>>> p.to_python('+49 555 350600')
'+49-555-350600'
>>> p.to_python('0049/ 555/ 871 82 96')
'+49-555-87182-96'
>>> p.to_python('0555-2 50-30')
'+49-555-250-30'
>>> p.to_python('0555 43-1200')
'+49-555-43-1200'
>>> p.to_python('(05 55)4 94 33 47')
'+49-555-49433-47'
>>> p.to_python('(00 48-555)2 31 72 41')
'+48-555-23172-41'
>>> p.to_python('+973-555431')
'+973-555431'
>>> p.to_python('1-393-555-3939')
'+1-393-555-3939'
>>> p.to_python('+43 (1) 55528/0')
'+43-1-55528-0'
>>> p.to_python('+43 5555 429 62-0')
'+43-5555-42962-0'
>>> p.to_python('00 218 55 33 50 317 321')
'+218-55-3350317-321'
>>> p.to_python('+218 (0)55-3636639/38')
'+218-55-3636639-38'
>>> p.to_python('032 555555 367')
'+49-32-555555-367'
>>> p.to_python('(+86) 555 3876693')
'+86-555-3876693'
Messages
Converts a given language into its ISO 639 alpha 2 code, if there is any. Returns the language’s full name in the reverse.
Warning: ISO 639 neither differentiates between languages such as Cantonese and Mandarin nor does it contain all spoken languages. E.g., Lechitic languages are missing. Warning: ISO 639 is a smaller subset of ISO 639-2
>>> l = LanguageValidator()
>>> l.to_python('German')
u'de'
>>> l.to_python('Chinese')
u'zh'
>>> l.to_python('Klingonian')
Traceback (most recent call last):
...
Invalid: That language is not listed in ISO 639
>>> l.from_python('de')
u'German'
>>> l.from_python('zh')
u'Chinese'
Messages