├── setup.cfg ├── .gitignore ├── tox.ini ├── .travis.yml ├── tests └── iso3166_1_test.py ├── README.rst ├── iso3166 ├── __init__.py └── table.csv └── setup.py /setup.cfg: -------------------------------------------------------------------------------- 1 | [bdist_wheel] 2 | universal=1 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.egg-info 2 | __pycache__ 3 | .idea 4 | .tox 5 | 6 | build 7 | dist 8 | *.py[c|o] 9 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py25,py26,py27,py32,py33,py34 3 | 4 | [testenv] 5 | deps= 6 | pytest 7 | flake8 8 | commands= 9 | py.test -v 10 | flake8 . 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.6" 4 | - "2.7" 5 | - "3.2" 6 | - "3.3" 7 | - "3.4" 8 | install: 9 | - python setup.py bdist_wheel 10 | - pip install flake8 11 | - pip install dist/iso_3166_1-*.whl 12 | - pip install -e .[tests] 13 | script: 14 | - flake8 . 15 | - py.test -vv 16 | -------------------------------------------------------------------------------- /tests/iso3166_1_test.py: -------------------------------------------------------------------------------- 1 | from iso3166 import Country 2 | 3 | 4 | def test_member_name_must_be_equal_to_alpha2_code(): 5 | for member_name, e in Country.__members__.items(): 6 | assert member_name == getattr(e, 'alpha2').lower() 7 | 8 | 9 | def test_number_of_country(): 10 | assert len(Country.__members__) == 249 11 | 12 | 13 | def test_kr_is_korea_republic_of(): 14 | assert Country.kr.english_short_name == 'Korea (Republic of)' 15 | assert Country.kr.alpha3 == 'KOR' 16 | assert Country.kr.numeric == 410 17 | 18 | 19 | def test_str(): 20 | assert str(Country.kr) == 'Korea (Republic of)' 21 | assert str(Country.jp) == 'Japan' 22 | 23 | 24 | def test_repr(): 25 | assert repr(Country.kr) == "" 26 | assert repr(Country.jp) == "" 27 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ISO 3166-1 2 | ~~~~~~~~~~~~ 3 | 4 | .. image:: https://badge.fury.io/py/iso-3166-1.svg 5 | :target: http://badge.fury.io/py/iso-3166-1 6 | 7 | .. image:: https://travis-ci.org/spoqa/iso-3166-1.svg 8 | :target: https://travis-ci.org/spoqa/iso-3166-1 9 | 10 | define codes for the names of countries as a enum_ it supports python2.7, 3.3 11 | with enum34_ of course support python3.4 as well. 12 | 13 | .. _enum: https://docs.python.org/3/library/enum.html 14 | .. _enum34: https://pypi.python.org/pypi/enum34 15 | 16 | .. code-block:: 17 | 18 | >>> from iso3166 import Country 19 | >>> Country.kr 20 | 21 | >>> Country.kr.alpha2 22 | KR 23 | >>> Country.kr.alpha3 24 | KOR 25 | >>> Country.kr.numeric 26 | 410 27 | >>> Country.kr.english_short_name 28 | Korea (Republic of) 29 | 30 | 31 | Written by `Kang Hyojun`_. Distributed under Public Domain. 32 | 33 | .. _Kang Hyojun: http://github.com/admire93 34 | -------------------------------------------------------------------------------- /iso3166/__init__.py: -------------------------------------------------------------------------------- 1 | import enum 2 | import os 3 | 4 | 5 | __version__ = 0, 0, 3 6 | __all__ = 'Country', 'codes', 7 | 8 | 9 | def read_from_csv(filename, sep='|'): 10 | table = {} 11 | with open(filename, 'r') as f: 12 | for line in f: 13 | line = line.strip() 14 | codes = line.split(sep) 15 | table[codes[1]] = { 16 | 'english_short_name': codes[0], 17 | 'alpha2': codes[1], 18 | 'alpha3': codes[2], 19 | 'numeric': int(codes[3]), 20 | } 21 | return table 22 | 23 | 24 | codes = read_from_csv(os.path.join(os.path.dirname(__file__), 25 | 'table.csv')) 26 | 27 | 28 | def update_enum_dict(locals_, table): 29 | for code in table.keys(): 30 | locals_[code.lower()] = code 31 | 32 | 33 | class Country(enum.Enum): 34 | 35 | update_enum_dict(locals(), codes) 36 | 37 | @property 38 | def alpha2(self): 39 | return codes[self.value]['alpha2'] 40 | 41 | @property 42 | def alpha3(self): 43 | return codes[self.value]['alpha3'] 44 | 45 | @property 46 | def numeric(self): 47 | return codes[self.value]['numeric'] 48 | 49 | @property 50 | def english_short_name(self): 51 | return codes[self.value]['english_short_name'] 52 | 53 | def __str__(self): 54 | return self.english_short_name 55 | 56 | def __repr__(self): 57 | cls = type(self) 58 | typename = getattr(cls, '__qualname__', cls.__name__) 59 | return '<%s.%s.%s %r>' % ( 60 | cls.__module__, typename, self.name, 61 | self.english_short_name 62 | ) 63 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | import ast 4 | import sys 5 | 6 | 7 | def readme(): 8 | try: 9 | f = open('README.rst') 10 | except IOError: 11 | return 12 | try: 13 | return f.read() 14 | finally: 15 | f.close() 16 | 17 | 18 | def get_version(): 19 | filename = 'iso3166/__init__.py' 20 | with open(filename, 'r') as f: 21 | tree = ast.parse(f.read(), filename) 22 | for node in tree.body: 23 | if (isinstance(node, ast.Assign) and 24 | node.targets[0].id == '__version__'): 25 | version = ast.literal_eval(node.value) 26 | if isinstance(version, tuple): 27 | version = '.'.join([str(x) for x in version]) 28 | return version 29 | 30 | 31 | tests_require = [ 32 | 'pytest >= 2.7.0', 33 | 'tox >= 2.1.1', 34 | ] 35 | 36 | 37 | def get_install_requirements(): 38 | install_requires = ['setuptools'] 39 | if 'bdist_wheel' not in sys.argv and sys.version_info < (3, 4): 40 | install_requires.append('enum34') 41 | return install_requires 42 | 43 | 44 | def get_extras_require(): 45 | """Generate conditional requirements with environ marker.""" 46 | for pyversion in '2.5', '2.6', '2.7', '3.2', '3.3': 47 | yield ':python_version==' + repr(pyversion), ['enum34'] 48 | 49 | 50 | setup( 51 | name='iso-3166-1', 52 | version=get_version(), 53 | description='ISO 3361-1 Country code package for Python', 54 | long_description=readme(), 55 | license='Public Domain', 56 | author='Kang Hyojun', 57 | author_email='ed' '@' 'spoqa.com', 58 | packages=find_packages(), 59 | package_data={'iso3166': ['table.csv']}, 60 | url='http://github.com/spoqa/iso-3166-1', 61 | keywords='internationalization i18n country iso3166', 62 | install_requires=get_install_requirements(), 63 | extras_require=dict( 64 | get_extras_require(), 65 | tests=tests_require 66 | ), 67 | tests_require=tests_require, 68 | classifiers=[ 69 | 'Development Status :: 1 - Planning', 70 | 'Intended Audience :: Developers', 71 | 'License :: Public Domain', 72 | 'Operating System :: OS Independent', 73 | 'Programming Language :: Python :: 2.5', 74 | 'Programming Language :: Python :: 2.6', 75 | 'Programming Language :: Python :: 2.7', 76 | 'Programming Language :: Python :: 3.2', 77 | 'Programming Language :: Python :: 3.3', 78 | 'Programming Language :: Python :: 3.4', 79 | 'Programming Language :: Python :: 3.5', 80 | 'Programming Language :: Python :: Implementation :: CPython', 81 | 'Programming Language :: Python :: Implementation :: PyPy', 82 | 'Programming Language :: Python :: Implementation :: Stackless', 83 | 'Topic :: Software Development :: Internationalization', 84 | ] 85 | ) 86 | -------------------------------------------------------------------------------- /iso3166/table.csv: -------------------------------------------------------------------------------- 1 | Afghanistan|AF|AFG|004 2 | Åland Islands|AX|ALA|248 3 | Albania|AL|ALB|008 4 | Algeria|DZ|DZA|012 5 | American Samoa|AS|ASM|016 6 | Andorra|AD|AND|020 7 | Angola|AO|AGO|024 8 | Anguilla|AI|AIA|660 9 | Antarctica|AQ|ATA|010 10 | Antigua and Barbuda|AG|ATG|028 11 | Argentina|AR|ARG|032 12 | Armenia|AM|ARM|051 13 | Aruba|AW|ABW|533 14 | Australia|AU|AUS|036 15 | Austria|AT|AUT|040 16 | Azerbaijan|AZ|AZE|031 17 | Bahamas|BS|BHS|044 18 | Bahrain|BH|BHR|048 19 | Bangladesh|BD|BGD|050 20 | Barbados|BB|BRB|052 21 | Belarus|BY|BLR|112 22 | Belgium|BE|BEL|056 23 | Belize|BZ|BLZ|084 24 | Benin|BJ|BEN|204 25 | Bermuda|BM|BMU|060 26 | Bhutan|BT|BTN|064 27 | Bolivia (Plurinational State of)|BO|BOL|068 28 | Bonaire, Sint Eustatius and Saba|BQ|BES|535 29 | Bosnia and Herzegovina|BA|BIH|070 30 | Botswana|BW|BWA|072 31 | Bouvet Island|BV|BVT|074 32 | Brazil|BR|BRA|076 33 | British Indian Ocean Territory|IO|IOT|086 34 | Brunei Darussalam|BN|BRN|096 35 | Bulgaria|BG|BGR|100 36 | Burkina Faso|BF|BFA|854 37 | Burundi|BI|BDI|108 38 | Cambodia|KH|KHM|116 39 | Cameroon|CM|CMR|120 40 | Canada|CA|CAN|124 41 | Cabo Verde|CV|CPV|132 42 | Cayman Islands|KY|CYM|136 43 | Central African Republic|CF|CAF|140 44 | Chad|TD|TCD|148 45 | Chile|CL|CHL|152 46 | China|CN|CHN|156 47 | Christmas Island|CX|CXR|162 48 | Cocos (Keeling) Islands|CC|CCK|166 49 | Colombia|CO|COL|170 50 | Comoros|KM|COM|174 51 | Congo|CG|COG|178 52 | Congo (Democratic Republic of the)|CD|COD|180 53 | Cook Islands|CK|COK|184 54 | Costa Rica|CR|CRI|188 55 | Côte d'Ivoire|CI|CIV|384 56 | Croatia|HR|HRV|191 57 | Cuba|CU|CUB|192 58 | Curaçao|CW|CUW|531 59 | Cyprus|CY|CYP|196 60 | Czech Republic|CZ|CZE|203 61 | Denmark|DK|DNK|208 62 | Djibouti|DJ|DJI|262 63 | Dominica|DM|DMA|212 64 | Dominican Republic|DO|DOM|214 65 | Ecuador|EC|ECU|218 66 | Egypt|EG|EGY|818 67 | El Salvador|SV|SLV|222 68 | Equatorial Guinea|GQ|GNQ|226 69 | Eritrea|ER|ERI|232 70 | Estonia|EE|EST|233 71 | Ethiopia|ET|ETH|231 72 | Falkland Islands (Malvinas)|FK|FLK|238 73 | Faroe Islands|FO|FRO|234 74 | Fiji|FJ|FJI|242 75 | Finland|FI|FIN|246 76 | France|FR|FRA|250 77 | French Guiana|GF|GUF|254 78 | French Polynesia|PF|PYF|258 79 | French Southern Territories|TF|ATF|260 80 | Gabon|GA|GAB|266 81 | Gambia|GM|GMB|270 82 | Georgia|GE|GEO|268 83 | Germany|DE|DEU|276 84 | Ghana|GH|GHA|288 85 | Gibraltar|GI|GIB|292 86 | Greece|GR|GRC|300 87 | Greenland|GL|GRL|304 88 | Grenada|GD|GRD|308 89 | Guadeloupe|GP|GLP|312 90 | Guam|GU|GUM|316 91 | Guatemala|GT|GTM|320 92 | Guernsey|GG|GGY|831 93 | Guinea|GN|GIN|324 94 | Guinea-Bissau|GW|GNB|624 95 | Guyana|GY|GUY|328 96 | Haiti|HT|HTI|332 97 | Heard Island and McDonald Islands|HM|HMD|334 98 | Holy See|VA|VAT|336 99 | Honduras|HN|HND|340 100 | Hong Kong|HK|HKG|344 101 | Hungary|HU|HUN|348 102 | Iceland|IS|ISL|352 103 | India|IN|IND|356 104 | Indonesia|ID|IDN|360 105 | Iran (Islamic Republic of)|IR|IRN|364 106 | Iraq|IQ|IRQ|368 107 | Ireland|IE|IRL|372 108 | Isle of Man|IM|IMN|833 109 | Israel|IL|ISR|376 110 | Italy|IT|ITA|380 111 | Jamaica|JM|JAM|388 112 | Japan|JP|JPN|392 113 | Jersey|JE|JEY|832 114 | Jordan|JO|JOR|400 115 | Kazakhstan|KZ|KAZ|398 116 | Kenya|KE|KEN|404 117 | Kiribati|KI|KIR|296 118 | Korea (Democratic People's Republic of)|KP|PRK|408 119 | Korea (Republic of)|KR|KOR|410 120 | Kuwait|KW|KWT|414 121 | Kyrgyzstan|KG|KGZ|417 122 | Lao People's Democratic Republic|LA|LAO|418 123 | Latvia|LV|LVA|428 124 | Lebanon|LB|LBN|422 125 | Lesotho|LS|LSO|426 126 | Liberia|LR|LBR|430 127 | Libya|LY|LBY|434 128 | Liechtenstein|LI|LIE|438 129 | Lithuania|LT|LTU|440 130 | Luxembourg|LU|LUX|442 131 | Macao|MO|MAC|446 132 | Macedonia (the former Yugoslav Republic of)|MK|MKD|807 133 | Madagascar|MG|MDG|450 134 | Malawi|MW|MWI|454 135 | Malaysia|MY|MYS|458 136 | Maldives|MV|MDV|462 137 | Mali|ML|MLI|466 138 | Malta|MT|MLT|470 139 | Marshall Islands|MH|MHL|584 140 | Martinique|MQ|MTQ|474 141 | Mauritania|MR|MRT|478 142 | Mauritius|MU|MUS|480 143 | Mayotte|YT|MYT|175 144 | Mexico|MX|MEX|484 145 | Micronesia (Federated States of)|FM|FSM|583 146 | Moldova (Republic of)|MD|MDA|498 147 | Monaco|MC|MCO|492 148 | Mongolia|MN|MNG|496 149 | Montenegro|ME|MNE|499 150 | Montserrat|MS|MSR|500 151 | Morocco|MA|MAR|504 152 | Mozambique|MZ|MOZ|508 153 | Myanmar|MM|MMR|104 154 | Namibia|NA|NAM|516 155 | Nauru|NR|NRU|520 156 | Nepal|NP|NPL|524 157 | Netherlands|NL|NLD|528 158 | New Caledonia|NC|NCL|540 159 | New Zealand|NZ|NZL|554 160 | Nicaragua|NI|NIC|558 161 | Niger|NE|NER|562 162 | Nigeria|NG|NGA|566 163 | Niue|NU|NIU|570 164 | Norfolk Island|NF|NFK|574 165 | Northern Mariana Islands|MP|MNP|580 166 | Norway|NO|NOR|578 167 | Oman|OM|OMN|512 168 | Pakistan|PK|PAK|586 169 | Palau|PW|PLW|585 170 | Palestine, State of|PS|PSE|275 171 | Panama|PA|PAN|591 172 | Papua New Guinea|PG|PNG|598 173 | Paraguay|PY|PRY|600 174 | Peru|PE|PER|604 175 | Philippines|PH|PHL|608 176 | Pitcairn|PN|PCN|612 177 | Poland|PL|POL|616 178 | Portugal|PT|PRT|620 179 | Puerto Rico|PR|PRI|630 180 | Qatar|QA|QAT|634 181 | Réunion|RE|REU|638 182 | Romania|RO|ROU|642 183 | Russian Federation|RU|RUS|643 184 | Rwanda|RW|RWA|646 185 | Saint Barthélemy|BL|BLM|652 186 | Saint Helena, Ascension and Tristan da Cunha|SH|SHN|654 187 | Saint Kitts and Nevis|KN|KNA|659 188 | Saint Lucia|LC|LCA|662 189 | Saint Martin (French part)|MF|MAF|663 190 | Saint Pierre and Miquelon|PM|SPM|666 191 | Saint Vincent and the Grenadines|VC|VCT|670 192 | Samoa|WS|WSM|882 193 | San Marino|SM|SMR|674 194 | Sao Tome and Principe|ST|STP|678 195 | Saudi Arabia|SA|SAU|682 196 | Senegal|SN|SEN|686 197 | Serbia|RS|SRB|688 198 | Seychelles|SC|SYC|690 199 | Sierra Leone|SL|SLE|694 200 | Singapore|SG|SGP|702 201 | Sint Maarten (Dutch part)|SX|SXM|534 202 | Slovakia|SK|SVK|703 203 | Slovenia|SI|SVN|705 204 | Solomon Islands|SB|SLB|090 205 | Somalia|SO|SOM|706 206 | South Africa|ZA|ZAF|710 207 | South Georgia and the South Sandwich Islands|GS|SGS|239 208 | South Sudan|SS|SSD|728 209 | Spain|ES|ESP|724 210 | Sri Lanka|LK|LKA|144 211 | Sudan|SD|SDN|729 212 | Suriname|SR|SUR|740 213 | Svalbard and Jan Mayen|SJ|SJM|744 214 | Swaziland|SZ|SWZ|748 215 | Sweden|SE|SWE|752 216 | Switzerland|CH|CHE|756 217 | Syrian Arab Republic|SY|SYR|760 218 | Taiwan, Province of China|TW|TWN|158 219 | Tajikistan|TJ|TJK|762 220 | Tanzania, United Republic of|TZ|TZA|834 221 | Thailand|TH|THA|764 222 | Timor-Leste|TL|TLS|626 223 | Togo|TG|TGO|768 224 | Tokelau|TK|TKL|772 225 | Tonga|TO|TON|776 226 | Trinidad and Tobago|TT|TTO|780 227 | Tunisia|TN|TUN|788 228 | Turkey|TR|TUR|792 229 | Turkmenistan|TM|TKM|795 230 | Turks and Caicos Islands|TC|TCA|796 231 | Tuvalu|TV|TUV|798 232 | Uganda|UG|UGA|800 233 | Ukraine|UA|UKR|804 234 | United Arab Emirates|AE|ARE|784 235 | United Kingdom of Great Britain and Northern Ireland|GB|GBR|826 236 | United States of America|US|USA|840 237 | United States Minor Outlying Islands|UM|UMI|581 238 | Uruguay|UY|URY|858 239 | Uzbekistan|UZ|UZB|860 240 | Vanuatu|VU|VUT|548 241 | Venezuela (Bolivarian Republic of)|VE|VEN|862 242 | Viet Nam|VN|VNM|704 243 | Virgin Islands (British)|VG|VGB|092 244 | Virgin Islands (U.S.)|VI|VIR|850 245 | Wallis and Futuna|WF|WLF|876 246 | Western Sahara|EH|ESH|732 247 | Yemen|YE|YEM|887 248 | Zambia|ZM|ZMB|894 249 | Zimbabwe|ZW|ZWE|716 250 | --------------------------------------------------------------------------------