From 5b724077e67ac2e97d7adcdef037873a2dabb17d Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Thu, 12 Jun 2025 14:53:57 +0200 Subject: [PATCH] feat: implemented empty country --- pycountry_wrapper/__main__.py | 12 +++++++++++- pycountry_wrapper/country.py | 24 ++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/pycountry_wrapper/__main__.py b/pycountry_wrapper/__main__.py index de19c1a..a723c72 100644 --- a/pycountry_wrapper/__main__.py +++ b/pycountry_wrapper/__main__.py @@ -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) \ No newline at end of file + country = EmptyCountry(pycountry_object=t) + print(type(country)) + print(country) + + print() + empty_country = EmptyCountry(country="zwx") + print(type(empty_country)) + print(empty_country) \ No newline at end of file diff --git a/pycountry_wrapper/country.py b/pycountry_wrapper/country.py index ec40e09..0588e5a 100644 --- a/pycountry_wrapper/country.py +++ b/pycountry_wrapper/country.py @@ -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