countrynames/__init__.py
def to_code(country_name: Any, fuzzy: bool = False) -> Optional[str]:
    """Given a human name for a country, return a two letter code.

    Arguments:
        ``fuzzy``: Try fuzzy matching based on Levenshtein distance.
    """
    # Lazy load country list
    if not len(COUNTRY_NAMES):
        _load_data()

    # shortcut before costly ICU stuff
    if isinstance(country_name, str):
        country_name = country_name.upper().strip()
        # Check if the input is actually an ISO code:
        if country_name in COUNTRY_NAMES.values():
            return country_name

    # Transliterate and clean up
    name = _normalize_name(country_name)
    if name is None:
        return None

    # Direct look up
    code = COUNTRY_NAMES.get(name)
    if code == "FAIL":
        return None

    # Find closest match with spelling mistakes
    if code is None and fuzzy is True:
        code = _fuzzy_search(name)
    return code
Given a human name for a country, return a two letter code.

    Arguments:
        ``fuzzy``: Try fuzzy matching based on Levenshtein distance.
    
Given a human name for a country, return a two letter code.
Given a human name for a country return a two letter code .
def to_code(country_name: Any, fuzzy: bool = False) -> Optional[str]:
    if not len(COUNTRY_NAMES):
        _load_data()
    if isinstance(country_name, str):
        country_name = country_name.upper().strip()
        if country_name in COUNTRY_NAMES.values():
            return country_name
    name = _normalize_name(country_name)
    if name is None:
        return None
    code = COUNTRY_NAMES.get(name)
    if code == "FAIL":
        return None
    if code is None and fuzzy is True:
        code = _fuzzy_search(name)
    return code
def to_code ( country_name : Any , fuzzy : bool = False ) -> Optional [ str ] : if not len ( COUNTRY_NAMES ) : _load_data ( ) if isinstance ( country_name , str ) : country_name = country_name . upper ( ) . strip ( ) if country_name in COUNTRY_NAMES . values ( ) : return country_name name = _normalize_name ( country_name ) if name is None : return None code = COUNTRY_NAMES . get ( name ) if code == "FAIL" : return None if code is None and fuzzy is True : code = _fuzzy_search ( name ) return code
Given a human name for a country, return a two letter code.
Given a human name for a country return a two letter code .
"""Given a human name for a country, return a two letter code.

    Arguments:
        ``fuzzy``: Try fuzzy matching based on Levenshtein distance.
    """ # Lazy load country list # shortcut before costly ICU stuff # Check if the input is actually an ISO code: # Transliterate and clean up # Direct look up # Find closest match with spelling mistakes
country_name
Any
fuzzy
bool
``fuzzy``
Try fuzzy matching based on Levenshtein distance.
Try fuzzy matching based on Levenshtein distance .