diff options
| author | Bobby <[email protected]> | 2022-11-27 15:36:40 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-11-27 15:36:40 -0500 |
| commit | 473c86e0ac00cc479401c40d05ad6cbc10d2cf0d (patch) | |
| tree | cd591dc2a11b005ab17071e3678991cb2800e910 | |
| parent | 2076d462ed8c38a1202fd5ca5b20f0e07accbbee (diff) | |
| download | edify-473c86e0ac00cc479401c40d05ad6cbc10d2cf0d.tar.xz edify-473c86e0ac00cc479401c40d05ad6cbc10d2cf0d.zip | |
added zip code validator
| -rw-r--r-- | docs/built-in/index.rst | 753 | ||||
| -rw-r--r-- | docs/built-in/zip_locales.rst | 717 | ||||
| -rw-r--r-- | src/edify/library/__init__.py | 1 | ||||
| -rw-r--r-- | src/edify/library/support/zip.py | 1172 | ||||
| -rw-r--r-- | src/edify/library/zip.py | 28 | ||||
| -rw-r--r-- | tests/test_zip.py | 42 |
6 files changed, 2713 insertions, 0 deletions
diff --git a/docs/built-in/index.rst b/docs/built-in/index.rst index 5025195..e28e515 100644 --- a/docs/built-in/index.rst +++ b/docs/built-in/index.rst @@ -1,3 +1,13 @@ +.. |toggleStart| raw:: html + + <details> + <summary style="font-size: 1.2rem; font-style: bold; cursor: pointer;">View Supported Locales</summary> + +.. |toggleEnd| raw:: html + + </details> + + Pre-Built Pattern API Reference ================================ @@ -191,3 +201,746 @@ You can use the ``uuid`` function as follows: uuid('123e4567-e89b-12d3-a456-426655440000') # returns True uuid('123e4567-e') # returns False + +zip(zip: str, locale?: str) +--------------------------- + +The ``zip`` function verifies that a string is a valid ZIP code. The function takes a ``string`` argument which is supposed to be a valid ZIP code. The function returns ``True`` if the string is a valid ZIP code, and ``False`` otherwise. + +The ``zip`` function also accepts an optional ``locale`` argument. The ``locale`` argument is a string that you can use to configure what types of ZIP codes the function should match. You can view the ``locale`` argument values below. + +|toggleStart| + +.. list-table:: + :header-rows: 1 + + * - Country + - Locale + + * - Afghanistan + - AF + + * - Albania + - AL + + * - Algeria + - DZ + + * - American Samoa + - AS + + * - Andorra + - AD + + * - Angola + - AO + + * - Anguilla + - AI + + * - Antigua and Barbuda + - AG + + * - Argentina + - AR + + * - Armenia + - AM + + * - Aruba + - AW + + * - Australia + - AU + + * - Austria + - AT + + * - Azerbaijan + - AZ + + * - Bahamas + - BS + + * - Bahrain + - BH + + * - Bangladesh + - BD + + * - Barbados + - BB + + * - Belarus + - BY + + * - Belgium + - BE + + * - Belize + - BZ + + * - Benin + - BJ + + * - Bermuda + - BM + + * - Bhutan + - BT + + * - Bolivia + - BO + + * - Bonaire + - BQ + + * - Bosnia and Herzegovina + - BA + + * - Botswana + - BW + + * - Brazil + - BR + + * - Brunei + - BN + + * - Bulgaria + - BG + + * - Burkina Faso + - BF + + * - Burundi + - BI + + * - Cambodia + - KH + + * - Cameroon + - CM + + * - Canada + - CA + + * - Canary Islands + - CI + + * - Cape Verde + - CV + + * - Cayman Islands + - KY + + * - Central African Republic + - CF + + * - Chad + - TD + + * - Channel Islands + - CI + + * - Chile + - CL + + * - China, People's Republic + - CN + + * - Colombia + - CO + + * - Comoros + - KM + + * - Congo + - CG + + * - Congo, The Democratic Republic of + - CD + + * - Cook Islands + - CK + + * - Costa Rica + - CR + + * - Côte d'Ivoire + - CI + + * - Croatia + - HR + + * - Cuba + - CU + + * - Curacao + - CW + + * - Cyprus + - CY + + * - Czech Republic + - CZ + + * - Denmark + - DK + + * - Djibouti + - DJ + + * - Dominica + - DM + + * - Dominican Republic + - DO + + * - East Timor + - TL + + * - Ecuador + - EC + + * - Egypt + - EG + + * - El Salvador + - SV + + * - Eritrea + - ER + + * - Estonia + - EE + + * - Ethiopia + - ET + + * - Falkland Islands + - FK + + * - Faroe Islands + - FO + + * - Fiji + - FJ + + * - Finland + - FI + + * - France + - FR + + * - French Polynesia + - PF + + * - Gabon + - GA + + * - Gambia + - GM + + * - Georgia + - GE + + * - Germany + - DE + + * - Ghana + - GH + + * - Gibraltar + - GI + + * - Greece + - GR + + * - Greenland + - GL + + * - Grenada + - GD + + * - Guadeloupe + - GP + + * - Guam + - GU + + * - Guatemala + - GT + + * - Guernsey + - GG + + * - Guinea-Bissau + - GW + + * - Guinea-Equatorial + - GQ + + * - Guinea Republic + - GN + + * - Guyana (British) + - GY + + * - Guyana (French) + - GF + + * - Haiti + - HT + + * - Honduras + - HN + + * - Hong Kong + - HK + + * - Hungary + - HU + + * - Iceland + - IS + + * - India + - IN + + * - Indonesia + - ID + + * - Iran + - IR + + * - Iraq + - IQ + + * - Ireland, Republic of + - IE + + * - Islas Malvinas + - FK + + * - Israel + - IL + + * - Italy + - IT + + * - Ivory Coast + - CI + + * - Jamaica + - JM + + * - Japan + - JP + + * - Jersey + - JE + + * - Jordan + - JO + + * - Kazakhstan + - KZ + + * - Kenya + - KE + + * - Kiribati + - KI + + * - Korea, Republic of + - KR + + * - Korea, The D.P.R of + - KP + + * - Kosovo + - XK + + * - Kuwait + - KW + + * - Kyrgyzstan + - KG + + * - Laos + - LA + + * - Latvia + - LV + + * - Lebanon + - LB + + * - Lesotho + - LS + + * - Liberia + - LR + + * - Libya + - LY + + * - Liechtenstein + - LI + + * - Lithuania + - LT + + * - Luxembourg + - LU + + * - Macau + - MO + + * - Macedonia, Republic of + - MK + + * - Madagascar + - MG + + * - Malawi + - MW + + * - Malaysia + - MY + + * - Maldives + - MV + + * - Mali + - ML + + * - Malta + - MT + + * - Marshall Islands + - MH + + * - Martinique + - MQ + + * - Mauritania + - MR + + * - Mauritius + - MU + + * - Mayotte + - YT + + * - Mexico + - MX + + * - Moldova, Republic of + - MD + + * - Monaco + - MC + + * - Mongolia + - MN + + * - Montenegro + - ME + + * - Montserrat + - MS + + * - Morocco + - MA + + * - Mozambique + - MZ + + * - Myanmar + - MM + + * - Namibia + - NA + + * - Nauru + - NR + + * - Nepal + - NP + + * - Netherlands + - NL + + * - New Caledonia + - NC + + * - New Zealand + - NZ + + * - Nicaragua + - NI + + * - Niger + - NE + + * - Nigeria + - NG + + * - Niue + - NU + + * - Northern Mariana Islands + - MP + + * - Norway + - NO + + * - Oman + - OM + + * - Pakistan + - PK + + * - Palau + - PW + + * - Panama + - PA + + * - Papua New Guinea + - PG + + * - Paraguay + - PY + + * - Peru + - PE + + * - Philippines + - PH + + * - Poland + - PL + + * - Portugal + - PT + + * - Puerto Rico + - PR + + * - Qatar + - QA + + * - Réunion + - RE + + * - Romania + - RO + + * - Russian Federation + - RU + + * - Rwanda + - RW + + * - Saipan + - MP + + * - Samoa + - WS + + * - Sao Tome and Principe + - ST + + * - Saudi Arabia + - SA + + * - Senegal + - SN + + * - Serbia + - RS + + * - Seychelles + - SC + + * - Sierra Leone + - SL + + * - Singapore + - SG + + * - Slovakia + - SK + + * - Slovenia + - SI + + * - Solomon Islands + - SB + + * - Somalia + - SO + + * - South Africa + - ZA + + * - South Sudan + - SS + + * - Spain + - ES + + * - Sri Lanka + - LK + + * - St. Barthélemy + - BL + + * - St. Croix + - VI + + * - St. Eustatius + - SE + + * - St. Helena + - SH + + * - St. John + - AG + + * - St. Kitts and Nevis + - KN + + * - St. Lucia + - LC + + * - St. Maarten + - SX + + * - St. Thomas + - VI + + * - St. Vincent and the Grenadines + - VC + + * - Sudan + - SD + + * - Suriname + - SR + + * - Swaziland + - SZ + + * - Sweden + - SE + + * - Switzerland + - CH + + * - Syria + - SY + + * - Tahiti + - PF + + * - Taiwan + - TW + + * - Tanzania + - TZ + + * - Thailand + - TH + + * - Togo + - TG + + * - Tonga + - TO + + * - Tortola + - VG + + * - Trinidad and Tobago + - TT + + * - Tunisia + - TN + + * - Turkey + - TR + + * - Turkmenistan + - TM + + * - Turks and Caicos Islands + - TC + + * - Tuvalu + - TV + + * - Uganda + - UG + + * - Ukraine + - UA + + * - United Arab Emirates + - AE + + * - United Kingdom + - GB + + * - United States of America + - US + + * - Uruguay + - UY + + * - Uzbekistan + - UZ + + * - Vanuatu + - VU + + * - Venezuela + - VE + + * - Vietnam + - VN + + * - Virgin Islands (British) + - VG + + * - Virgin Islands (US) + - VI + + * - Yemen + - YE + + * - Zambia + - ZM + + * - Zimbabwe + - ZW + +|toggleEnd| + +By default, the ``zip`` function matches ZIP codes for "US". Here's an example of how to use the ``zip`` function to match ZIP codes: + +.. code-block:: python + + from edify.library import zip + + zip('12345') # returns True + zip('1234') # returns False + zip('12345', locale='US') # returns True + zip('12345-1234') # returns True + zip('12345-1234', locale='US') # returns True + zip('123456', locale='IN') # returns True + +If you supply an Invalid or empty value in the ``locale`` argument, the function will raise a ``ValueError`` exception. Similarly, if you supply another data type in the ``locale`` argument, the function will raise a ``TypeError`` exception. diff --git a/docs/built-in/zip_locales.rst b/docs/built-in/zip_locales.rst new file mode 100644 index 0000000..92670b1 --- /dev/null +++ b/docs/built-in/zip_locales.rst @@ -0,0 +1,717 @@ +.. list-table:: + :header-rows: 1 + + * - Country + - Locale + + * - Afghanistan + - AF + + * - Albania + - AL + + * - Algeria + - DZ + + * - American Samoa + - AS + + * - Andorra + - AD + + * - Angola + - AO + + * - Anguilla + - AI + + * - Antigua and Barbuda + - AG + + * - Argentina + - AR + + * - Armenia + - AM + + * - Aruba + - AW + + * - Australia + - AU + + * - Austria + - AT + + * - Azerbaijan + - AZ + + * - Bahamas + - BS + + * - Bahrain + - BH + + * - Bangladesh + - BD + + * - Barbados + - BB + + * - Belarus + - BY + + * - Belgium + - BE + + * - Belize + - BZ + + * - Benin + - BJ + + * - Bermuda + - BM + + * - Bhutan + - BT + + * - Bolivia + - BO + + * - Bonaire + - BQ + + * - Bosnia and Herzegovina + - BA + + * - Botswana + - BW + + * - Brazil + - BR + + * - Brunei + - BN + + * - Bulgaria + - BG + + * - Burkina Faso + - BF + + * - Burundi + - BI + + * - Cambodia + - KH + + * - Cameroon + - CM + + * - Canada + - CA + + * - Canary Islands + - CI + + * - Cape Verde + - CV + + * - Cayman Islands + - KY + + * - Central African Republic + - CF + + * - Chad + - TD + + * - Channel Islands + - CI + + * - Chile + - CL + + * - China, People's Republic + - CN + + * - Colombia + - CO + + * - Comoros + - KM + + * - Congo + - CG + + * - Congo, The Democratic Republic of + - CD + + * - Cook Islands + - CK + + * - Costa Rica + - CR + + * - Côte d'Ivoire + - CI + + * - Croatia + - HR + + * - Cuba + - CU + + * - Curacao + - CW + + * - Cyprus + - CY + + * - Czech Republic + - CZ + + * - Denmark + - DK + + * - Djibouti + - DJ + + * - Dominica + - DM + + * - Dominican Republic + - DO + + * - East Timor + - TL + + * - Ecuador + - EC + + * - Egypt + - EG + + * - El Salvador + - SV + + * - Eritrea + - ER + + * - Estonia + - EE + + * - Ethiopia + - ET + + * - Falkland Islands + - FK + + * - Faroe Islands + - FO + + * - Fiji + - FJ + + * - Finland + - FI + + * - France + - FR + + * - French Polynesia + - PF + + * - Gabon + - GA + + * - Gambia + - GM + + * - Georgia + - GE + + * - Germany + - DE + + * - Ghana + - GH + + * - Gibraltar + - GI + + * - Greece + - GR + + * - Greenland + - GL + + * - Grenada + - GD + + * - Guadeloupe + - GP + + * - Guam + - GU + + * - Guatemala + - GT + + * - Guernsey + - GG + + * - Guinea-Bissau + - GW + + * - Guinea-Equatorial + - GQ + + * - Guinea Republic + - GN + + * - Guyana (British) + - GY + + * - Guyana (French) + - GF + + * - Haiti + - HT + + * - Honduras + - HN + + * - Hong Kong + - HK + + * - Hungary + - HU + + * - Iceland + - IS + + * - India + - IN + + * - Indonesia + - ID + + * - Iran + - IR + + * - Iraq + - IQ + + * - Ireland, Republic of + - IE + + * - Islas Malvinas + - FK + + * - Israel + - IL + + * - Italy + - IT + + * - Ivory Coast + - CI + + * - Jamaica + - JM + + * - Japan + - JP + + * - Jersey + - JE + + * - Jordan + - JO + + * - Kazakhstan + - KZ + + * - Kenya + - KE + + * - Kiribati + - KI + + * - Korea, Republic of + - KR + + * - Korea, The D.P.R of + - KP + + * - Kosovo + - XK + + * - Kuwait + - KW + + * - Kyrgyzstan + - KG + + * - Laos + - LA + + * - Latvia + - LV + + * - Lebanon + - LB + + * - Lesotho + - LS + + * - Liberia + - LR + + * - Libya + - LY + + * - Liechtenstein + - LI + + * - Lithuania + - LT + + * - Luxembourg + - LU + + * - Macau + - MO + + * - Macedonia, Republic of + - MK + + * - Madagascar + - MG + + * - Malawi + - MW + + * - Malaysia + - MY + + * - Maldives + - MV + + * - Mali + - ML + + * - Malta + - MT + + * - Marshall Islands + - MH + + * - Martinique + - MQ + + * - Mauritania + - MR + + * - Mauritius + - MU + + * - Mayotte + - YT + + * - Mexico + - MX + + * - Moldova, Republic of + - MD + + * - Monaco + - MC + + * - Mongolia + - MN + + * - Montenegro + - ME + + * - Montserrat + - MS + + * - Morocco + - MA + + * - Mozambique + - MZ + + * - Myanmar + - MM + + * - Namibia + - NA + + * - Nauru + - NR + + * - Nepal + - NP + + * - Netherlands + - NL + + * - New Caledonia + - NC + + * - New Zealand + - NZ + + * - Nicaragua + - NI + + * - Niger + - NE + + * - Nigeria + - NG + + * - Niue + - NU + + * - Northern Mariana Islands + - MP + + * - Norway + - NO + + * - Oman + - OM + + * - Pakistan + - PK + + * - Palau + - PW + + * - Panama + - PA + + * - Papua New Guinea + - PG + + * - Paraguay + - PY + + * - Peru + - PE + + * - Philippines + - PH + + * - Poland + - PL + + * - Portugal + - PT + + * - Puerto Rico + - PR + + * - Qatar + - QA + + * - Réunion + - RE + + * - Romania + - RO + + * - Russian Federation + - RU + + * - Rwanda + - RW + + * - Saipan + - MP + + * - Samoa + - WS + + * - Sao Tome and Principe + - ST + + * - Saudi Arabia + - SA + + * - Senegal + - SN + + * - Serbia + - RS + + * - Seychelles + - SC + + * - Sierra Leone + - SL + + * - Singapore + - SG + + * - Slovakia + - SK + + * - Slovenia + - SI + + * - Solomon Islands + - SB + + * - Somalia + - SO + + * - South Africa + - ZA + + * - South Sudan + - SS + + * - Spain + - ES + + * - Sri Lanka + - LK + + * - St. Barthélemy + - BL + + * - St. Croix + - VI + + * - St. Eustatius + - SE + + * - St. Helena + - SH + + * - St. John + - AG + + * - St. Kitts and Nevis + - KN + + * - St. Lucia + - LC + + * - St. Maarten + - SX + + * - St. Thomas + - VI + + * - St. Vincent and the Grenadines + - VC + + * - Sudan + - SD + + * - Suriname + - SR + + * - Swaziland + - SZ + + * - Sweden + - SE + + * - Switzerland + - CH + + * - Syria + - SY + + * - Tahiti + - PF + + * - Taiwan + - TW + + * - Tanzania + - TZ + + * - Thailand + - TH + + * - Togo + - TG + + * - Tonga + - TO + + * - Tortola + - VG + + * - Trinidad and Tobago + - TT + + * - Tunisia + - TN + + * - Turkey + - TR + + * - Turkmenistan + - TM + + * - Turks and Caicos Islands + - TC + + * - Tuvalu + - TV + + * - Uganda + - UG + + * - Ukraine + - UA + + * - United Arab Emirates + - AE + + * - United Kingdom + - GB + + * - United States of America + - US + + * - Uruguay + - UY + + * - Uzbekistan + - UZ + + * - Vanuatu + - VU + + * - Venezuela + - VE + + * - Vietnam + - VN + + * - Virgin Islands (British) + - VG + + * - Virgin Islands (US) + - VI + + * - Yemen + - YE + + * - Zambia + - ZM + + * - Zimbabwe + - ZW +
\ No newline at end of file diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py index f2827ea..d048a8c 100644 --- a/src/edify/library/__init__.py +++ b/src/edify/library/__init__.py @@ -10,3 +10,4 @@ from .mail import email_rfc_5322 from .phone import phone_number from .url import url from .uuid import uuid +from .zip import zip diff --git a/src/edify/library/support/zip.py b/src/edify/library/support/zip.py new file mode 100644 index 0000000..350837f --- /dev/null +++ b/src/edify/library/support/zip.py @@ -0,0 +1,1172 @@ +ZIP_LOCALES = [ + { + "abbrev": "AF", + "name": "Afghanistan", + "zip": "[0-9]{4}" + }, + { + "abbrev": "AL", + "name": "Albania", + "zip": "(120|122)[0-9]{2}" + }, + { + "abbrev": "DZ", + "name": "Algeria", + "zip": "[0-9]{5}" + }, + { + "abbrev": "AS", + "name": "American Samoa", + "zip": "[0-9]{5}" + }, + { + "abbrev": "AD", + "name": "Andorra", + "zip": "[0-9]{5}" + }, + { + "abbrev": "AO", + "name": "Angola" + }, + { + "abbrev": "AI", + "name": "Anguilla", + "zip": "AI-2640" + }, + { + "abbrev": "AG", + "name": "Antigua and Barbuda" + }, + { + "abbrev": "AR", + "name": "Argentina", + "zip": "[A-Z]{1}[0-9]{4}[A-Z]{3}" + }, + { + "abbrev": "AM", + "name": "Armenia", + "zip": "[0-9]{4}" + }, + { + "abbrev": "AW", + "name": "Aruba" + }, + { + "abbrev": "AU", + "name": "Australia", + "zip": "[0-9]{4}" + }, + { + "abbrev": "AT", + "name": "Austria", + "zip": "[0-9]{4}" + }, + { + "abbrev": "AZ", + "name": "Azerbaijan", + "zip": "[0-9]{4}" + }, + { + "abbrev": "BS", + "name": "Bahamas" + }, + { + "abbrev": "BH", + "name": "Bahrain" + }, + { + "abbrev": "BD", + "name": "Bangladesh", + "zip": "[0-9]{4}" + }, + { + "abbrev": "BB", + "name": "Barbados", + "zip": "BB[0-9]{5}" + }, + { + "abbrev": "BY", + "name": "Belarus", + "zip": "[0-9]{6}" + }, + { + "abbrev": "BE", + "name": "Belgium", + "zip": "[0-9]{4}" + }, + { + "abbrev": "BZ", + "name": "Belize" + }, + { + "abbrev": "BJ", + "name": "Benin" + }, + { + "abbrev": "BM", + "name": "Bermuda", + "zip": "[A-Z]{2}[0-9]{2}" + }, + { + "abbrev": "BT", + "name": "Bhutan", + "zip": "[0-9]{5}" + }, + { + "abbrev": "BO", + "name": "Bolivia" + }, + { + "abbrev": "BQ", + "name": "Bonaire" + }, + { + "abbrev": "BA", + "name": "Bosnia and Herzegovina", + "zip": "[0-9]{5}" + }, + { + "abbrev": "BW", + "name": "Botswana" + }, + { + "abbrev": "BR", + "name": "Brazil", + "zip": "[0-9]{5}-[0-9]{3}" + }, + { + "abbrev": "BN", + "name": "Brunei", + "zip": "[A-Z]{2}[0-9]{4}" + }, + { + "abbrev": "BG", + "name": "Bulgaria", + "zip": "[0-9]{4}" + }, + { + "abbrev": "BF", + "name": "Burkina Faso" + }, + { + "abbrev": "BI", + "name": "Burundi" + }, + { + "abbrev": "KH", + "name": "Cambodia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "CM", + "name": "Cameroon" + }, + { + "abbrev": "CA", + "name": "Canada", + "zip": "[A-Z][0-9][A-Z] ?[0-9][A-Z][0-9]" + }, + { + "abbrev": "CI", + "name": "Canary Islands", + "zip": "[0-9]{5}" + }, + { + "abbrev": "CV", + "name": "Cape Verde", + "zip": "[0-9]{4}" + }, + { + "abbrev": "KY", + "name": "Cayman Islands", + "zip": "[A-Z]{2}[0-9]-[0-9]{4}" + }, + { + "abbrev": "CF", + "name": "Central African Republic" + }, + { + "abbrev": "TD", + "name": "Chad" + }, + { + "abbrev": "CI", + "name": "Channel Islands", + "zip": "[A-Z]{2}[0-9]{2}" + }, + { + "abbrev": "CL", + "name": "Chile", + "zip": "[0-9]{7}" + }, + { + "abbrev": "CN", + "name": "China, People's Republic", + "zip": "[0-9]{6}" + }, + { + "abbrev": "CO", + "name": "Colombia", + "zip": "[0-9]{6}" + }, + { + "abbrev": "KM", + "name": "Comoros" + }, + { + "abbrev": "CG", + "name": "Congo" + }, + { + "abbrev": "CD", + "name": "Congo, The Democratic Republic of" + }, + { + "abbrev": "CK", + "name": "Cook Islands" + }, + { + "abbrev": "CR", + "name": "Costa Rica", + "zip": "[0-9]{5}" + }, + { + "abbrev": "CI", + "name": "Côte d'Ivoire" + }, + { + "abbrev": "HR", + "name": "Croatia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "CU", + "name": "Cuba", + "zip": "[0-9]{5}" + }, + { + "abbrev": "CW", + "name": "Curacao" + }, + { + "abbrev": "CY", + "name": "Cyprus", + "zip": "[0-9]{4}" + }, + { + "abbrev": "CZ", + "name": "Czech Republic", + "zip": "[0-9]{3} [0-9]{2}" + }, + { + "abbrev": "DK", + "name": "Denmark", + "zip": "[0-9]{5}" + }, + { + "abbrev": "DJ", + "name": "Djibouti" + }, + { + "abbrev": "DM", + "name": "Dominica" + }, + { + "abbrev": "DO", + "name": "Dominican Republic", + "zip": "[0-9]{5}" + }, + { + "abbrev": "TL", + "name": "East Timor" + }, + { + "abbrev": "EC", + "name": "Ecuador", + "zip": "[0-9]{6}" + }, + { + "abbrev": "EG", + "name": "Egypt", + "zip": "[0-9]{5}" + }, + { + "abbrev": "SV", + "name": "El Salvador", + "zip": "[0-9]{4}" + }, + { + "abbrev": "ER", + "name": "Eritrea" + }, + { + "abbrev": "EE", + "name": "Estonia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "ET", + "name": "Ethiopia", + "zip": "[0-9]{4}" + }, + { + "abbrev": "FK", + "name": "Falkland Islands", + "zip": "FIQQ 1ZZ" + }, + { + "abbrev": "FO", + "name": "Faroe Islands", + "zip": "[0-9]{3}" + }, + { + "abbrev": "FJ", + "name": "Fiji" + }, + { + "abbrev": "FI", + "name": "Finland", + "zip": "[0-9]{5}" + }, + { + "abbrev": "FR", + "name": "France", + "zip": "[0-9]{5}" + }, + { + "abbrev": "PF", + "name": "French Polynesia", + "zip": "987[0-9]{2}", + "range": [ + "98700", + "98790" + ] + }, + { + "abbrev": "GA", + "name": "Gabon" + }, + { + "abbrev": "GM", + "name": "Gambia" + }, + { + "abbrev": "GE", + "name": "Georgia" + }, + { + "abbrev": "DE", + "name": "Germany", + "zip": "[0-9]{5}" + }, + { + "abbrev": "GH", + "name": "Ghana" + }, + { + "abbrev": "GI", + "name": "Gibraltar", + "zip": "GX11 1AA" + }, + { + "abbrev": "GR", + "name": "Greece", + "zip": "[0-9]{3} [0-9]{2}" + }, + { + "abbrev": "GL", + "name": "Greenland", + "zip": "[0-9]{4}" + }, + { + "abbrev": "GD", + "name": "Grenada" + }, + { + "abbrev": "GP", + "name": "Guadeloupe", + "zip": "971[0-9]{2}", + "range": [ + "97100", + "97190" + ] + }, + { + "abbrev": "GU", + "name": "Guam", + "zip": "\\d{5}(?:[-\\s]\\d{4})?", + "range": [ + "96910", + "96932" + ] + }, + { + "abbrev": "GT", + "name": "Guatemala", + "zip": "[0-9]{5}" + }, + { + "abbrev": "GG", + "name": "Guernsey", + "zip": "([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\\s?[0-9][A-Za-z]{2})" + }, + { + "abbrev": "GW", + "name": "Guinea-Bissau", + "zip": "[0-9]{4}" + }, + { + "abbrev": "GQ", + "name": "Guinea-Equatorial" + }, + { + "abbrev": "GN", + "name": "Guinea Republic", + "zip": "[0-9]{3}" + }, + { + "abbrev": "GY", + "name": "Guyana (British)" + }, + { + "abbrev": "GF", + "name": "Guyana (French)", + "zip": "973[0-9]{2}", + "range": [ + "97300", + "97390" + ] + }, + { + "abbrev": "HT", + "name": "Haiti", + "zip": "[0-9]{4}" + }, + { + "abbrev": "HN", + "name": "Honduras", + "zip": "[0-9]{5}" + }, + { + "abbrev": "HK", + "name": "Hong Kong" + }, + { + "abbrev": "HU", + "name": "Hungary", + "zip": "[0-9]{4}" + }, + { + "abbrev": "IS", + "name": "Iceland", + "zip": "[0-9]{3}" + }, + { + "abbrev": "IN", + "name": "India", + "zip": "^(?!0{1})\d{6}$" + }, + { + "abbrev": "ID", + "name": "Indonesia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "IR", + "name": "Iran", + "zip": "[0-9]{5}" + }, + { + "abbrev": "IQ", + "name": "Iraq", + "zip": "[0-9]{5}" + }, + { + "abbrev": "IE", + "name": "Ireland, Republic of", + "zip": "(?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$" + }, + { + "abbrev": "FK", + "name": "Islas Malvinas", + "zip": "FIQQ 1ZZ" + }, + { + "abbrev": "IL", + "name": "Israel", + "zip": "[0-9]{5}|[0-9]{7}" + }, + { + "abbrev": "IT", + "name": "Italy", + "zip": "[0-9]{5}" + }, + { + "abbrev": "CI", + "name": "Ivory Coast" + }, + { + "abbrev": "JM", + "name": "Jamaica" + }, + { + "abbrev": "JP", + "name": "Japan", + "zip": "[0-9]{3}-[0-9]{4}" + }, + { + "abbrev": "JE", + "name": "Jersey", + "zip": "([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\\s?[0-9][A-Za-z]{2})" + }, + { + "abbrev": "JO", + "name": "Jordan", + "zip": "[0-9]{5}" + }, + { + "abbrev": "KZ", + "name": "Kazakhstan", + "zip": "[0-9]{6}" + }, + { + "abbrev": "KE", + "name": "Kenya", + "zip": "[0-9]{5}" + }, + { + "abbrev": "KI", + "name": "Kiribati" + }, + { + "abbrev": "KR", + "name": "Korea, Republic of", + "zip": "[0-9]{5}" + }, + { + "abbrev": "KP", + "name": "Korea, The D.P.R of" + }, + { + "abbrev": "XK", + "name": "Kosovo", + "zip": "[0-9]{5}" + }, + { + "abbrev": "KW", + "name": "Kuwait", + "zip": "[0-9]{5}" + }, + { + "abbrev": "KG", + "name": "Kyrgyzstan", + "zip": "[0-9]{6}" + }, + { + "abbrev": "LA", + "name": "Laos", + "zip": "[0-9]{5}" + }, + { + "abbrev": "LV", + "name": "Latvia", + "zip": "LV-[0-9]{4}" + }, + { + "abbrev": "LB", + "name": "Lebanon", + "zip": "[0-9]{4} [0-9]{4}" + }, + { + "abbrev": "LS", + "name": "Lesotho", + "zip": "[0-9]{3}" + }, + { + "abbrev": "LR", + "name": "Liberia", + "zip": "[0-9]{4}" + }, + { + "abbrev": "LY", + "name": "Libya" + }, + { + "abbrev": "LI", + "name": "Liechtenstein", + "zip": "[0-9]{4}", + "range": [ + "9485", + "9498" + ] + }, + { + "abbrev": "LT", + "name": "Lithuania", + "zip": "LT-[0-9]{5}" + }, + { + "abbrev": "LU", + "name": "Luxembourg", + "zip": "[0-9]{4}" + }, + { + "abbrev": "MO", + "name": "Macau" + }, + { + "abbrev": "MK", + "name": "Macedonia, Republic of", + "zip": "[0-9]{4}" + }, + { + "abbrev": "MG", + "name": "Madagascar", + "zip": "[0-9]{3}" + }, + { + "abbrev": "MW", + "name": "Malawi" + }, + { + "abbrev": "MY", + "name": "Malaysia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "MV", + "name": "Maldives", + "zip": "[0-9]{5}" + }, + { + "abbrev": "ML", + "name": "Mali" + }, + { + "abbrev": "MT", + "name": "Malta", + "zip": "[A-Z]{3} [0-9]{4}" + }, + { + "abbrev": "MH", + "name": "Marshall Islands", + "zip": "\\d{5}(?:[-\\s]\\d{4})?", + "range": [ + "96960", + "96970" + ] + }, + { + "abbrev": "MQ", + "name": "Martinique", + "zip": "972[0-9]{2}", + "range": [ + "97200", + "97290" + ] + }, + { + "abbrev": "MR", + "name": "Mauritania" + }, + { + "abbrev": "MU", + "name": "Mauritius", + "zip": "[0-9]{5}" + }, + { + "abbrev": "YT", + "name": "Mayotte", + "zip": "976[0-9]{2}", + "range": [ + "97600", + "97690" + ] + }, + { + "abbrev": "MX", + "name": "Mexico", + "zip": "[0-9]{5}" + }, + { + "abbrev": "MD", + "name": "Moldova, Republic of", + "zip": "MD-?[0-9]{4}" + }, + { + "abbrev": "MC", + "name": "Monaco", + "zip": "980[0-9]{2}" + }, + { + "abbrev": "MN", + "name": "Mongolia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "ME", + "name": "Montenegro", + "zip": "[0-9]{5}" + }, + { + "abbrev": "MS", + "name": "Montserrat", + "zip": "MSR [0-9]{4}", + "range": [ + "MSR 1110", + "MSR 1350" + ] + }, + { + "abbrev": "MA", + "name": "Morocco", + "zip": "[0-9]{5}" + }, + { + "abbrev": "MZ", + "name": "Mozambique", + "zip": "[0-9]{4}" + }, + { + "abbrev": "MM", + "name": "Myanmar", + "zip": "[0-9]{5}" + }, + { + "abbrev": "NA", + "name": "Namibia" + }, + { + "abbrev": "NR", + "name": "Nauru" + }, + { + "abbrev": "NP", + "name": "Nepal", + "zip": "[0-9]{5}" + }, + { + "abbrev": "NL", + "name": "Netherlands", + "zip": "(?:NL-)?(\\d{4})\\s*([A-Z]{2})" + }, + { + "abbrev": "NC", + "name": "New Caledonia", + "zip": "988[0-9]{2}", + "range": [ + "96950", + "96952" + ] + }, + { + "abbrev": "NZ", + "name": "New Zealand", + "zip": "[0-9]{4}" + }, + { + "abbrev": "NI", + "name": "Nicaragua" + }, + { + "abbrev": "NE", + "name": "Niger", + "zip": "[0-9]{4}" + }, + { + "abbrev": "NG", + "name": "Nigeria", + "zip": "[0-9]{6}" + }, + { + "abbrev": "NU", + "name": "Niue" + }, + { + "abbrev": "MP", + "name": "Northern Mariana Islands", + "zip": "^\\d{5}(?:[-\\s]\\d{4})?$" + }, + { + "abbrev": "NO", + "name": "Norway", + "zip": "[0-9]{4}" + }, + { + "abbrev": "OM", + "name": "Oman", + "zip": "[0-9]{3}" + }, + { + "abbrev": "PK", + "name": "Pakistan", + "zip": "[0-9]{5}" + }, + { + "abbrev": "PW", + "name": "Palau", + "zip": "\\d{5}(?:[-\\s]\\d{4})?" + }, + { + "abbrev": "PA", + "name": "Panama", + "zip": "[0-9]{4}" + }, + { + "abbrev": "PG", + "name": "Papua New Guinea", + "zip": "[0-9]{3}" + }, + { + "abbrev": "PY", + "name": "Paraguay", + "zip": "[0-9]{4}" + }, + { + "abbrev": "PE", + "name": "Peru", + "zip": "[0-9]{5}" + }, + { + "abbrev": "PH", + "name": "Philippines", + "zip": "[0-9]{4}" + }, + { + "abbrev": "PL", + "name": "Poland", + "zip": "[0-9]{2}-[0-9]{3}" + }, + { + "abbrev": "PT", + "name": "Portugal", + "zip": "[0-9]{4}-[0-9]{3}" + }, + { + "abbrev": "PR", + "name": "Puerto Rico", + "zip": "\\d{5}(?:[-\\s]\\d{4})?" + }, + { + "abbrev": "QA", + "name": "Qatar" + }, + { + "abbrev": "RE", + "name": "Réunion", + "zip": "974[0-9]{2}", + "range": [ + "97400", + "97490" + ] + }, + { + "abbrev": "RO", + "name": "Romania", + "zip": "[0-9]{6}" + }, + { + "abbrev": "RU", + "name": "Russian Federation", + "zip": "[0-9]{6}" + }, + { + "abbrev": "RW", + "name": "Rwanda" + }, + { + "abbrev": "MP", + "name": "Saipan", + "zip": "96950" + }, + { + "abbrev": "WS", + "name": "Samoa", + "zip": "WS[0-9]{4}" + }, + { + "abbrev": "ST", + "name": "Sao Tome and Principe" + }, + { + "abbrev": "SA", + "name": "Saudi Arabia", + "zip": "[0-9]{5}(-[0-9]{4})?" + }, + { + "abbrev": "SN", + "name": "Senegal", + "zip": "[0-9]{5}" + }, + { + "abbrev": "RS", + "name": "Serbia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "SC", + "name": "Seychelles" + }, + { + "abbrev": "SL", + "name": "Sierra Leone" + }, + { + "abbrev": "SG", + "name": "Singapore", + "zip": "[0-9]{6}" + }, + { + "abbrev": "SK", + "name": "Slovakia", + "zip": "[0-9]{3} [0-9]{2}" + }, + { + "abbrev": "SI", + "name": "Slovenia", + "zip": "[0-9]{4}" + }, + { + "abbrev": "SB", + "name": "Solomon Islands" + }, + { + "abbrev": "SO", + "name": "Somalia", + "zip": "[A-Z]{2} [0-9]{5}" + }, + { + "abbrev": "ZA", + "name": "South Africa", + "zip": "[0-9]{4}" + }, + { + "abbrev": "SS", + "name": "South Sudan" + }, + { + "abbrev": "ES", + "name": "Spain", + "zip": "[0-9]{5}" + }, + { + "abbrev": "LK", + "name": "Sri Lanka", + "zip": "[0-9]{4}" + }, + { + "abbrev": "BL", + "name": "St. Barthélemy", + "zip": "[0-9]{5}", + "range": [ + "97100", + "97190" + ] + }, + { + "abbrev": "VI", + "name": "St. Croix", + "zip": "[0-9]{5}" + }, + { + "abbrev": "SE", + "name": "St. Eustatius" + }, + { + "abbrev": "SH", + "name": "St. Helena", + "zip": "STHL 1ZZ" + }, + { + "abbrev": "AG", + "name": "St. John", + "zip": "\\d{5}(?:[-\\s]\\d{4})?" + }, + { + "abbrev": "KN", + "name": "St. Kitts and Nevis", + "zip": "[A-Z]{2}[0-9]{4}" + }, + { + "abbrev": "LC", + "name": "St. Lucia", + "zip": "[A-Z]{2}[0-9]{2} [0-9]{3}" + }, + { + "abbrev": "SX", + "name": "St. Maarten" + }, + { + "abbrev": "VI", + "name": "St. Thomas" + }, + { + "abbrev": "VC", + "name": "St. Vincent and the Grenadines", + "zip": "VC[0-9]{4}" + }, + { + "abbrev": "SD", + "name": "Sudan", + "zip": "[0-9]{5}" + }, + { + "abbrev": "SR", + "name": "Suriname" + }, + { + "abbrev": "SZ", + "name": "Swaziland", + "zip": "[A-Z]{1}[0-9]{3}" + }, + { + "abbrev": "SE", + "name": "Sweden", + "zip": "[0-9]{3} [0-9]{2}" + }, + { + "abbrev": "CH", + "name": "Switzerland", + "zip": "[0-9]{4}" + }, + { + "abbrev": "SY", + "name": "Syria" + }, + { + "abbrev": "PF", + "name": "Tahiti", + "zip": "[0-9]{5}" + }, + { + "abbrev": "TW", + "name": "Taiwan", + "zip": "[0-9]{3}(-[0-9]{2})?" + }, + { + "abbrev": "TZ", + "name": "Tanzania", + "zip": "[0-9]{5}" + }, + { + "abbrev": "TH", + "name": "Thailand", + "zip": "[0-9]{5}" + }, + { + "abbrev": "TG", + "name": "Togo" + }, + { + "abbrev": "TO", + "name": "Tonga" + }, + { + "abbrev": "VG", + "name": "Tortola", + "zip": "VG[0-9]{4}" + }, + { + "abbrev": "TT", + "name": "Trinidad and Tobago", + "zip": "[0-9]{6}" + }, + { + "abbrev": "TN", + "name": "Tunisia", + "zip": "[0-9]{4}" + }, + { + "abbrev": "TR", + "name": "Turkey", + "zip": "[0-9]{5}" + }, + { + "abbrev": "TM", + "name": "Turkmenistan", + "zip": "[0-9]{6}" + }, + { + "abbrev": "TC", + "name": "Turks and Caicos Islands", + "zip": "TKCA 1ZZ" + }, + { + "abbrev": "TV", + "name": "Tuvalu" + }, + { + "abbrev": "UG", + "name": "Uganda" + }, + { + "abbrev": "UA", + "name": "Ukraine", + "zip": "[0-9]{5}" + }, + { + "abbrev": "AE", + "name": "United Arab Emirates" + }, + { + "abbrev": "GB", + "name": "United Kingdom", + "zip": "([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\\s?[0-9][A-Za-z]{2})" + }, + { + "abbrev": "US", + "name": "United States of America", + "zip": "^[0-9]{5}(?:-[0-9]{4})?$" + }, + { + "abbrev": "UY", + "name": "Uruguay", + "zip": "[0-9]{5}" + }, + { + "abbrev": "UZ", + "name": "Uzbekistan", + "zip": "[0-9]{6}" + }, + { + "abbrev": "VU", + "name": "Vanuatu" + }, + { + "abbrev": "VE", + "name": "Venezuela", + "zip": "[0-9]{4}(-[A-Z]{1})?" + }, + { + "abbrev": "VN", + "name": "Vietnam", + "zip": "[0-9]{6}" + }, + { + "abbrev": "VG", + "name": "Virgin Islands (British)", + "zip": "VG[0-9]{4}" + }, + { + "abbrev": "VI", + "name": "Virgin Islands (US)", + "range": [ + "00801", + "00851" + ], + "zip": "\\d{5}(?:[-\\s]\\d{4})?" + }, + { + "abbrev": "YE", + "name": "Yemen" + }, + { + "abbrev": "ZM", + "name": "Zambia", + "zip": "[0-9]{5}" + }, + { + "abbrev": "ZW", + "name": "Zimbabwe" + } +] diff --git a/src/edify/library/zip.py b/src/edify/library/zip.py new file mode 100644 index 0000000..88152ee --- /dev/null +++ b/src/edify/library/zip.py @@ -0,0 +1,28 @@ +import re +from .support.zip import ZIP_LOCALES +locales = [locale["abbrev"] for locale in ZIP_LOCALES] + +def zip(zip: str, locale: str = "US") -> bool: + """Check if a string is a valid zip code. + + Args: + zip (str): The string to check. + locale (str): (optional) The locale to check against. Defaults to "US". + Returns: + bool: True if the string is a valid zip code, False otherwise. + """ + + if not isinstance(locale, str): + raise TypeError("locale must be a string") + + if locale == "": + raise ValueError("locale cannot be empty") + + if locale not in locales: + print(locales) + raise ValueError("locale must be one of {}".format(locales)) + + pattern = ZIP_LOCALES[locales.index(locale)]["zip"] + return re.match(pattern, zip) is not None + + diff --git a/tests/test_zip.py b/tests/test_zip.py new file mode 100644 index 0000000..18632f7 --- /dev/null +++ b/tests/test_zip.py @@ -0,0 +1,42 @@ +from edify.library import zip + +def test_valid_zips(): + zips = { + "12345": True, + "12345-1234": True, + "12345-123456": False, + "1234": False + } + for zip_string, expectation in zips.items(): + assert zip(zip_string) == expectation + +def test_invalid_locale(): + try: + zip("12345", locale = "INVALID") + except ValueError: + assert True + +def test_invalid_locale_type(): + try: + zip("12345", 5) + except TypeError: + assert True + +def test_empty_locale(): + try: + zip("12345", "") + except ValueError: + assert True + +def test_locale_IN(): + zips = { + "123456": True, + "000000": False, + "012345": False, + "12345": False, + "1234567": False + } + for zip_string, expectation in zips.items(): + assert zip(zip_string, locale="IN") == expectation + + |
