From fb4cdcf403707f1d57a07782d0b9a5f88c1d9dc1 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Tue, 19 Nov 2024 15:44:33 +0100 Subject: [PATCH] implemented silent mode --- pycountry_wrapper/__init__.py | 26 +++++++++++++++++++++++++- pycountry_wrapper/__main__.py | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pycountry_wrapper/__init__.py b/pycountry_wrapper/__init__.py index b94f331..5c983d9 100644 --- a/pycountry_wrapper/__init__.py +++ b/pycountry_wrapper/__init__.py @@ -15,7 +15,9 @@ class Country: If the country couldn't be found, it raises the pycountry_wrapper.CountryDoesNotExist exception. """ - def __init__(self, country: str = None, pycountry_object = None): + def __new__(cls, country: str = None, pycountry_object = None, silent: bool = False): + self = super().__new__(cls) + if country is not None: # auto detect if alpha_2 or alpha_3 if len(country) == 2: @@ -24,9 +26,31 @@ class Country: pycountry_object = pycountry.countries.get(alpha_3=country.upper()) if pycountry_object is None: + if silent: + return None raise CountryDoesNotExist() self.pycountry_object = pycountry_object + return self + + def __init__(self, country: str = None, pycountry_object = None, silent: bool = False) -> None: ... + + """ + def __init__(self, ): + if country is not None: + # auto detect if alpha_2 or alpha_3 + if len(country) == 2: + pycountry_object = pycountry.countries.get(alpha_2=country.upper()) + elif len(country) == 3: + pycountry_object = pycountry.countries.get(alpha_3=country.upper()) + + if pycountry_object is None and not silent: + raise CountryDoesNotExist() + + self.pycountry_object = pycountry_object + + print("init") + """ @classmethod def from_alpha_2(cls, alpha_2: str) -> Country: diff --git a/pycountry_wrapper/__main__.py b/pycountry_wrapper/__main__.py index c4002b5..78b0945 100644 --- a/pycountry_wrapper/__main__.py +++ b/pycountry_wrapper/__main__.py @@ -5,4 +5,4 @@ from . import Country def cli(): print(f"Running {__name__} version {__version__} from __main__.py") - print(Country("DEU")) + print(Country(country="DE")) \ No newline at end of file