Skip to main content

Useful Enums - a library for creating Enums in a DRY way in Python

Build Status

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

Metadata

Release files for django-useful-enums 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-useful-enums 1.0.1
File Size Uploaded Provenance Actions
django-useful-enums-1.0.1.tar.gz 5.0 kB Without provenance Details for django-useful-enums-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-useful-enums 1.0.1
File Interpreter ABI Platform Actions
django_useful_enums-1.0.1-py3-none-any.whl Python 3 none any Details for django_useful_enums-1.0.1-py3-none-any.whl

Total release size: 11.5 kB

Release files / django-useful-enums-1.0.1.tar.gz

Download URL django-useful-enums-1.0.1.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4ad8ddf79e12f44c304ce1be470645209406bbaf26da276f260eaf3b96e5f2e8
BLAKE2b-256 checksum
How to use checksums
64e6435e123a4c28f881ec07875c5b523d6429ecd33d49312434983cf1cf3faa
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / django_useful_enums-1.0.1-py3-none-any.whl

Download URL django_useful_enums-1.0.1-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8caecb66bfe74d3e251253152fa38cdaa1d7b3916de83d5ccde573f842db617
BLAKE2b-256 checksum
How to use checksums
9710a9a0ad8089329640b5062be401e0ed01165b61a37c03890024e13f823897
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for django-useful-enums 1.0.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page