Package: gocept.country
Copyright (c) 2008-2015 gocept gmbh & co. kgAll Rights Reserved.This software is subject to the provisions of the Zope Public License,
Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
THIS SOFTWARE IS PROVIDED “AS IS” AND ANY AND ALL EXPRESS OR IMPLIED
WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE.gocept.countryThis package lets you use thepycountry databasewithin Zope 3.In practice, this means e.g., that you can easily get a zope.schema.Choice
field to provide a full list of iso 3166 country codes.For more information about the database please refer to thepycountry product.Contentsgocept.countryIntroductionISO 3166 CountriesISO 3166-2 Country subdivisionsContext free sourceContextual sourceISO 15924 ScriptsISO 4217 CurrenciesISO 639 LanguagesTranslationsComparisonPickling and unpicklingChanges3.0 (2023-07-14)2.1 (2019-09-30)2.0 (2017-01-21)1.0 (2015-08-05)0.6.5 (2015-08-05)0.6.4 (2008-10-21)0.6.3 (2008-10-14)0.6.2 (2008-10-13)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)0.2 (2008-05-20)0.1 (2008-05-20)ContributorsIntroductiongocept.countryprovides Zope 3 sources for the pycountry databases. You can
use it e.g. to get azope.schema.Choicefield with all ISO 3166 countries.>>> import gocept.country
>>> import gocept.country.db
>>> import zope.schemaISO 3166 CountriesTo get a list of ISO 3166 countries in a web form, you can use thezope.schema.Choicefield and provide the gocept.country.countries as
source:>>> 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)Thegocept.country.countriessource factory returns Country objects as
values, which use the values from pycountry:>>> aruba = next(countries)
>>> afghanistan = next(countries)
>>> afghanistan
<gocept.country.db.Country object at 0x...>
>>> print(afghanistan.name)
AfghanistanCallingnext()again returns the next country from the source:>>> angola = next(countries)
>>> print(angola.name)
AngolaThere are all information available, which you can get from pycountry:>>> print(afghanistan.alpha_2)
AF
>>> print(afghanistan.alpha_3)
AFG
>>> print(afghanistan.numeric)
004
>>> print(afghanistan.official_name)
Islamic Republic of AfghanistanTo reduce the amount of results you can provide a list or tuple of countries
you like to have in your source:>>> 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, USPlease note, that the result items are sorted byalpha_2code. Please also
note, that you can provide alpha_3 and numeric codes and names resp.
official_names to reduce the amount of result items, too:>>> 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)))
4Providing codes, which are not present, does not result in an exception but
in an empty list:>>> len(list(gocept.country.CountrySource(capital=['Berlin', 'Paris'])))
0ISO 3166-2 Country subdivisionsContext free sourceCountry subdivisions are similar to countries:>>> 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-03Please note, that the result items are sorted by theircode. Please
also note, that you can provide names and numeric codes to reduce the
amount of result items, too.>>> 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'])))
2Contextual sourceThere is also a contextual source for country subdivisions which
depends on a country. Let’s set up a context object first:>>> 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')The contextual source expects an adapter between the context andgocept.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)
GermanySo the source contains only the country subdivisions belonging to the
country:>>> 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, BremenChanging the country changes also the subdivisions:>>> 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')))
AargauIf the country is not set there are no subdivisions:>>> address.country = None
>>> len(list(iter(gocept.country.contextual_subdivisions(address))))
0
>>> list(iter(gocept.country.contextual_subdivisions(address)))
[]ISO 15924 ScriptsScripts are similar to countries:>>> 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))
AfakPlease note, that the result items are sorted byalpha_4code. Please also
note, that you can provide names and numeric codes to smaller the amount of
result items, too.>>> 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'])))
2ISO 4217 CurrenciesCurrencies are similar to the ones before:>>> 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))
AFNPlease note, that the result items are sorted byalpha_3code. Please also
note, that you can provide names and numeric codes to reduce the amount of
result items, too.>>> 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', ])))
1ISO 639 LanguagesLanguages are similar, too:>>> 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))
aabPlease note, that the result items are sorted byalpha_3. Please also
note, that you can provide names to reduce the amount of result items, too.>>> 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'])))
2TranslationsFirst we fetch a specific country:>>> countries = list(iter(countries_field.source))
>>> germany = [x for x in countries if x.name == u'Germany'][0]The i18n translate method translates ‘Germany’ into German:>>> print(zope.i18n.translate(germany.name, target_language='de'))
DeutschlandThere are also translations for scripts, currencies and languages.ComparisonCountries, scripts, currencies and languages can be compared to equality. To
test this, we will need another country objectafghanistan, which is not
thesameobject as retrieved before:>>> afghanistan = next(iter(gocept.country.CountrySource(alpha_2=['AF'])))
>>> afghanistan2 = next(iter(gocept.country.CountrySource(alpha_2=['AF'])))>>> str(afghanistan) == str(afghanistan2)
FalseComparing them will get the token for each and compare it:>>> afghanistan == afghanistan2
True
>>> afghanistan != afghanistan2
False
>>> afghanistan != germany
True
>>> afghanistan == germany
FalseComparing with an instance of another class always returns False:>>> afghanistan == None
False
>>> afghanistan != None
True
>>> afghanistan == object()
False
>>> afghanistan != object()
TruePickling and unpicklingIt should be possible to store “proxy objects” in a database (like the ZODB).
Therefore, they have to be pickleable:>>> from io import BytesIO
>>> import pickle
>>> f = BytesIO(b'')Pickling a country should never raise an error…>>> pickle.dump(afghanistan, f)… and results in storing the token in the pickle:>>> ignored = f.seek(0)
>>> b'AF' in f.read()
TrueReading the pickle again will return the same country which was pickled
before:>>> ignored = f.seek(0)
>>> afghanistan2 = pickle.load(f)
>>> afghanistan2 == afghanistan
True
>>> print(afghanistan2.name)
AfghanistanChanges3.0 (2023-07-14)Drop support for Python 2.7, 3.5, 3.6.Add support for Python 3.9, 3.10, 3.11.2.1 (2019-09-30)Add support for Python 3.5 up to 3.8, PyPy and PyPy3.Replace test dependency onzope.app.testingbyzope.app.wsgi[testlayer].Migrate to Github.2.0 (2017-01-21)Update topycountry >= 16.x.Drop support for Python 2.6.Bring test coverage to 100 %.Change testrunner to py.test.1.0 (2015-08-05)Update topycountry 1.12thus adding support for ISO 639 3, dropping the
old ISO 639 support.0.6.5 (2015-08-05)Move repos tohttps://bitbucket.org/gocept/gocept.country0.6.4 (2008-10-21)Bugfix: declared namespace package in setup.py0.6.3 (2008-10-14)Bugfix: added not-equal compare method for db objects0.6.2 (2008-10-13)Added security declarations for token.Bugfix in comparison of db objects, whereisinstancereturns False
for objects of the same type0.6.1 (2008-09-13)Bugfix in countextual subdivision source: parameters of some methods
where in wrong order.0.6 (2008-09-12)Added contextual country subdivision source, so country subdivisions
can depend on the country.0.5 (2008-09-11)Added support for country subdivisions.0.4.2 (2008-09-10)Added security declarations for tokens.0.4.1 (2008-09-10)Fixed bug in token comparison.0.4 (2008-06-10)added possibility to smaller the amount of results generated by the
sourcefactory0.3 (2008-05-21)added test for comparing the returned countries to equalityadded __reduce__ to data objects so that they can be pickledadded tests for pickling and unpickling data objects0.2 (2008-05-20)gocept.country now returns special data objects instead of pycountry
objects for a better object-oriented purpose and flexibility in handling the
resultimproved configure.zcml and added functional tests for the i18n translationsimproved test cases in general0.1 (2008-05-20)initial releaseContributorsMichael Howitz <mh at gocept dot com>