generated from Hazel/python-project
added automatic detection of alpha_2 and alpha_3
This commit is contained in:
parent
4fe3be34d8
commit
3cddb53809
21
README.md
21
README.md
@ -28,18 +28,23 @@ except CountryDoesNotExist:
|
|||||||
|
|
||||||
### Creating country class
|
### Creating country class
|
||||||
|
|
||||||
You can call create an instance of `pycountry_wrapper.Country` in three ways.
|
You can call create an instance of `pycountry_wrapper.Country` in multiple slightly different ways.
|
||||||
|
|
||||||
Using **ISO 3166-1** with either 2 or 3 letters:
|
The [**ISO 3166-1**](https://en.wikipedia.org/wiki/ISO_3166-1) standart can either use 2 or 3 letters (alpha_2 or alpha_3).
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from pycountry_wrapper import Country
|
from pycountry_wrapper import Country
|
||||||
|
|
||||||
|
# auto detects if alpha_2 or alpha_3
|
||||||
|
Country("DE")
|
||||||
|
Country("DEU")
|
||||||
|
|
||||||
|
# you can specify what to use, if required.
|
||||||
Country.from_alpha_2("DE")
|
Country.from_alpha_2("DE")
|
||||||
Country.from_alpha_2("DEU")
|
Country.from_alpha_3("DEU")
|
||||||
```
|
```
|
||||||
|
|
||||||
Or you can do a fuzzy search in a similar way:
|
If you want to use fuzzy search, you will have to use `Country.from_fuzzy()`.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from pycountry_wrapper import Country
|
from pycountry_wrapper import Country
|
||||||
@ -47,14 +52,6 @@ from pycountry_wrapper import Country
|
|||||||
Country.from_fuzzy("Deutschland")
|
Country.from_fuzzy("Deutschland")
|
||||||
```
|
```
|
||||||
|
|
||||||
Or you can pass a pycountry object in the constructor:
|
|
||||||
|
|
||||||
```python
|
|
||||||
import pycountry
|
|
||||||
from pycountry_wrapper import Country
|
|
||||||
|
|
||||||
Country(pycountry.countries.get(alpha_2="DE"))
|
|
||||||
```
|
|
||||||
|
|
||||||
### Accessing information
|
### Accessing information
|
||||||
|
|
||||||
|
@ -15,7 +15,14 @@ class Country:
|
|||||||
If the country couldn't be found, it raises the pycountry_wrapper.CountryDoesNotExist exception.
|
If the country couldn't be found, it raises the pycountry_wrapper.CountryDoesNotExist exception.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, pycountry_object):
|
def __init__(self, country: str = None, pycountry_object = None):
|
||||||
|
if country is not None:
|
||||||
|
# auto detect if alpha_2 or alpha_3
|
||||||
|
if len(country) == 2:
|
||||||
|
pycountry_object = pycountry.countries.get(alpha_2=country.upper())
|
||||||
|
elif len(country) == 3:
|
||||||
|
pycountry_object = pycountry.countries.get(alpha_3=country.upper())
|
||||||
|
|
||||||
if pycountry_object is None:
|
if pycountry_object is None:
|
||||||
raise CountryDoesNotExist()
|
raise CountryDoesNotExist()
|
||||||
|
|
||||||
@ -23,15 +30,15 @@ class Country:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_alpha_2(cls, alpha_2: str) -> Country:
|
def from_alpha_2(cls, alpha_2: str) -> Country:
|
||||||
return cls(pycountry.countries.get(alpha_2=alpha_2.upper()))
|
return cls(pycountry_object=pycountry.countries.get(alpha_2=alpha_2.upper()))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_alpha_3(cls, alpha_3: str) -> Country:
|
def from_alpha_3(cls, alpha_3: str) -> Country:
|
||||||
return cls(pycountry.countries.get(alpha_3=alpha_3.upper()))
|
return cls(pycountry_object=pycountry.countries.get(alpha_3=alpha_3.upper()))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_fuzzy(cls, fuzzy: str) -> Country:
|
def from_fuzzy(cls, fuzzy: str) -> Country:
|
||||||
return cls(pycountry.countries.search_fuzzy(fuzzy))
|
return cls(pycountry_object=pycountry.countries.search_fuzzy(fuzzy))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
|
@ -5,4 +5,4 @@ from . import Country
|
|||||||
def cli():
|
def cli():
|
||||||
print(f"Running {__name__} version {__version__} from __main__.py")
|
print(f"Running {__name__} version {__version__} from __main__.py")
|
||||||
|
|
||||||
print(Country.from_alpha_2("DE"))
|
print(Country("DEU"))
|
||||||
|
Loading…
Reference in New Issue
Block a user