Zope 3的pycountry数据库源
项目描述
版权所有 (c) 2008-2015 gocept gmbh & co. kg
版权所有。
本软件受Zope公共许可证第二版(ZPL)的规定约束。应随此分发提供ZPL副本。本软件按“原样”提供,并放弃所有明示或暗示的保证,包括但不限于标题保证、适销性保证、非侵权性和针对特定目的的适用性保证。
gocept.country
此软件包允许您在Zope 3中使用pycountry数据库。
实际上,这意味着您可以轻松地获得一个zope.schema.Choice字段,提供完整的iso 3166国家代码列表。
有关数据库的更多信息,请参阅pycountry产品。
简介
gocept.country为pycountry数据库提供了Zope 3源。您可以使用它,例如,获取包含所有ISO 3166国家的zope.schema.Choice字段。
>>> import gocept.country >>> import gocept.country.db >>> import zope.schema
ISO 3166国家
要获取一个网页表单中ISO 3166国家的列表,您可以使用zope.schema.Choice字段,并使用gocept.country.countries作为源
>>> countries_field = zope.schema.Choice(title=u'Country', ... source=gocept.country.countries) >>> countries_field <zope.schema._field.Choice object at 0x...> >>> countries = iter(countries_field.source)
gocept.country.countries源工厂返回国家对象作为值,这些对象使用pycountry的值
>>> aruba = next(countries) >>> afghanistan = next(countries) >>> afghanistan <gocept.country.db.Country object at 0x...> >>> print(afghanistan.name) Afghanistan
再次调用next()将返回源中的下一个国家
>>> angola = next(countries) >>> print(angola.name) Angola
所有信息均可用,您可以从pycountry获取这些信息
>>> print(afghanistan.alpha_2) AF >>> print(afghanistan.alpha_3) AFG >>> print(afghanistan.numeric) 004 >>> print(afghanistan.official_name) Islamic Republic of Afghanistan
为了减少结果的数量,您可以提供一个您希望包含在源中的国家列表或元组
>>> countries = gocept.country.CountrySource(alpha_2=['DE', 'US']) >>> print(*[countries.factory.getTitle(x) for x in countries], sep=', ') Germany, United States >>> print(*[countries.factory.getToken(x) for x in countries], sep=', ') DE, US
请注意,结果项按alpha_2代码排序。请注意,您还可以提供alpha_3和数字代码以及相应名称来进一步减少结果项的数量
>>> len(list(gocept.country.CountrySource())) > 200 True >>> len(list(gocept.country.CountrySource(alpha_2=['DE', 'US', 'GB']))) 3 >>> len(list(gocept.country.CountrySource(alpha_3=['DEU', 'USA']))) 2 >>> len(list(gocept.country.CountrySource(numeric=['276', ]))) 1 >>> countries_list = ['Germany', 'Italy', 'Poland', 'France'] >>> len(list(gocept.country.CountrySource(name=countries_list))) 4
提供不存在的代码不会引发异常,但会返回一个空列表
>>> len(list(gocept.country.CountrySource(capital=['Berlin', 'Paris']))) 0
ISO 3166-2国家子划分
上下文无关源
国家子划分类似于国家
>>> subdivisions_field = zope.schema.Choice( ... title=u'Country subdivisions', source=gocept.country.subdivisions) >>> subdivisions = iter(subdivisions_field.source)>>> canillo = next(subdivisions) >>> print(canillo.name) Canillo >>> print(canillo.code) AD-02>>> encamp = next(subdivisions) >>> print(encamp.name) Encamp >>> print(encamp.code) AD-03 >>> print(gocept.country.subdivisions.factory.getToken(encamp)) AD-03
请注意,结果项按其code排序。请注意,您还可以提供名称和数字代码来减少结果项的数量
>>> len(list(gocept.country.SubdivisionSource())) > 4000 True >>> len(list(gocept.country.SubdivisionSource(code=['DE-ST', 'US-WA']))) 2 >>> len(list(gocept.country.SubdivisionSource(country_code=['DE']))) 16 >>> print(*[x.name ... for x in gocept.country.SubdivisionSource(country_code=['DE'])][3:5], ... sep=', ') Bayern, Bremen >>> len(list(gocept.country.SubdivisionSource(name=[u'Bayern', u'Bremen']))) 2
上下文源
还有一个依赖于国家的上下文国家子划分源。首先让我们设置一个上下文对象
>>> import zope.interface >>> class IAddress(zope.interface.Interface): ... country = zope.interface.Attribute("The country of the address.") ... subdivision = zope.schema.Choice( ... title=u'Country subdivisions', ... source=gocept.country.contextual_subdivisions)>>> @zope.interface.implementer(IAddress) ... class Address(object): ... pass >>> address = Address() >>> address.country = gocept.country.db.Country('DE')
上下文源期望上下文和gocept.country.interfaces.ICountry之间的适配器
>>> import zope.component >>> import gocept.country.interfaces >>> def get_country(context): ... return context.country >>> zope.component.provideAdapter( ... get_country, (IAddress, ), gocept.country.interfaces.ICountry)>>> print(gocept.country.interfaces.ICountry(address).name) Germany
因此,源仅包含属于该国家的子划分
>>> len(list(iter(gocept.country.contextual_subdivisions(address)))) 16 >>> print(*[x.name ... for x in iter(gocept.country.contextual_subdivisions(address))][3:5], ... sep=', ') Bayern, Bremen
更改国家也会更改子划分
>>> address.country = gocept.country.db.Country('CH') >>> len(list(iter(gocept.country.contextual_subdivisions(address)))) 26 >>> print(*[x.name ... for x in iter(gocept.country.contextual_subdivisions(address))], ... sep=', ') Aargau, Appenzell Innerrhoden, ... >>> print(*[x.code ... for x in iter(gocept.country.contextual_subdivisions(address))], ... sep=', ') CH-AG, CH-AI, ... >>> print(*[gocept.country.contextual_subdivisions.factory.getToken(address, x) ... for x in iter(gocept.country.contextual_subdivisions(address))], ... sep=', ') CH-AG, CH-AI, ...>>> print(gocept.country.contextual_subdivisions.factory.getTitle( ... address, gocept.country.db.Subdivision('CH-AG'))) Aargau
如果没有设置国家,则没有子划分
>>> address.country = None >>> len(list(iter(gocept.country.contextual_subdivisions(address)))) 0 >>> list(iter(gocept.country.contextual_subdivisions(address))) []
ISO 15924脚本
脚本类似于国家
>>> scripts_field = zope.schema.Choice(title=u'Script', ... source=gocept.country.scripts) >>> scripts = iter(scripts_field.source)>>> adlam = next(scripts) >>> print(adlam.name) Adlam>>> afaka = next(scripts) >>> print(afaka.name) Afaka >>> print(gocept.country.scripts.factory.getToken(afaka)) Afak
请注意,结果项按alpha_4代码排序。请注意,您还可以提供名称和数字代码来减少结果项的数量
>>> len(list(gocept.country.ScriptSource())) > 130 True >>> len(list(gocept.country.ScriptSource(alpha_4=['Arab', 'Latn']))) 2 >>> len(list(gocept.country.ScriptSource(numeric=['215', ]))) 1 >>> len(list(gocept.country.ScriptSource(name=['Arabic', 'Latin']))) 2
ISO 4217货币
货币与之前类似
>>> currencies_field = zope.schema.Choice(title=u'Currency', ... source=gocept.country.currencies)>>> currencies = iter(currencies_field.source)>>> dirham = next(currencies) >>> print(dirham.name) UAE Dirham>>> afghani = next(currencies) >>> print(afghani.name) Afghani >>> print(gocept.country.currencies.factory.getToken(afghani)) AFN
请注意,结果项按alpha_3代码排序。请注意,您还可以提供名称和数字代码来减少结果项的数量
>>> len(list(gocept.country.CurrencySource())) >= 170 True >>> len(list(gocept.country.CurrencySource(alpha_3=['ARS', 'AED', 'AFN']))) 3 >>> len(list(gocept.country.CurrencySource(numeric=['032', '784']))) 2 >>> len(list(gocept.country.CurrencySource(name=['Afghani', ]))) 1
ISO 639语言
语言也与之前类似
>>> languages_field = zope.schema.Choice(title=u'Language', ... source=gocept.country.languages)>>> languages = iter(languages_field.source)>>> ghotuo = next(languages) >>> print(ghotuo.name) Ghotuo>>> alumu_tesu = next(languages) >>> print(alumu_tesu.name) Alumu-Tesu >>> print(gocept.country.languages.factory.getToken(alumu_tesu)) aab
请注意,结果项按alpha_3排序。请注意,您还可以提供名称来减少结果项的数量
>>> len(list(gocept.country.LanguageSource())) > 480 True >>> len(list(gocept.country.LanguageSource(alpha_3=['eng', 'deu']))) 2 >>> len(list(gocept.country.LanguageSource(name=['English', 'German']))) 2
翻译
首先,我们获取一个特定的国家
>>> countries = list(iter(countries_field.source)) >>> germany = [x for x in countries if x.name == u'Germany'][0]
i18n翻译方法将“德国”翻译成德语
>>> print(zope.i18n.translate(germany.name, target_language='de')) Deutschland
脚本、货币和语言的翻译也存在。
比较
国家、脚本、货币和语言可以进行相等性比较。为了测试这一点,我们需要另一个国家对象 afghanistan,它不是之前获取的同一个对象
>>> afghanistan = next(iter(gocept.country.CountrySource(alpha_2=['AF']))) >>> afghanistan2 = next(iter(gocept.country.CountrySource(alpha_2=['AF'])))>>> str(afghanistan) == str(afghanistan2) False
比较它们将获取每个对象的令牌并进行比较
>>> afghanistan == afghanistan2 True >>> afghanistan != afghanistan2 False >>> afghanistan != germany True >>> afghanistan == germany False
与另一个类的实例比较总是返回 False
>>> afghanistan == None False >>> afghanistan != None True >>> afghanistan == object() False >>> afghanistan != object() True
序列化和反序列化
应该在数据库中存储“代理对象”(如 ZODB)。因此,它们必须是可 picklable 的
>>> from io import BytesIO >>> import pickle >>> f = BytesIO(b'')
country 对象的 picklable 从不引发错误...
>>> pickle.dump(afghanistan, f)
…并将令牌存储在 pickle 中
>>> ignored = f.seek(0) >>> b'AF' in f.read() True
再次读取 pickle 将返回之前 picklable 的相同国家
>>> ignored = f.seek(0) >>> afghanistan2 = pickle.load(f) >>> afghanistan2 == afghanistan True >>> print(afghanistan2.name) Afghanistan
变更
3.0 (2023-07-14)
取消对 Python 2.7、3.5、3.6 的支持。
添加对 Python 3.9、3.10、3.11 的支持。
2.1 (2019-09-30)
添加对 Python 3.5 至 3.8、PyPy 和 PyPy3 的支持。
将测试依赖项从 zope.app.testing 替换为 zope.app.wsgi[testlayer]。
迁移到 Github。
2.0 (2017-01-21)
更新到 pycountry >= 16.x。
取消对 Python 2.6 的支持。
将测试覆盖率提升到 100 %。
将测试运行器更改为 py.test。
1.0 (2015-08-05)
更新到 pycountry 1.12,从而添加对 ISO 639 3 的支持,取消对旧 ISO 639 的支持。
0.6.5 (2015-08-05)
0.6.4 (2008-10-21)
错误修复:在 setup.py 中声明了命名空间包
0.6.3 (2008-10-14)
错误修复:为数据库对象添加了不等比较方法
0.6.2 (2008-10-13)
为令牌添加了安全声明。
错误修复:在比较 db 对象时,isinstance 返回 False,对于相同类型的对象
0.6.1 (2008-09-13)
错误修复:上下文细分源中的参数顺序错误。
0.6 (2008-09-12)
添加了上下文国家细分源,因此国家细分可以依赖于国家。
0.5 (2008-09-11)
添加了对国家细分的支持。
0.4.2 (2008-09-10)
为令牌添加了安全声明。
0.4.1 (2008-09-10)
修复了令牌比较中的错误。
0.4 (2008-06-10)
添加了减少源工厂生成的结果数量的可能性
0.3 (2008-05-21)
添加了比较返回国家的等性测试
为数据对象添加了 __reduce__,以便它们可以被 picklable
添加了对 picklable 和 unpicklable 数据对象的测试
0.2 (2008-05-20)
gocept.country 现在返回特殊数据对象,而不是 pycountry 对象,以实现更好的面向对象目的和处理结果的灵活性
改进了 configure.zcml 并添加了 i18n 翻译的功能测试
改进了通用测试用例
0.1 (2008-05-20)
初始发布
贡献者
Michael Howitz <mh at gocept dot com>
项目详细信息
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。
源分布
构建分布
gocept.country-3.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cc6e51a12ec4cad2b045d97571316e096099df24007b1654bd6f2514fc98fea7 |
|
MD5 | 4feb0d1196dcbc20af2b5ad3b31c441e |
|
BLAKE2b-256 | 2009ed1e40c8dc6c37782d5675bba5481015856f77119dc0b53d07a5404a239c |
gocept.country-3.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4c4ea78da313ff6e926081285bd7d0dd5896ccf9edc5d60c147b21fcae251c71 |
|
MD5 | 6d32af16b2d8ce28428cd7f88e2459ca |
|
BLAKE2b-256 | 61fed61a31fb39e59ac170d9deda2466c9523fb11e88500b3876b71abaeddf44 |