Skip to main content

Zope 3 sources for pycountry databases

Project description

This package lets you use the pycountry database within 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 the pycountry product. provides Zope 3 sources for the pycountry databases. You can use it e.g. to get a zope.schema.Choice field with all iso 3166 countries.

>>> import
>>> import
>>> import zope.schema

ISO 3166 Countries

To get a list of ISO 3166 countries in a webform, you can use the zope.schema.Choice field and provide the as source:

>>> countries_field = zope.schema.Choice(title=u'Country',
>>> countries_field
<zope.schema._field.Choice object at 0x...>
>>> countries = iter(countries_field.source)

The sourcefactory returns Country objects as values, which use the values from pycountry:

>>> afghanistan =
>>> afghanistan
< object at 0x...>

Calling the next() method again returns the next country from the source:

>>> islands =
u'\xc5land Islands'

There are all information available, which you can get from pycountry:

>>> afghanistan.alpha2
>>> afghanistan.alpha3
>>> afghanistan.numeric
>>> afghanistan.official_name
'Islamic Republic of Afghanistan'

To smaller the amount of results you can provide a list or tuple of countries you like to have in your source:

>>> countries = iter(['DE', 'US']))
u'United States'
Traceback (most recent call last):

Please note, that the result items are sorted by alpha2 code. Please also note, that you can provide alpha3 and numeric codes and names resp. official_names to smaller the amount of result items, too:

>>> len(list(
>>> len(list(['DE', 'US', 'GB'])))
>>> len(list(['DEU', 'USA'])))
>>> len(list(['276', ])))
>>> countries_list = ['Germany', 'Italy', 'Poland', 'France']
>>> len(list(

Providing codes, which are not present, does not results in an exception but in an empty list:

>>> len(list(['Berlin', 'Paris'])))

ISO 15924 Scripts

Scripts are similar to countries:

>>> scripts_field = zope.schema.Choice(title=u'Script',
>>> scripts = iter(scripts_field.source)
>>> arabic =
>>> aramaic =
u'Imperial Aramaic'

Please note, that the result items are sorted by alpha4 code. Please also note, that you can provide names and numeric codes to smaller the amount of result items, too.

>>> len(list(
>>> len(list(['Arab', 'Latn'])))
>>> len(list(['215', ])))
>>> len(list(['Arabic', 'Latin'])))

ISO 4217 Currencies

Currencies are, again, similar to the ones before:

>>> currencies_field = zope.schema.Choice(title=u'Currency',
>>> currencies = iter(currencies_field.source)
>>> dirham =
u'UAE Dirham'
>>> afghani =

Please note, that the result items are sorted by letter code. Please also note, that you can provide names and numeric codes to smaller the amount of result items, too.

>>> len(list(
>>> len(list(['ARS', 'AED', 'AFN'])))
>>> len(list(['032', '784'])))
>>> len(list(['Afghani', ])))

ISO 639 Languages

Languages are similar, too:

>>> languages_field = zope.schema.Choice(title=u'Language',
>>> languages = iter(languages_field.source)
>>> afar =
>>> abkhazian =

Please note, that the result items are sorted by bibliographic. Please also note, that you can provide alpha2 and terminology codes and names to smaller the amount of result items, too.

>>> len(list(
>>> len(list(['an', 'en', 'de'])))
>>> len(list(['eng', 'ger'])))
>>> len(list(['arg', 'abk'])))
>>> len(list(['English', 'German'])))


First we fetch a specific country:

>>> countries = list(iter(countries_field.source))
>>> germany = countries[80]

The i18n translate method translates ‘Germany’ into german:

>>> zope.i18n.translate(, target_language='de')

Translations are also operating for scripts, currencies and languages.


Countries, scripts, currenties and languages can be compared to equality. To test this, we will need another country object afghanistan, which is not the same object as retrieved before:

>>> countries = iter(countries_field.source)
>>> afghanistan =
>>> countries = iter(countries_field.source)
>>> afghanistan2 =
>>> str(afghanistan) == str(afghanistan2)

Comparing them will get the token for each and compare it:

>>> afghanistan == afghanistan2

Pickling and unpickling

It should be possible to store “proxy objects” in a database (like the ZODB). Therefore, they have to be pickleable:

>>> import StringIO
>>> import cPickle
>>> f = StringIO.StringIO('')

Pickling a country should never raise an error…

>>> cPickle.dump(afghanistan, f)

… and results in storing the token in the pickle:

>>> 'AF' in

Reading the pickle again will return the same country which was pickled before:

>>> afghanistan2 = cPickle.load(f)
>>> afghanistan2 == afghanistan


0.4 (unreleased)

  • added possibility to smaller the amount of results generated by the sourcefactory

0.3 (2008-05-21)

  • added test for comparing the returned countries to equality

  • added __reduce__ to data objects so that they can be pickled

  • added tests for pickling and unpickling data objects

0.2 (2008-05-20)

  • now returns special data objects instead of pycountry objects for a better object-oriented purpose and flexibility in handling the result

  • improved configure.zcml and added functional tests for the i18n translations

  • improved test cases in general

0.1 (2008-05-20)

  • initial release

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution (8.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page