本文整理汇总了Python中pycountry.countries方法的典型用法代码示例。如果您正苦于以下问题:Python pycountry.countries方法的具体用法?Python pycountry.countries怎么用?Python pycountry.countries使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycountry
的用法示例。
在下文中一共展示了pycountry.countries方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_table_args
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def update_table_args(self, registry, Model):
"""Return check constraints to limit the value
:param registry:
:param Model:
:return: list of checkConstraint
"""
if self.encrypt_key:
# dont add constraint because the state is crypted and nobody
# can add new entry
return []
if sgdb_in(registry.engine, ['MariaDB', 'MsSQL']):
# No Check constraint in MariaDB
return []
enum = [country.alpha_3 for country in pycountry.countries]
constraint = """"%s" in ('%s')""" % (self.fieldname, "', '".join(enum))
enum.sort()
key = md5()
key.update(str(enum).encode('utf-8'))
name = self.fieldname + '_' + key.hexdigest() + '_types'
return [CheckConstraint(constraint, name=name)]
示例2: test_lookup
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_lookup():
c = pycountry.countries
g = c.get(alpha_2='DE')
assert g == c.lookup('de')
assert g == c.lookup('DEU')
assert g == c.lookup('276')
assert g == c.lookup('germany')
assert g == c.lookup('Federal Republic of Germany')
# try a generated field
bqaq = pycountry.historic_countries.get(alpha_4='BQAQ')
assert bqaq == pycountry.historic_countries.lookup('atb')
german = pycountry.languages.get(alpha_2='de')
assert german == pycountry.languages.lookup('De')
euro = pycountry.currencies.get(alpha_3='EUR')
assert euro == pycountry.currencies.lookup('euro')
latin = pycountry.scripts.get(name='Latin')
assert latin == pycountry.scripts.lookup('latn')
al_bu = pycountry.subdivisions.get(code='AL-BU')
assert al_bu == pycountry.subdivisions.lookup('al-bu')
with pytest.raises(LookupError):
pycountry.countries.lookup('bogus country')
with pytest.raises(LookupError):
pycountry.countries.lookup(12345)
示例3: countries
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def countries(output):
"""Generate JSON file containing all countries."""
import pycountry
results = []
for country in pycountry.countries:
results.append({
"type": "country",
"key": country.alpha_2,
"text": "{} ({})".format(country.name, country.alpha_2),
})
with open(output, "w+") as f:
json.dump(results, f, sort_keys=True, indent=2)
click.echo("stored {} countries in {}".format(len(results), output))
示例4: make_safesearch_config_file
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def make_safesearch_config_file(config_file, opendns):
new_config = {"port": 53, "host": "127.0.0.1", "rules": []}
if opendns:
servers = ', '.join(opendns_servers)
else:
# use google servers
servers = ', '.join(google_servers)
import pycountry
import json
rule_google = "cname ^(?:www.google.)(?:{}) to forcesafesearch.google.com using " + servers
rule_youtube = "cname ^(?:www.youtube.|m.youtube.|youtubei.googleapis.|youtube.googleapis.|www.youtube-nocookie.)(?:{}) " \
"to restrict.youtube.com using " + servers
country_names = [country.alpha2.lower() for country in pycountry.countries]
country_names.extend(second_level_domains)
country_names_re = '|'.join(country_names)
new_config['rules'].append(rule_google.format(country_names_re))
new_config['rules'].append(rule_youtube.format(country_names_re))
new_config['rules'].append("resolve ^(.*) using " + servers)
logger.debug("new safesearch parental control config: {}".format(new_config))
g = open(config_file, 'w+')
json.dump(new_config, g)
g.close()
logger.debug("finished writing new safesearch parental control to {}".format(config_file))
示例5: process_result_value
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def process_result_value(self, value, dialect):
if value:
return pycountry.countries.get(alpha_3=value)
return value
示例6: _coerce
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def _coerce(self, value):
if value is not None and not isinstance(value, self.python_type):
return pycountry.countries.get(alpha_3=value)
return value
示例7: __init__
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def __init__(self, mode='alpha_2', *args, **kwargs):
self.mode = mode
if pycountry is None:
raise FieldException(
"'pycountry' package is required for use 'CountryType'")
self.choices = {getattr(country, mode): country.name
for country in pycountry.countries}
super(Country, self).__init__(*args, **kwargs)
示例8: setter_format_value
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def setter_format_value(self, value):
"""Return formatted country value
:param value:
:return:
"""
if value and not isinstance(value, self.sqlalchemy_type.python_type):
value = pycountry.countries.get(
**{
self.mode: value,
'default': pycountry.countries.lookup(value)
})
return value
示例9: test_country_list
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_country_list():
assert len(pycountry.countries) == 249
assert isinstance(list(pycountry.countries)[0], pycountry.db.Data)
示例10: test_country_fuzzy_search
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_country_fuzzy_search():
results = pycountry.countries.search_fuzzy(u'England')
assert len(results) == 1
assert results[0] == pycountry.countries.get(alpha_2='GB')
# Match alternative names exactly and thus GB ends up with Wales
# before Australia.
results = pycountry.countries.search_fuzzy(u'Wales')
assert len(results) == 2
assert results[0] == pycountry.countries.get(alpha_2='GB')
assert results[1] == pycountry.countries.get(alpha_2='AU')
# Match with accents removed, first a country with a partial match in the
# country name, then a country with multiple subdivision partial matches,
# and then a country with a single subdivision match.
results = pycountry.countries.search_fuzzy(u'Cote')
assert len(results) == 3
assert results[0] == pycountry.countries.get(alpha_2='CI')
assert results[1] == pycountry.countries.get(alpha_2='FR')
assert results[2] == pycountry.countries.get(alpha_2='HN')
# A somewhat carefully balanced point system allows for a (bias-based)
# graceful sorting of common substrings being used in multiple matches:
results = pycountry.countries.search_fuzzy(u'New')
assert results[0] == pycountry.countries.get(alpha_2='NC')
assert results[1] == pycountry.countries.get(alpha_2='NZ')
assert results[2] == pycountry.countries.get(alpha_2='PG')
assert results[3] == pycountry.countries.get(alpha_2='GB')
assert results[4] == pycountry.countries.get(alpha_2='US')
assert results[5] == pycountry.countries.get(alpha_2='CA')
assert results[6] == pycountry.countries.get(alpha_2='AU')
assert results[7] == pycountry.countries.get(alpha_2='MH')
# bug #34, likely about capitalization that was broken
results = pycountry.countries.search_fuzzy(u'united states of america')
assert len(results) == 1
assert results[0] == pycountry.countries.get(alpha_2='US')
示例11: test_germany_has_all_attributes
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_germany_has_all_attributes():
germany = pycountry.countries.get(alpha_2='DE')
assert germany.alpha_2 == u'DE'
assert germany.alpha_3 == u'DEU'
assert germany.numeric == u'276'
assert germany.name == u'Germany'
assert germany.official_name == u'Federal Republic of Germany'
示例12: test_subdivisions_directly_accessible
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_subdivisions_directly_accessible():
assert len(pycountry.subdivisions) == 4883
assert isinstance(list(pycountry.subdivisions)[0], pycountry.db.Data)
de_st = pycountry.subdivisions.get(code='DE-ST')
assert de_st.code == u'DE-ST'
assert de_st.name == u'Sachsen-Anhalt'
assert de_st.type == u'State'
assert de_st.parent is None
assert de_st.parent_code is None
assert de_st.country is pycountry.countries.get(alpha_2='DE')
示例13: test_repr
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_repr():
assert re.match("Country\\(alpha_2=u?'DE', "
"alpha_3=u?'DEU', "
"name=u?'Germany', "
"numeric=u?'276', "
"official_name=u?'Federal Republic of Germany'\\)",
repr(pycountry.countries.get(alpha_2='DE')))
示例14: test_get
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_get():
c = pycountry.countries
with pytest.raises(TypeError):
c.get(alpha_2='DE', alpha_3='DEU')
assert c.get(alpha_2='DE') == c.get(alpha_3='DEU')
assert c.get(alpha_2='Foo') is None
tracer = object()
assert c.get(alpha_2='Foo', default=tracer) is tracer
示例15: test_lookup
# 需要导入模块: import pycountry [as 别名]
# 或者: from pycountry import countries [as 别名]
def test_lookup():
c = pycountry.countries
g = c.get(alpha_2='DE')
assert g == c.get(alpha_2='de')
assert g == c.lookup('de')
assert g == c.lookup('DEU')
assert g == c.lookup('276')
assert g == c.lookup('germany')
assert g == c.lookup('Federal Republic of Germany')
# try a generated field
bqaq = pycountry.historic_countries.get(alpha_4='BQAQ')
assert bqaq == pycountry.historic_countries.lookup('atb')
german = pycountry.languages.get(alpha_2='de')
assert german == pycountry.languages.lookup('De')
euro = pycountry.currencies.get(alpha_3='EUR')
assert euro == pycountry.currencies.lookup('euro')
latin = pycountry.scripts.get(name='Latin')
assert latin == pycountry.scripts.lookup('latn')
al_bu = pycountry.subdivisions.get(code='AL-BU')
assert al_bu == pycountry.subdivisions.lookup('al-bu')
with pytest.raises(LookupError):
pycountry.countries.lookup('bogus country')
with pytest.raises(LookupError):
pycountry.countries.lookup(12345)
with pytest.raises(LookupError):
pycountry.countries.get(alpha_2=12345)