A useful enumeration / choice library for Django.
Project description
Useful Enums - a library for creating Enums in a DRY way in Python
This library grew out of me getting fed up with having to create Django choices that were lists of tuples. I far preferred nicely contained enums of the form:
class MyEnum:
VALUE_1 = 1
VALUE_2 = 2
However, most of the time you don't need to create all of the IDs yourself.
You should just be able to define the key names and have the Enum class do the
work for you. usefulenums does that.
The syntax is very similar to the Django idiom for creating a tuple of choices:
from usefulenums import Enum
MyEnum = Enum(
("MY_FIRST_VALUE", "My First Display Text"),
("MY_SECOND_VALUE", "My Second Value"),
)
However, you can now refer to those items by name in a more Pythonic fashion, rather than having to use hard-coded strings:
>>> MyEnum.MY_FIRST_VALUE
0
Note that here, Enum() has given automatic values to the enum items. You can
set these values yourself by passing a three-tuple to Enum when you create
it:
>>> MyEnum = Enum(
... ("VALUE_1", "MY_FIRST_VALUE", "My First Value"),
... ("VALUE_2", "MY_SECOND_VALUE", "My Second Value"),
... )
>>> print(MyEnum.MY_FIRST_VALUE)
VALUE_1
You can also extract the Django-style choices from the Enum so that you use
it as you've always used choices:
>>> MyEnum.as_choices()
(('VALUE_1', 'My First value'), ('VALUE_2', 'My Second Value'))
Testing
django-useful-enums comes with unit tests that cover its behaviour. You can
run them using nose:
$ pip install nose
$ nosetests
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-useful-enums-1.0.1.tar.gz.
File metadata
- Download URL: django-useful-enums-1.0.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ad8ddf79e12f44c304ce1be470645209406bbaf26da276f260eaf3b96e5f2e8
|
|
| MD5 |
c6a6bf06f55874300a4458f72dd212d9
|
|
| BLAKE2b-256 |
64e6435e123a4c28f881ec07875c5b523d6429ecd33d49312434983cf1cf3faa
|
File details
Details for the file django_useful_enums-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_useful_enums-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8caecb66bfe74d3e251253152fa38cdaa1d7b3916de83d5ccde573f842db617
|
|
| MD5 |
1f4a5c19635c355cfe6576f233a0c484
|
|
| BLAKE2b-256 |
9710a9a0ad8089329640b5062be401e0ed01165b61a37c03890024e13f823897
|