An alternative to the built-in Python `enum` implementation
Project description
Data Enums
An alternative to the built-in Python enum
implementation. Tested in Python 3.6 and above.
Data enums allow you to:
- Associate data with enum members without using tuple-based initialization
- Use an intuitive initialization syntax to define members
- Define pure enums without using
auto()
- Define value-based enums without storing them as class attributes
- Define secondary unique keys & use them to look up enum members
- Use classmethod syntax (
.get(…)
) to look up members, instead of using initializers
Usage
Install via PyPI:
pip install data-enum
Minimal usage:
from data_enum import DataEnum
class Currency(DataEnum):
data_attributes = ('symbol', 'name')
Currency('CAD', symbol='$', name='Canadian dollar')
Currency('USD', symbol='$', name='United States dollar')
Currency('EUR', symbol='€', name='Euro')
Access the members by value:
Currency.get('USD')
Currency.get('CAD')
Currency.get('EUR')
Store the members as attributes:
class Currency(DataEnum):
data_attributes = ('symbol', 'name')
Currency.CAD = Currency('CAD', symbol='$', name='Canadian dollar')
Currency.USD = Currency('USD', symbol='$', name='United States dollar')
Currency.EUR = Currency('EUR', symbol='€', name='Euro')
Use a custom attribute as the primary ID:
class Currency(DataEnum):
primary_attribute = 'code'
data_attributes = ('symbol', 'name')
Currency('CAD', symbol='$', name='Canadian dollar')
Currency('USD', symbol='$', name='United States dollar')
Currency('EUR', symbol='€', name='Euro')
Use integers as primary IDs:
class Door(DataEnum):
data_attributes = ('description',)
Door(1, description='Door #1')
Door(2, description='Door #2')
Door(3, description='Door #3')
d2 = Door(2) # returns Door(2, description='Door #2')
int(d2) # returns 2
Or, skip primary IDs altogether for a pure enumeration:
from data_enum import DataEnum
class Currency(DataEnum):
data_attributes = ('symbol', 'name')
Currency.CAD = Currency(symbol='$', name='Canadian dollar')
Currency.USD = Currency(symbol='$', name='United States dollar')
Currency.EUR = Currency(symbol='€', name='Euro')
Access the attached data:
print(Currency.USD.name) # prints 'United States dollar'
print(Currency.EUR.symbol) # prints '€'
print(Currency.USD) # prints 'USD'
Compare directly:
Currency.USD == Currency.CAD # returns False
Currency.EUR == Currency.EUR # returns True
Enforce unique secondary attributes:
class Currency(DataEnum):
# Use a tuple with the second value as True for unique keys
data_attributes = (('symbol', True), 'name')
# Throws ValueError
Currency('CAD', symbol='$', name='Canadian dollar')
Currency('USD', symbol='$', name='United States dollar')
Look up members by unique secondary attributes:
Currency.get(symbol='€') # returns Currency.EUR
Currency.get(symbol='&') # throws MemberDoesNotExistError
Look up with members with defaults:
Currency.get('ZZZ', Currency.USD) # returns Currency.USD
Currency.get('ZZZ', default=Currency.USD) # returns Currency.USD
Currency.get(symbol='&', default=Currency.USD) # returns Currency.USD
Testing, etc.
Install requrements:
make install
Sort imports (Requires Python >= 3.6):
make format
Lint (Requires Python >= 3.6):
make lint
Test:
make test
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
File details
Details for the file data-enum-2.0.1.tar.gz
.
File metadata
- Download URL: data-enum-2.0.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cc461fb6aa7f98188a67fd9ba357cd36847b1b31b0aaee99ad42c0fa34c0e3f |
|
MD5 | fef982850841f8af7b7599977f0eb13b |
|
BLAKE2b-256 | 8fe5c5647bf1f0dad89fc4e1c1e4a3275c8d56a5dcd7c0a46d78a554aa16dba3 |
File details
Details for the file data_enum-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: data_enum-2.0.1-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74edbda5a6dc461877ea0b53273bed2d6f298c6e402d086f043c8309259c104f |
|
MD5 | 8ad28e3bf820d99add92ac5a7bb6ab74 |
|
BLAKE2b-256 | 000fe154f74864682dd3e62f7811545b2bd66290f4b117ad165cc0ab33e616ab |