Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _best_guess_iso_2(c):
iso3 = _best_guess_iso_3(c)
if iso3 is None:
return None
isoc = iso3166.countries.get(iso3)
if isoc is None:
return None
iso2 = isoc.alpha2
if c.iso_a2 != "-99" and _is_iso_2_name(c.iso_a2):
assert c.iso_a2 == iso2
return iso2
lambda x: iso3166.countries.get(x).alpha3, df['code'].values))
def materialize_country_facets(cls, counts):
"""
Materialize country facet counts.
Returns:
dict: {label, value, count}
"""
facets = []
for abbr, count in counts:
country = countries.get(abbr)
if country:
facets.append(dict(
label = country.name,
value = abbr.upper(),
count = count,
))
return facets
Returns: bool
"""
a = ' '.join(text.surname_tokens)
t = ' '.join(text.title_tokens)
return (
# US states
us.states.lookup(a) or
us.states.lookup(t) or
# Countries
iso3166.countries.get(a, None) or
iso3166.countries.get(t, None)
def get(cls, country):
try:
if PYCOUNTRY:
c = countries.lookup(country)
return Country(c.alpha_2, c.alpha_3, c.numeric, c.name, getattr(c, "official_name", c.name))
else:
c = countries.get(country)
return Country(c.alpha2, c.alpha3, c.numeric, c.name, c.apolitical_name)
except (LookupError, KeyError):
raise LookupError("Invalid country code: {0}".format(country))
def is_toponym(value):
"""
Is a string the name of a US state or country?
Args:
value (str)
Returns: bool
"""
return bool(
iso3166.countries.get(value, None) or
us.states.lookup(value)
)
def country_codes(self):
"""A list of valid country codes"""
return [c.alpha2.lower() for c in countries]
def check_value(self, value):
try:
countries.get(value)
return True
except KeyError:
return False
def _is_iso_3_name(n):
if len(_iso_3_names) == 0:
for c in iso3166.countries:
_iso_3_names.add(c.alpha3)
return n in _iso_3_names