generated from Hazel/python-project
feat: playing with new
This commit is contained in:
parent
a9a6e78b89
commit
65b0c2b8e6
@ -1,8 +1,8 @@
|
|||||||
from .__about__ import __name__, __version__
|
from .__about__ import __name__, __version__
|
||||||
from . import Country
|
from . import Country, EmptyCountry
|
||||||
|
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
print(f"Running {__name__} version {__version__} from __main__.py")
|
print(f"Running {__name__} version {__version__} from __main__.py")
|
||||||
|
|
||||||
print(Country(country="DE").name)
|
print(EmptyCountry(country="doesn't exist").name)
|
@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from typing_extensions import Self
|
||||||
import pycountry
|
import pycountry
|
||||||
import pycountry.db
|
import pycountry.db
|
||||||
|
|
||||||
@ -22,7 +23,6 @@ class Country:
|
|||||||
If the country couldn't be found, it raises a ValueError, or creates an empty object.
|
If the country couldn't be found, it raises a ValueError, or creates an empty object.
|
||||||
Empty objects return for every attribute None
|
Empty objects return for every attribute None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None) -> None:
|
def __init__(self, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None) -> None:
|
||||||
if pycountry_object is None:
|
if pycountry_object is None:
|
||||||
# search for the country string instead if the pycountry_object isn't given
|
# search for the country string instead if the pycountry_object isn't given
|
||||||
@ -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) # type: ignore
|
return pycountry_object
|
||||||
|
|
||||||
# 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]) # type: ignore
|
return found_countries[0]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def search(cls, country: Optional[str]) -> Optional[Country]:
|
def search(cls, country: Optional[str]) -> Optional[Country]:
|
||||||
@ -110,8 +110,10 @@ class EmptyCountry(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
|
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
|
||||||
You can access the same attributes but they will just return None
|
You can access the same attributes but they will just return None
|
||||||
"""
|
"""
|
||||||
def __init__(self) -> None:
|
def __new__(cls, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None):
|
||||||
pass
|
print("new", country, pycountry_object)
|
||||||
|
return super().__new__(cls)
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def search(cls, country: Optional[str]) -> Union[Country, EmptyCountry]:
|
def search(cls, country: Optional[str]) -> Union[Country, EmptyCountry]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user