choice_enum
Wrapper class for defining DRY, encapsulated choice options for CharFields.
ChoiceEnumeration
ChoiceEnumeration class can be declared at the module or class level in the following format:
class ChoiceClass(ChoiceEnumeration):
ATTR1 = ChoiceEnumeration.Option('db_value1', 'Human readable text 1')
ATTR2 = ChoiceEnumeration.Option('db_value2', 'Human readable text 2')
The following attributes are generated on the ChoiceEnumeration child class:
ALL_OPTIONS - a tuple of the supplied options
CHOICES - a Django char field choices-compatible tuple
CHOICES_DICT - a dictionary of option:text values
DEFAULT - (optional) the item marked as default
A typical Django example would look like:
class SomeModel(models.Model):
class MetaVar(ChoiceEnumeration):
FOO = ChoiceEnumeration.Option('foo', 'Foo Choice', default=True)
BAR = ChoiceEnumeration.Option('bar', 'Bar Option')
BAZ = ChoiceEnumeration.Option('baz', 'Baz Pick')
SPAM = ChoiceEnumeration.Option('spam', 'Spam spam spam')
EGGS = ChoiceEnumeration.Option('eggs', 'Eggs, Spam, and Ham')
- Example interactive usage::
>>> class MetaVar(ChoiceEnumeration): ... FOO = ChoiceEnumeration.Option('foo', 'Foo Choice', default=True) ... BAR = ChoiceEnumeration.Option('bar', 'Bar Option') ... BAZ = ChoiceEnumeration.Option('baz', 'Baz Pick') ... SPAM = ChoiceEnumeration.Option('spam', 'Spam spam spam') ... EGGS = ChoiceEnumeration.Option('eggs', 'Eggs, Spam, and Ham') >>> MetaVar.FOO u'foo' >>> MetaVar.ALL_OPTIONS (u'foo', u'bar', u'baz', u'spam', u'eggs') >>> MetaVar.CHOICES ((u'foo', 'Foo Choice'), (u'bar', 'Bar Option'), (u'baz', 'Baz Pick'), (u'spam', 'Spam spam spam'), (u'eggs', 'Eggs, Spam, and Ham')) >>> MetaVar.CHOICES_DICT {u'baz': 'Baz Pick', u'eggs': 'Eggs, Spam, and Ham', u'foo': 'Foo Choice', u'bar': 'Bar Option', u'spam': 'Spam spam spam'} >>> MetaVar.DEFAULT u'foo' >>> MetaVar.CHOICES_DICT[MetaVar.FOO] 'Foo Choice'
make_enum_class
make_enum_class dynamically generates a ChoiceEnumeration derived class.
Example:
from choice_enum import make_enum_class, Option
MetaVar = make_enum_class('MetaVar',
FOO=Option('foo', 'Foo Choice', default=True),
BAR=Option('bar', 'Bar Option'),
BAZ=Option('baz', 'Baz Pick')
)
Metadata
Release files for choice_enum 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| choice_enum-1.0.0.tar.gz | 3.8 kB | Without provenance | Details for choice_enum-1.0.0.tar.gz |
Release files / choice_enum-1.0.0.tar.gz
| Download URL | choice_enum-1.0.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
03d832fccb9b1ff2d770c3cf67553f6904d30b7645210e84609ea21c99d8b724
|
|
BLAKE2b-256 checksum How to use checksums |
5355d69359665df9a5d8412aa53ed1962a3763e3bad56f586dc6cd6c777b2290
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5
|
Security for choice_enum 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.