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.
gocept.country
gocept.country 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 gocept.country
>>> import gocept.country.db
>>> 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 gocept.country.countries as source:
>>> countries_field = zope.schema.Choice(title=u'Country',
... source=gocept.country.countries)
>>> countries_field
>>> countries = iter(countries_field.source)
The gocept.country.countries sourcefactory returns Country objects as values, which use the values from pycountry:
>>> afghanistan = countries.next()
>>> afghanistan
>>> afghanistan.name
u'Afghanistan'
Calling the next() method again returns the next country from the source:
>>> islands = countries.next()
>>> islands.name
u'\xc5land Islands'
There are all information available, which you can get from pycountry:
>>> afghanistan.alpha2
AF
>>> afghanistan.alpha3
AFG
>>> afghanistan.numeric
004
>>> 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(gocept.country.CountrySource(alpha2=[DE , 'US']))
>>> countries.next().name
u'Germany'
>>> countries.next().name
u'United States'
>>> countries.next().name
Traceback (most recent call last):
...
StopIteration
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(gocept.country.CountrySource()))
246
>>> len(list(gocept.country.CountrySource(alpha2=[DE , US , 'GB'])))
3
>>> len(list(gocept.country.CountrySource(alpha3=[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
|