Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install z3c.csvvocabulary

How to install z3c.csvvocabulary

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install z3c.csvvocabulary
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.1.0
2.0.0Never BuiltWhy not?
1.1.0 Available View build log
Windows (64-bit)
1.1.0
2.0.0Never BuiltWhy not?
1.1.0 Available View build log
Mac OS X (10.5+)
1.1.0
2.0.0Never BuiltWhy not?
1.1.0 Available View build log
Linux (32-bit)
2.0.0 Available View build log
1.1.0 Available View build log
Linux (64-bit)
2.0.0 Available View build log
1.1.0 Available View build log
2.0.0 Available View build log
 
License
ZPL 2.1
Lastest release
version 2.0.0 on Feb 20th, 2013

This package provides a very simple vocabulary implementation using CSV files. The advantage of CSV files is that they provide an external point to specify data, which allows a non-developer to adjust the data themselves.

Detailed Documentation

CSV Vocabulary

This package provides a very simple vocabulary implementation using CSV files. The advantage of CSV files is that they provide an external point to specify data, which allows a non-developer to adjust the data themselves.

>>> import z3c.csvvocabulary
>>> import os.path
>>> path = os.path.dirname(z3c.csvvocabulary.__file__)
CSV Vocabulary

The CSV Vocabulary implementation is really just a function that creates a simple vocabulary with titled terms. There is a sample.csv file in the data directory of the testing sub-package, so let's create a vocabulary from that:

>>> csvfile = os.path.join(path, 'testing', 'data', 'sample.csv')
>>> samples = z3c.csvvocabulary.CSVVocabulary(csvfile)
>>> samples
<zope.schema.vocabulary.SimpleVocabulary object at ...>
>>> sorted([term.value for term in samples])
['value1', 'value2', 'value3', 'value4', 'value5']

Let's now look at a term:

>>> term1 = samples.getTerm('value1')
>>> term1
<zope.schema.vocabulary.SimpleTerm object at ...>

As you can see, the vocabulary automatically prefixes the value:

>>> term1.value
'value1'
>>> term1.token
'value1'
>>> term1.title
u'sample-value1'

While it looks like the title is the wrong unicode string, it is really an I18n message:

>>> type(term1.title)
<type 'zope.i18nmessageid.message.Message'>
>>> term1.title.default
u'Title 1'
>>> term1.title.domain
'zope'

Of course, it is not always acceptable to make 'zope' the domain of the message. You can specify the message factory when initializing the vocabulary:

>>> from zope.i18nmessageid import MessageFactory
>>> exampleDomain = MessageFactory(u'example')
>>> samples = z3c.csvvocabulary.CSVVocabulary(csvfile, exampleDomain)
>>> term1 = samples.getTerm(u'value1')
>>> term1.title.domain
'example'

The vocabulary is designed to work with small data sets, typically choices in user interfaces. All terms are created upon initialization, so the vocabulary does not detect updates in the csv file or loads the data when needed. But as I said, this is totally okay.

Encoding

By default the vocabulary expects the csv file to be latin1 encoded.

>>> csvfile = os.path.join(path, 'testing', 'data', 'utf-8.csv')
>>> wrongEncoding = z3c.csvvocabulary.CSVVocabulary(csvfile)
>>> wrongEncoding.getTerm('ae').title.default
u'\xc3\xa4'

If you csv file has a different encoding you can specify it explicitly:

>>> utf8Encoded = z3c.csvvocabulary.CSVVocabulary(csvfile, encoding='utf-8')
>>> term = utf8Encoded.getTerm('ae')
>>> term.title.default
u'\xe4'
CSV Message String Extraction

There is a simple function in i18nextract.py that extracts all message strings from the CSV files in a particular sub-tree. Here we just want to make sure that the function completes and some dummy data from the testing package will be used:

>>> basedir = os.path.dirname(z3c.__file__)
>>> catalog = z3c.csvvocabulary.csvStrings(path, basedir)
>>> pprint(catalog)
{u'sample-value1': [('...sample.csv', 1)],
 u'sample-value2': [('...sample.csv', 2)],
 u'sample-value3': [('...sample.csv', 3)],
 u'sample-value4': [('...sample.csv', 4)],
 u'sample-value5': [('...sample.csv', 5)],
 u'utf-8-ae': [('...utf-8.csv', 1)],
 u'utf-8-oe': [('...utf-8.csv', 2)]}
Change History
2.0.0 (2013-02-19)
  • Add Python 3.3 support.
  • Drop Python 2.4 and 2.5 support.
1.1.0 (2010/01/11)
  • enocding of csv file can now be specified CSVVocabulary(csvfile, encoding='utf-8') [fRiSi]
1.0.0
  • initial release

Subscribe to package updates

Last updated Feb 20th, 2013

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.