Skip to main content

A package to create vocabularies based on CSV files.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

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('example')
>>> samples = z3c.csvvocabulary.CSVVocabulary(csvfile, exampleDomain)
>>> term1 = samples.getTerm('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.

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)]}

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

z3c.csvvocabulary-1.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

z3c.csvvocabulary-1.0.0-py2.4.egg (10.8 kB view details)

Uploaded Egg

File details

Details for the file z3c.csvvocabulary-1.0.0.tar.gz.

File metadata

File hashes

Hashes for z3c.csvvocabulary-1.0.0.tar.gz
Algorithm Hash digest
SHA256 82b4687097a81448a5ba074f66271922d3b0388501e0477f8227e2ec57424db1
MD5 28769266863e3cd00e35ed692f6f1e85
BLAKE2b-256 012a2e1de9068692f32a6fb427df646ea3d346f4bb59c5d685445863c95a97b5

See more details on using hashes here.

File details

Details for the file z3c.csvvocabulary-1.0.0-py2.4.egg.

File metadata

File hashes

Hashes for z3c.csvvocabulary-1.0.0-py2.4.egg
Algorithm Hash digest
SHA256 b357281cc15467474eb6e3629783709e28d550d3617815ca4062245bb75c5f96
MD5 b69c97ff5a17d4d427063f90ff2bb527
BLAKE2b-256 fc18d3f22a957cb1a006d49e919a620cd204882ed6f6ba45c2caaeee009e8cfd

See more details on using hashes here.

Supported by

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