| 1 |
Metadata-Version: 1.1 |
| 2 |
Name: google-i18n-address |
| 3 |
Version: 2.4.0 |
| 4 |
Summary: Address validation helpers for Google's i18n address database |
| 5 |
Home-page: https://github.com/mirumee/google-i18n-address |
| 6 |
Author: Mirumee Software |
| 7 |
Author-email: [email protected] |
| 8 |
License: BSD |
| 9 |
Description: Google i18n address |
| 10 |
=========================================================================================== |
| 11 |
|
| 12 |
|codecov.io| |Circle CI| |PyPi downloads| |requires.io| |PyPi version| |PyPi pythons| |GitHub| |
| 13 |
|
| 14 |
This package contains a copy of `Google's i18n |
| 15 |
address <https://chromium-i18n.appspot.com/ssl-address>`_ metadata repository |
| 16 |
that contains great data but comes with no uptime guarantees. |
| 17 |
|
| 18 |
Contents of this package will allow you to programatically build address |
| 19 |
forms that adhere to rules of a particular region or country, validate |
| 20 |
local addresses and format them to produce a valid address label for |
| 21 |
delivery. |
| 22 |
|
| 23 |
The package also contains a Python interface for address validation. |
| 24 |
|
| 25 |
Addresses validation |
| 26 |
-------------------- |
| 27 |
|
| 28 |
The ``normalize_address`` function checks the address and either returns its |
| 29 |
canonical form (suitable for storage and use in addressing envelopes) or |
| 30 |
raises an ``InvalidAddress`` exception that contains a list of errors. |
| 31 |
|
| 32 |
|
| 33 |
Address fields |
| 34 |
~~~~~~~~~~~~~~ |
| 35 |
|
| 36 |
Here is the list of recognized fields: |
| 37 |
|
| 38 |
* ``country_code`` is a two-letter ISO 3166-1 country code |
| 39 |
* ``country_area`` is a designation of a region, province or state, |
| 40 |
recognized values include official names, designated abbreviations, |
| 41 |
official translations and latin transliterations |
| 42 |
* ``city`` is a city or town name, recognized values include official names, |
| 43 |
official translations and latin transliterations |
| 44 |
* ``city_area`` is a sublocality like a district, recognized values include |
| 45 |
official names, official translations and latin transliterations |
| 46 |
* ``street_address`` is the (possibly multiline) street address |
| 47 |
* ``postal_code`` is a postal code or zip code |
| 48 |
* ``sorting_code`` is a sorting code |
| 49 |
* ``name`` is a person's name |
| 50 |
* ``company_name`` is a name of a company or organization |
| 51 |
|
| 52 |
|
| 53 |
Errors |
| 54 |
~~~~~~ |
| 55 |
|
| 56 |
Address validation with only country code: |
| 57 |
|
| 58 |
.. code:: python |
| 59 |
|
| 60 |
>>> from i18naddress import InvalidAddress, normalize_address |
| 61 |
>>> try: |
| 62 |
... address = normalize_address({'country_code': 'US'}) |
| 63 |
... except InvalidAddress as e: |
| 64 |
... print(e.errors) |
| 65 |
... |
| 66 |
{'city': 'required', |
| 67 |
'country_area': 'required', |
| 68 |
'postal_code': 'required', |
| 69 |
'street_address': 'required'} |
| 70 |
|
| 71 |
With correct address: |
| 72 |
|
| 73 |
.. code:: python |
| 74 |
|
| 75 |
>>> from i18naddress import normalize_address |
| 76 |
>>> address = normalize_address({ |
| 77 |
'country_code': 'US', |
| 78 |
'country_area': 'California', |
| 79 |
'city': 'Mountain View', |
| 80 |
'postal_code': '94043', |
| 81 |
'street_address': '1600 Amphitheatre Pkwy'}) |
| 82 |
>>> print(address) |
| 83 |
{'city': 'MOUNTAIN VIEW', |
| 84 |
'city_area': '', |
| 85 |
'country_area': 'CA', |
| 86 |
'country_code': 'US', |
| 87 |
'postal_code': '94043', |
| 88 |
'sorting_code': '', |
| 89 |
'street_address': '1600 Amphitheatre Pkwy'} |
| 90 |
|
| 91 |
Postal code/zip code validation example: |
| 92 |
|
| 93 |
.. code:: python |
| 94 |
|
| 95 |
>>> from i18naddress import InvalidAddress, normalize_address |
| 96 |
>>> try: |
| 97 |
... address = normalize_address({ |
| 98 |
... 'country_code': 'US', |
| 99 |
... 'country_area': 'California', |
| 100 |
... 'city': 'Mountain View', |
| 101 |
... 'postal_code': '74043', |
| 102 |
... 'street_address': '1600 Amphitheatre Pkwy'}) |
| 103 |
... except InvalidAddress as e: |
| 104 |
... print(e.errors) |
| 105 |
... |
| 106 |
{'postal_code': 'invalid'} |
| 107 |
|
| 108 |
|
| 109 |
Address latinization |
| 110 |
~~~~~~~~~~~~~~~~~~~~ |
| 111 |
|
| 112 |
In some cases it may be useful to display foreign addresses in a more |
| 113 |
accessible format. You can use the ``latinize_address`` function to obtain |
| 114 |
a more verbose, latinized version of an address. |
| 115 |
|
| 116 |
This version is suitable for display and useful for full text search indexing |
| 117 |
but the normalized form is what should be stored in the database and used when |
| 118 |
printing address labels. |
| 119 |
|
| 120 |
.. code:: python |
| 121 |
|
| 122 |
>>> from i18naddress import latinize_address |
| 123 |
>>> address = { |
| 124 |
... 'country_code': 'CN', |
| 125 |
... 'country_area': '云南省', |
| 126 |
... 'postal_code': '677400', |
| 127 |
... 'city': '临沧市', |
| 128 |
... 'city_area': '凤庆县', |
| 129 |
... 'street_address': '中关村东路1号'} |
| 130 |
>>> latinize_address(address) |
| 131 |
{'country_code': 'CN', |
| 132 |
'country_area': 'Yunnan Sheng', |
| 133 |
'city': 'Lincang Shi', |
| 134 |
'city_area': 'Lincang Shi', |
| 135 |
'sorting_code': '', |
| 136 |
'postal_code': '677400', |
| 137 |
'street_address': '中关村东路1号'} |
| 138 |
|
| 139 |
It will also return expanded names for area types that normally use codes and |
| 140 |
abbreviations such as state names in US: |
| 141 |
|
| 142 |
.. code:: python |
| 143 |
|
| 144 |
>>> from i18naddress import latinize_address |
| 145 |
>>> address = { |
| 146 |
... 'country_code': 'US', |
| 147 |
... 'country_area': 'CA', |
| 148 |
... 'postal_code': '94037', |
| 149 |
... 'city': 'Mountain View', |
| 150 |
... 'street_address': '1600 Charleston Rd.'} |
| 151 |
>>> latinize_address(address) |
| 152 |
{'country_code': 'US', |
| 153 |
'country_area': 'California', |
| 154 |
'city': 'Mountain View', |
| 155 |
'city_area': '', |
| 156 |
'sorting_code': '', |
| 157 |
'postal_code': '94037', |
| 158 |
'street_address': '1600 Charleston Rd.'} |
| 159 |
|
| 160 |
|
| 161 |
Address formatting |
| 162 |
~~~~~~~~~~~~~~~~~~ |
| 163 |
|
| 164 |
You can use the ``format_address`` function to format the address following |
| 165 |
the destination country's post office regulations: |
| 166 |
|
| 167 |
.. code:: python |
| 168 |
|
| 169 |
>>> address = { |
| 170 |
... 'country_code': 'CN', |
| 171 |
... 'country_area': '云南省', |
| 172 |
... 'postal_code': '677400', |
| 173 |
... 'city': '临沧市', |
| 174 |
... 'city_area': '凤庆县', |
| 175 |
... 'street_address': '中关村东路1号'} |
| 176 |
>>>> print(format_address(address)) |
| 177 |
677400 |
| 178 |
云南省临沧市凤庆县 |
| 179 |
中关村东路1号 |
| 180 |
CHINA |
| 181 |
|
| 182 |
You can also ask for a latin-friendly version: |
| 183 |
|
| 184 |
.. code:: python |
| 185 |
|
| 186 |
>>> address = { |
| 187 |
... 'country_code': 'CN', |
| 188 |
... 'country_area': '云南省', |
| 189 |
... 'postal_code': '677400', |
| 190 |
... 'city': '临沧市', |
| 191 |
... 'city_area': '凤庆县', |
| 192 |
... 'street_address': '中关村东路1号'} |
| 193 |
>>> print(format_address(address, latin=True)) |
| 194 |
中关村东路1号 |
| 195 |
凤庆县 |
| 196 |
临沧市 |
| 197 |
云南省, 677400 |
| 198 |
CHINA |
| 199 |
|
| 200 |
|
| 201 |
Validation rules |
| 202 |
~~~~~~~~~~~~~~~~ |
| 203 |
|
| 204 |
You can use the ``get_validation_rules`` function to obtain validation data |
| 205 |
useful for constructing address forms specific for a particular country: |
| 206 |
|
| 207 |
.. code:: python |
| 208 |
|
| 209 |
>>> from i18naddress import get_validation_rules |
| 210 |
>>> get_validation_rules({'country_code': 'US', 'country_area': 'CA'}) |
| 211 |
ValidationRules( |
| 212 |
country_code='US', |
| 213 |
country_name='UNITED STATES', |
| 214 |
address_format='%N%n%O%n%A%n%C, %S %Z', |
| 215 |
address_latin_format='%N%n%O%n%A%n%C, %S %Z', |
| 216 |
allowed_fields={'street_address', 'company_name', 'city', 'name', 'country_area', 'postal_code'}, |
| 217 |
required_fields={'street_address', 'city', 'country_area', 'postal_code'}, |
| 218 |
upper_fields={'city', 'country_area'}, |
| 219 |
country_area_type='state', |
| 220 |
country_area_choices=[('AL', 'Alabama'), ..., ('WY', 'Wyoming')], |
| 221 |
city_type='city', |
| 222 |
city_choices=[], |
| 223 |
city_area_type='suburb', |
| 224 |
city_area_choices=[], |
| 225 |
postal_code_type='zip', |
| 226 |
postal_code_matchers=[re.compile('^(\\d{5})(?:[ \\-](\\d{4}))?$'), re.compile('^9[0-5]|96[01]')], |
| 227 |
postal_code_examples=['90000', '96199'], |
| 228 |
postal_code_prefix='') |
| 229 |
|
| 230 |
All known fields |
| 231 |
---------------- |
| 232 |
|
| 233 |
You can use ``KNOWN_FIELDS`` set, to render optional address fields as hidden |
| 234 |
elements of your form: |
| 235 |
|
| 236 |
.. code:: python |
| 237 |
|
| 238 |
>> from i18naddress import get_validation_rules, KNOWN_FIELDS |
| 239 |
>> rules = get_validation_rules({'country_code': 'US'}) |
| 240 |
>> KNOWN_FIELDS - rules.allowed_fields |
| 241 |
{'city_area', 'sorting_code'} |
| 242 |
|
| 243 |
|
| 244 |
Raw i18n data |
| 245 |
------------- |
| 246 |
|
| 247 |
Raw data is stored in a dict: |
| 248 |
|
| 249 |
.. code:: python |
| 250 |
|
| 251 |
>>> from i18naddress import load_validation_data |
| 252 |
>>> i18n_country_data = load_validation_data() |
| 253 |
>>> i18n_country_data['US'] |
| 254 |
{'fmt': '%N%n%O%n%A%n%C, %S %Z', |
| 255 |
'id': 'data/US', |
| 256 |
'key': 'US', |
| 257 |
'lang': 'en', |
| 258 |
'languages': 'en', |
| 259 |
'name': 'UNITED STATES', |
| 260 |
'posturl': 'https://tools.usps.com/go/ZipLookupAction!input.action', |
| 261 |
'require': 'ACSZ', |
| 262 |
'state_name_type': 'state', |
| 263 |
'sub_keys': 'AL~AK~AS~AZ~AR~AA~AE~AP~CA~CO~CT~DE~DC~FL~GA~GU~HI~ID~IL~IN~IA~KS~KY~LA~ME~MH~MD~MA~MI~FM~MN~MS~MO~MT~NE~NV~NH~NJ~NM~NY~NC~ND~MP~OH~OK~OR~PW~PA~PR~RI~SC~SD~TN~TX~UT~VT~VI~VA~WA~WV~WI~WY', |
| 264 |
'sub_names': 'Alabama~Alaska~American Samoa~Arizona~Arkansas~Armed Forces (AA)~Armed Forces (AE)~Armed Forces (AP)~California~Colorado~Connecticut~Delaware~District of Columbia~Florida~Georgia~Guam~Hawaii~Idaho~Illinois~Indiana~Iowa~Kansas~Kentucky~Louisiana~Maine~Marshall Islands~Maryland~Massachusetts~Michigan~Micronesia~Minnesota~Mississippi~Missouri~Montana~Nebraska~Nevada~New Hampshire~New Jersey~New Mexico~New York~North Carolina~North Dakota~Northern Mariana Islands~Ohio~Oklahoma~Oregon~Palau~Pennsylvania~Puerto Rico~Rhode Island~South Carolina~South Dakota~Tennessee~Texas~Utah~Vermont~Virgin Islands~Virginia~Washington~West Virginia~Wisconsin~Wyoming', |
| 265 |
'sub_zipexs': '35000,36999~99500,99999~96799~85000,86999~71600,72999~34000,34099~09000,09999~96200,96699~90000,96199~80000,81999~06000,06999~19700,19999~20000,20099:20200,20599:56900,56999~32000,33999:34100,34999~30000,31999:39800,39899:39901~96910,96932~96700,96798:96800,96899~83200,83999~60000,62999~46000,47999~50000,52999~66000,67999~40000,42799~70000,71599~03900,04999~96960,96979~20600,21999~01000,02799:05501:05544~48000,49999~96941,96944~55000,56799~38600,39799~63000,65999~59000,59999~68000,69999~88900,89999~03000,03899~07000,08999~87000,88499~10000,14999:06390:00501:00544~27000,28999~58000,58999~96950,96952~43000,45999~73000,74999~97000,97999~96940~15000,19699~00600,00799:00900,00999~02800,02999~29000,29999~57000,57999~37000,38599~75000,79999:88500,88599:73301:73344~84000,84999~05000,05999~00800,00899~20100,20199:22000,24699~98000,99499~24700,26999~53000,54999~82000,83199:83414', |
| 266 |
'sub_zips': '3[56]~99[5-9]~96799~8[56]~71[6-9]|72~340~09~96[2-6]~9[0-5]|96[01]~8[01]~06~19[7-9]~20[02-5]|569~3[23]|34[1-9]~3[01]|398|39901~969([1-2]\\d|3[12])~967[0-8]|9679[0-8]|968~83[2-9]~6[0-2]~4[67]~5[0-2]~6[67]~4[01]|42[0-7]~70|71[0-5]~039|04~969[67]~20[6-9]|21~01|02[0-7]|05501|05544~4[89]~9694[1-4]~55|56[0-7]~38[6-9]|39[0-7]~6[3-5]~59~6[89]~889|89~03[0-8]~0[78]~87|88[0-4]~1[0-4]|06390|00501|00544~2[78]~58~9695[0-2]~4[3-5]~7[34]~97~969(39|40)~1[5-8]|19[0-6]~00[679]~02[89]~29~57~37|38[0-5]~7[5-9]|885|73301|73344~84~05~008~201|2[23]|24[0-6]~98|99[0-4]~24[7-9]|2[56]~5[34]~82|83[01]|83414', |
| 267 |
'upper': 'CS', |
| 268 |
'zip': '(\\d{5})(?:[ \\-](\\d{4}))?', |
| 269 |
'zip_name_type': 'zip', |
| 270 |
'zipex': '95014,22162-1010'} |
| 271 |
>>> i18n_country_data['US/CA'] |
| 272 |
{'id': 'data/US/CA', |
| 273 |
'key': 'CA', |
| 274 |
'lang': 'en', |
| 275 |
'name': 'California', |
| 276 |
'zip': '9[0-5]|96[01]', |
| 277 |
'zipex': '90000,96199'} |
| 278 |
|
| 279 |
|
| 280 |
Used with Django form |
| 281 |
--------------------- |
| 282 |
|
| 283 |
Django forms will return only required address fields in ``form.cleaned_data`` dict. So addresses in the database will be normalized. |
| 284 |
|
| 285 |
.. code:: python |
| 286 |
|
| 287 |
from django import forms |
| 288 |
|
| 289 |
from i18naddress import InvalidAddress, normalize_address, get_validation_rules |
| 290 |
|
| 291 |
|
| 292 |
class AddressForm(forms.Form): |
| 293 |
|
| 294 |
COUNTRY_CHOICES = [ |
| 295 |
('PL', 'Poland'), |
| 296 |
('AE', 'United Arab Emirates'), |
| 297 |
('US', 'United States of America')] |
| 298 |
|
| 299 |
ERROR_MESSAGES = { |
| 300 |
'required': 'This field is required', |
| 301 |
'invalid': 'Enter a valid name'} |
| 302 |
|
| 303 |
name = forms.CharField(required=True) |
| 304 |
company_name = forms.CharField(required=False) |
| 305 |
street_address = forms.CharField(required=False) |
| 306 |
city = forms.CharField(required=False) |
| 307 |
city_area = forms.CharField(required=False) |
| 308 |
country_code = forms.ChoiceField(required=True, choices=COUNTRY_CHOICES) |
| 309 |
country_area = forms.CharField(required=False) |
| 310 |
postal_code = forms.CharField(required=False) |
| 311 |
|
| 312 |
def clean(self): |
| 313 |
clean_data = super(AddressForm, self).clean() |
| 314 |
validation_rules = get_validation_rules(clean_data) |
| 315 |
try: |
| 316 |
valid_address = normalize_address(clean_data) |
| 317 |
except InvalidAddress as e: |
| 318 |
errors = e.errors |
| 319 |
valid_address = None |
| 320 |
for field, error_code in errors.items(): |
| 321 |
if field == 'postal_code': |
| 322 |
examples = validation_rules.postal_code_examples |
| 323 |
msg = 'Invalid value, use fomat like %s' % examples |
| 324 |
else: |
| 325 |
msg = ERROR_MESSAGES[error_code] |
| 326 |
self.add_error(field, msg) |
| 327 |
return valid_address or clean_data |
| 328 |
|
| 329 |
|
| 330 |
.. image:: https://ga-beacon.appspot.com/UA-10159761-14/mirumee/google-i18n-address?pixel |
| 331 |
|
| 332 |
.. |codecov.io| image:: https://img.shields.io/codecov/c/github/mirumee/google-i18n-address.svg |
| 333 |
:target: https://codecov.io/github/mirumee/google-i18n-address?branch=master |
| 334 |
.. |Circle CI| image:: https://img.shields.io/circleci/project/mirumee/google-i18n-address.svg |
| 335 |
:target: https://circleci.com/gh/mirumee/google-i18n-address/tree/master |
| 336 |
.. |PyPi downloads| image:: https://img.shields.io/pypi/dm/google-i18n-address.svg |
| 337 |
:target: https://pypi.python.org/pypi/google-i18n-address |
| 338 |
.. |PyPi pythons| image:: https://img.shields.io/pypi/pyversions/google-i18n-address.svg |
| 339 |
:target: https://pypi.python.org/pypi/google-i18n-address |
| 340 |
.. |PyPi version| image:: https://img.shields.io/pypi/v/google-i18n-address.svg |
| 341 |
:target: https://pypi.python.org/pypi/google-i18n-address |
| 342 |
.. |GitHub| image:: https://img.shields.io/github/stars/mirumee/google-i18n-address.svg?style=social |
| 343 |
:target: https://github.com/mirumee/google-i18n-address |
| 344 |
.. |requires.io| image:: https://img.shields.io/requires/github/mirumee/google-i18n-address.svg |
| 345 |
:target: https://requires.io/github/mirumee/google-i18n-address/requirements/?branch=master |
| 346 |
|
| 347 |
Platform: UNKNOWN |
| 348 |
Classifier: Programming Language :: Python :: 2.7 |
| 349 |
Classifier: Programming Language :: Python :: 3.4 |
| 350 |
Classifier: Programming Language :: Python :: 3.5 |
| 351 |
Classifier: Programming Language :: Python :: 3.6 |
| 352 |
Classifier: Environment :: Web Environment |
| 353 |
Classifier: Topic :: Software Development :: Internationalization |