Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Get country by unambiguous name substring #28

Open
csindle opened this issue Oct 12, 2021 · 0 comments
Open

Enhancement: Get country by unambiguous name substring #28

csindle opened this issue Oct 12, 2021 · 0 comments

Comments

@csindle
Copy link

csindle commented Oct 12, 2021

Get the unambiguous matching Country from a partial name.

E.g.

from iso3166 import countries

>>> countries.get('Bolivia')
Country(name='Bolivia, Plurinational State of', alpha2='BO', alpha3='BOL', numeric='068', apolitical_name='Bolivia, Plurinational State of')

>>> iso3166.countries.get("Moldova")
Country(name='Moldova, Republic of', alpha2='MD', alpha3='MDA', numeric='498', apolitical_name='Moldova, Republic of')

More cases that would now return the expected Country:

  • United Kingdom
  • Russia
  • Syria
  • Iran
  • Macedonia (Actually "North Macedonia", so simple prefix /.startswith would not suffice)

This enhancement partially solves #21, though not the specific "South Korea" case.

Independent code to explain behaviour:

from iso3166 import countries, Country


def sub_get(partial_name: str) -> Country or None:
    """
    Get the single matching Country from a partial name.
    partial_name:  The country name, or sub-string thereof, to find.
    Return:  None, or the fuzzy matching country name.
    """
    name = partial_name.lower()
    country = None
    for key in INDEX:
        if name in key:    ###   Crux   ###
            if country is not None:
                # Ambiguous partial_name
                raise KeyError
            country = INDEX[key]

    return country


INDEX = {c.name.lower(): c for c in countries}

tests = (
    'United Kingdom', 'Iran', 'RUSSIA', 'spam', 'Macedonia',  # Passes
    'New', 'south', ' ',                                      # Raises KeyErrors
    )

for test in tests:
    print(sub_get(test))

Since all of the country name have to be searched through, this is certainly slower than a dictionary lookup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant