z3c.csvvocabulary


Namez3c.csvvocabulary JSON
Version 3.0 PyPI version JSON
download
home_pagehttps://https://github.com/zopefoundation/z3c.csvvocabulary
SummaryA package to create vocabularies based on CSV files.
upload_time2023-02-07 09:32:51
maintainer
docs_urlNone
authorZope Community
requires_python>=3.7
licenseZPL 2.1
keywords zope3 vocabulary csv
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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
  'sample-value1'

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

  >>> type(term1.title)
  <class 'zope.i18nmessageid.message.Message'>

  >>> term1.title.default
  '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.


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
  '\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
  '\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(path)

  >>> catalog = z3c.csvvocabulary.csvStrings(path, basedir)
  >>> pprint(catalog)
  {'sample-value1': [('...sample.csv', 1)],
   'sample-value2': [('...sample.csv', 2)],
   'sample-value3': [('...sample.csv', 3)],
   'sample-value4': [('...sample.csv', 4)],
   'sample-value5': [('...sample.csv', 5)],
   'utf-8-ae': [('...utf-8.csv', 1)],
   'utf-8-oe': [('...utf-8.csv', 2)]}

==============
Change History
==============

3.0 (2023-02-07)
----------------

- Add support for Python 3.7, 3.8, 3.9, 3.10, 3.11.

- Drop support for Python 2.6, 2.7, 3.3.


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

            

