feat: implemented empty country

This commit is contained in:
Hazel Noack 2025-06-12 14:53:57 +02:00
parent af3fff8559
commit 5b724077e6
2 changed files with 27 additions and 9 deletions

View File

@ -1,8 +1,18 @@
from .__about__ import __name__, __version__
from . import Country, EmptyCountry
import pycountry
def cli():
print(f"Running {__name__} version {__version__} from __main__.py")
t = pycountry.countries.get(alpha_2="DE")
print(EmptyCountry(country="zwx").name)
country = EmptyCountry(pycountry_object=t)
print(type(country))
print(country)
print()
empty_country = EmptyCountry(country="zwx")
print(type(empty_country))
print(empty_country)

View File

@ -116,14 +116,22 @@ class EmptyCountry(Country):
You can access the same attributes but they will just return None
"""
def __new__(cls, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None):
print("new", country, pycountry_object)
return super().__new__(cls)
try:
return Country(country=country, pycountry_object=pycountry_object)
except EmptyCountryException:
return super().__new__(cls)
def __init__(self, *args, **kwargs) -> None:
pass
name = None # type: ignore
alpha_2 = None # type: ignore
alpha_3 = None # type: ignore
numeric = None # type: ignore
@classmethod
def search(cls, country: Optional[str]) -> Union[Country, EmptyCountry]:
result = super().search(country)
def __str__(self) -> str:
return "EmptyCountry()"
def __repr__(self) -> str:
return "EmptyCountry()"
if result is None:
return EmptyCountry()
return result