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.5 and above, but also supports Python 2.7.
Data enums allow you to:
- Associate data with enum members without using tuple-based initialization
- Add secondary unique keys
- Lookup enum members by secondary unique keys
Usage
Install via PyPI:
pip install data-enum
Minimal usage:
from data_enum import DataEnum
class Currency(DataEnum):
data_attribute_names = ('symbol', 'name')
# Call the register function only once
Currency.register([
Currency('CAD', symbol='$', name='Canadian dollar'),
Currency('USD', symbol='$', name='United States dollar'),
Currency('EUR', symbol='€', name='Euro'),
])
Access the members by attribute (if the member value is an attribute-name-friendly string):
Currency.USD
Currency.CAD
Currency.EUR
Access the members by value:
Currency.get('USD')
Currency.get('CAD')
Currency.get('EUR')
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_attribute_names = (('symbol', True), 'name')
# Throws ValueError
Currency.register([
Currency('CAD', symbol='$', name='Canadian dollar'),
Currency('USD', symbol='$', name='United States dollar'),
Currency('EUR', symbol='€', name='Euro'),
])
Look up members by unique secondary attributes:
Currency.get(symbol='€') # returns Currency.EUR
Currency.get(symbol='&') # throws MemberDoesNotExistError exception
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
Use integers as values:
class Door(DataEnum):
data_attribute_names = ('description',)
# Call the register function only once
Door.register([
Door(1, name='Door #1'),
Door(2, name='Door #2'),
Door(3, name='Door #3'),
])
d2 = Door(2) # returns Door(2, name='Door #2')
int(d2) # returns 2
Testing, etc.
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
data-enum-1.1.1.tar.gz
(9.9 kB
view hashes)
Built Distribution
data_enum-1.1.1-py3-none-any.whl
(10.8 kB
view hashes)
Close
Hashes for data_enum-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcb34b8e4311d4c6e14f26a244ce303b5cd9c21a9859bb2a2a4f371e22fbf798 |
|
MD5 | 5b30f29abd51310e7ee3f8412ee095e1 |
|
BLAKE2b-256 | b940aceb9d22d9be15276452feb4c64071062008f5e5b3730cc412d6a6690eb4 |