Raw data

            {
    "_id": null,
    "home_page": "https://https://github.com/zopefoundation/z3c.csvvocabulary",
    "name": "z3c.csvvocabulary",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "zope3 vocabulary csv",
    "author": "Zope Community",
    "author_email": "zope-dev@zope.dev",
    "download_url": "https://files.pythonhosted.org/packages/3c/99/162391b9edae58e1d14b4a27a2d0a5834c44197fcc73431c5704eabaed5c/z3c.csvvocabulary-3.0.tar.gz",
    "platform": null,
    "description": "This package provides a very simple vocabulary implementation using CSV\nfiles. The advantage of CSV files is that they provide an external point to\nspecify data, which allows a non-developer to adjust the data themselves.\n\nDetailed Documentation\n**********************\n\n==============\nCSV Vocabulary\n==============\n\nThis package provides a very simple vocabulary implementation using CSV\nfiles. The advantage of CSV files is that they provide an external point to\nspecify data, which allows a non-developer to adjust the data themselves.\n\n  >>> import z3c.csvvocabulary\n\n  >>> import os.path\n  >>> path = os.path.dirname(z3c.csvvocabulary.__file__)\n\nCSV Vocabulary\n--------------\n\nThe CSV Vocabulary implementation is really just a function that creates a\nsimple vocabulary with titled terms. There is a ``sample.csv`` file in the\n``data`` directory of the ``testing`` sub-package, so let's create a\nvocabulary from that:\n\n  >>> csvfile = os.path.join(path, 'testing', 'data', 'sample.csv')\n\n  >>> samples = z3c.csvvocabulary.CSVVocabulary(csvfile)\n  >>> samples\n  <zope.schema.vocabulary.SimpleVocabulary object at ...>\n\n  >>> sorted([term.value for term in samples])\n  ['value1', 'value2', 'value3', 'value4', 'value5']\n\nLet's now look at a term:\n\n  >>> term1 = samples.getTerm('value1')\n  >>> term1\n  <zope.schema.vocabulary.SimpleTerm object at ...>\n\nAs you can see, the vocabulary automatically prefixes the value:\n\n  >>> term1.value\n  'value1'\n\n  >>> term1.token\n  'value1'\n\n  >>> term1.title\n  'sample-value1'\n\nWhile it looks like the title is the wrong unicode string, it is really an\nI18n message:\n\n  >>> type(term1.title)\n  <class 'zope.i18nmessageid.message.Message'>\n\n  >>> term1.title.default\n  'Title 1'\n\n  >>> term1.title.domain\n  'zope'\n\nOf course, it is not always acceptable to make 'zope' the domain of the\nmessage. You can specify the message factory when initializing the vocabulary:\n\n  >>> from zope.i18nmessageid import MessageFactory\n  >>> exampleDomain = MessageFactory('example')\n\n  >>> samples = z3c.csvvocabulary.CSVVocabulary(csvfile, exampleDomain)\n  >>> term1 = samples.getTerm('value1')\n  >>> term1.title.domain\n  'example'\n\nThe vocabulary is designed to work with small data sets, typically choices in\nuser interfaces. All terms are created upon initialization, so the vocabulary\ndoes not detect updates in the csv file or loads the data when needed. But as\nI said, this is totally okay.\n\n\nEncoding\n````````\n\nBy default the vocabulary expects the csv file to be latin1 encoded.\n\n  >>> csvfile = os.path.join(path, 'testing', 'data', 'utf-8.csv')\n  >>> wrongEncoding = z3c.csvvocabulary.CSVVocabulary(csvfile)\n  >>> wrongEncoding.getTerm('ae').title.default\n  '\\xc3\\xa4'\n\nIf you csv file has a different encoding you can specify it explicitly:\n\n  >>> utf8Encoded = z3c.csvvocabulary.CSVVocabulary(csvfile, encoding='utf-8')\n  >>> term = utf8Encoded.getTerm('ae')\n  >>> term.title.default\n  '\\xe4'\n\n\nCSV Message String Extraction\n-----------------------------\n\nThere is a simple function in ``i18nextract.py`` that extracts all message\nstrings from the CSV files in a particular sub-tree. Here we just want to make\nsure that the function completes and some dummy data from the testing package\nwill be used:\n\n  >>> basedir = os.path.dirname(path)\n\n  >>> catalog = z3c.csvvocabulary.csvStrings(path, basedir)\n  >>> pprint(catalog)\n  {'sample-value1': [('...sample.csv', 1)],\n   'sample-value2': [('...sample.csv', 2)],\n   'sample-value3': [('...sample.csv', 3)],\n   'sample-value4': [('...sample.csv', 4)],\n   'sample-value5': [('...sample.csv', 5)],\n   'utf-8-ae': [('...utf-8.csv', 1)],\n   'utf-8-oe': [('...utf-8.csv', 2)]}\n\n==============\nChange History\n==============\n\n3.0 (2023-02-07)\n----------------\n\n- Add support for Python 3.7, 3.8, 3.9, 3.10, 3.11.\n\n- Drop support for Python 2.6, 2.7, 3.3.\n\n\n2.0.0 (2013-02-19)\n------------------\n\n- Add Python 3.3 support.\n\n- Drop Python 2.4 and 2.5 support.\n\n\n1.1.0 (2010/01/11)\n------------------\n\n* enocding of csv file can now be specified ``CSVVocabulary(csvfile,\n  encoding='utf-8')`` [fRiSi]\n\n1.0.0\n-----\n\n* initial release\n",
    "bugtrack_url": null,
    "license": "ZPL 2.1",
    "summary": "A package to create vocabularies based on CSV files.",
    "version": "3.0",
    "split_keywords": [
        "zope3",
        "vocabulary",
        "csv"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2d10005537445058def4a4a69374dc5ce758eade74d7d76dd738c8626652dd3a",
                "md5": "58f615c102291f09241f8c42e90cb6a0",
                "sha256": "583a1e63590ed1e4e7faee7a3fe8a37a23768c4b74ba373a1bcf792a6d4b8b4b"
            },
            "downloads": -1,
            "filename": "z3c.csvvocabulary-3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "58f615c102291f09241f8c42e90cb6a0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 9680,
            "upload_time": "2023-02-07T09:32:49",
            "upload_time_iso_8601": "2023-02-07T09:32:49.367195Z",
            "url": "https://files.pythonhosted.org/packages/2d/10/005537445058def4a4a69374dc5ce758eade74d7d76dd738c8626652dd3a/z3c.csvvocabulary-3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3c99162391b9edae58e1d14b4a27a2d0a5834c44197fcc73431c5704eabaed5c",
                "md5": "e097b36afb4c46d3fc1af09dee70d9e3",
                "sha256": "16d6d85031cdd42f19ccaafca6e6230b28118561044ba362551fbd7e672f0201"
            },
            "downloads": -1,
            "filename": "z3c.csvvocabulary-3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e097b36afb4c46d3fc1af09dee70d9e3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 8251,
            "upload_time": "2023-02-07T09:32:51",
            "upload_time_iso_8601": "2023-02-07T09:32:51.140195Z",
            "url": "https://files.pythonhosted.org/packages/3c/99/162391b9edae58e1d14b4a27a2d0a5834c44197fcc73431c5704eabaed5c/z3c.csvvocabulary-3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-07 09:32:51",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "z3c.csvvocabulary"
}
        
Elapsed time: 0.03756s