generated from Hazel/python-project
started to use a simple value error
This commit is contained in:
parent
d956ea6b24
commit
ba370e4638
@ -13,7 +13,7 @@ pip install pycountry-wrapper
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from pycountry_wrapper import Country, CountryDoesNotExist
|
from pycountry_wrapper import Country
|
||||||
|
|
||||||
germany = Country.from_alpha_2("DE")
|
germany = Country.from_alpha_2("DE")
|
||||||
print(germany)
|
print(germany)
|
||||||
@ -21,8 +21,8 @@ print(germany.name)
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
does_not_exist = Country.from_alpha_2("EN")
|
does_not_exist = Country.from_alpha_2("EN")
|
||||||
except CountryDoesNotExist:
|
except ValueError:
|
||||||
# if the country wasn't found, this exception is thrown
|
# if the country wasn't found, a ValueError is raised
|
||||||
pass
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import pycountry
|
import pycountry
|
||||||
|
|
||||||
class CountryDoesNotExist(Exception): pass
|
|
||||||
|
|
||||||
|
|
||||||
class Country:
|
class Country:
|
||||||
"""
|
"""
|
||||||
This gets countries based on the ISO 3166-1 standart.
|
This gets countries based on the ISO 3166-1 standart.
|
||||||
@ -12,7 +9,7 @@ class Country:
|
|||||||
- Country.from_alpha_2("DE")
|
- Country.from_alpha_2("DE")
|
||||||
- Country.from_alpha_3("DEU")
|
- Country.from_alpha_3("DEU")
|
||||||
|
|
||||||
If the country couldn't be found, it raises the pycountry_wrapper.CountryDoesNotExist exception.
|
If the country couldn't be found, it raises a ValueError.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __new__(cls, country: str = None, pycountry_object = None, silent: bool = False):
|
def __new__(cls, country: str = None, pycountry_object = None, silent: bool = False):
|
||||||
@ -28,7 +25,7 @@ class Country:
|
|||||||
if pycountry_object is None:
|
if pycountry_object is None:
|
||||||
if silent:
|
if silent:
|
||||||
return None
|
return None
|
||||||
raise CountryDoesNotExist()
|
raise ValueError(f"Country {country} couldn't be found")
|
||||||
|
|
||||||
self.pycountry_object = pycountry_object
|
self.pycountry_object = pycountry_object
|
||||||
return self
|
return self
|
||||||
|
Loading…
Reference in New Issue
Block a user