generated from Hazel/python-project
feat: layed out empty country class
This commit is contained in:
parent
de2ab0b8c0
commit
a9a6e78b89
@ -1,9 +1,9 @@
|
|||||||
from .country import Country, StrictCountry, EmptyCountryException
|
from .country import Country, EmptyCountry, EmptyCountryException
|
||||||
from . import config
|
from . import config
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Country",
|
"Country",
|
||||||
"StrictCountry",
|
"EmptyCountry",
|
||||||
"config",
|
"config",
|
||||||
"EmptyCountryException",
|
"EmptyCountryException",
|
||||||
]
|
]
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import Optional
|
from typing import Optional, Union
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import pycountry
|
import pycountry
|
||||||
import pycountry.db
|
import pycountry.db
|
||||||
@ -54,13 +54,13 @@ class Country:
|
|||||||
elif len(country) == 3:
|
elif len(country) == 3:
|
||||||
pycountry_object = pycountry.countries.get(alpha_3=country.upper())
|
pycountry_object = pycountry.countries.get(alpha_3=country.upper())
|
||||||
if pycountry_object is not None:
|
if pycountry_object is not None:
|
||||||
return cls(pycountry_object=pycountry_object)
|
return cls(pycountry_object=pycountry_object) # type: ignore
|
||||||
|
|
||||||
# fuzzy search if enabled
|
# fuzzy search if enabled
|
||||||
if config.allow_fuzzy_search:
|
if config.allow_fuzzy_search:
|
||||||
found_countries = pycountry.countries.search_fuzzy(country)
|
found_countries = pycountry.countries.search_fuzzy(country)
|
||||||
if len(found_countries):
|
if len(found_countries):
|
||||||
return cls(pycountry_object=found_countries[0])
|
return cls(pycountry_object=found_countries[0]) # type: ignore
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def search(cls, country: Optional[str]) -> Optional[Country]:
|
def search(cls, country: Optional[str]) -> Optional[Country]:
|
||||||
@ -76,7 +76,7 @@ class Country:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_fuzzy(cls, fuzzy: str) -> Country:
|
def from_fuzzy(cls, fuzzy: str) -> Country:
|
||||||
return cls(pycountry_object=pycountry.countries.search_fuzzy(fuzzy))
|
return cls(pycountry_object=pycountry.countries.search_fuzzy(fuzzy)) # type: ignore
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
@ -105,31 +105,18 @@ class Country:
|
|||||||
return self.pycountry_object.__repr__()
|
return self.pycountry_object.__repr__()
|
||||||
|
|
||||||
|
|
||||||
class StrictCountry(Country):
|
class EmptyCountry(Country):
|
||||||
"""
|
"""
|
||||||
This works just like Country,
|
This will be used if you don't want to use a fallback country but you still want to be able to not have None
|
||||||
but the object cant be empty
|
You can access the same attributes but they will just return None
|
||||||
"""
|
"""
|
||||||
|
def __init__(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
def __init__(self, country: Optional[str] = None, pycountry_object = None) -> None:
|
@classmethod
|
||||||
super().__init__(country=country, pycountry_object=pycountry_object, allow_empty=False)
|
def search(cls, country: Optional[str]) -> Union[Country, EmptyCountry]:
|
||||||
|
result = super().search(country)
|
||||||
|
|
||||||
@property
|
if result is None:
|
||||||
def name(self) -> str:
|
return EmptyCountry()
|
||||||
return self.pycountry_object.name
|
return result
|
||||||
|
|
||||||
@property
|
|
||||||
def alpha_2(self) -> str:
|
|
||||||
return self.pycountry_object.alpha_2
|
|
||||||
|
|
||||||
@property
|
|
||||||
def alpha_3(self) -> str:
|
|
||||||
return self.pycountry_object.alpha_3
|
|
||||||
|
|
||||||
@property
|
|
||||||
def numeric(self) -> str:
|
|
||||||
return self.pycountry_object.numeric
|
|
||||||
|
|
||||||
@property
|
|
||||||
def official_name(self) -> str:
|
|
||||||
return self.pycountry_object.official_name
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user