User friendly implementation of Enum in Python
Project description
User friendly implementation of documented Enum type for Python language.
Installation
$ pip install easy_enum
To install the latest version from master branch execute in shell following commands:
$ pip install -U https://github.com/molejar/pyEnum/archive/master.zip
In case of development, install pyEnum from sources:
$ git clone https://github.com/molejar/pyEnum.git
$ cd pyEnum
$ pip install -U -e .
You may run into a permissions issues running these commands. Here are a few options how to fix it:
Run with sudo to install pyEnum and dependencies globally
Specify the --user option to install locally into your home directory (export “~/.local/bin” into PATH variable if haven’t).
Run the command in a virtualenv local to a specific project working set.
Usage
Example for basic Enum (Enum):
from easy_enum import Enum
class TestEnum(Enum):
# attribute with no description, the name will be 'FIRST_ITEM' and empty string as description
FIRST_ITEM = 1
# attribute with description
SECOND_ITEM = (2, 'Description for second item')
# attribute with description and custom string name
THIRD_ITEM = (3, 'third', 'Description for third item')
# attribute with custom string name (the description must be specified as empty string)
FOURTH_ITEM = (4, 'fourth', '')
# Read attributes value and name
print(TestEnum.SECOND_ITEM) # 2
print(TestEnum['FIRST_ITEM']) # 1
print(TestEnum[1]) # 'FIRST_ITEM'
print(TestEnum[3]) # 'third'
print(TestEnum['third']) # 3
# Get count of all attributes
print(len(TestEnum)) # 4
# Get list with all attributes name
names = [item[0] for item in TestEnum]
print(names)
# Get list with all attributes value
values = [item[1] for item in TestEnum]
print(values)
# Read all items
for name, value, desc in TestEnum:
print('{} = {} ({})'.format(name, value, desc))
Example for extended Enum (EEnum):
from easy_enum import EEnum as Enum
class TestEnum(Enum):
# attribute with no description, the name will be 'FIRST_ITEM' and empty string as description
FIRST_ITEM = 1
# attribute with description
SECOND_ITEM = (2, 'Description for second item')
# attribute with description and custom string name
THIRD_ITEM = (3, 'third', 'Description for third item')
# attribute with custom string name (the description must be specified as empty string)
FOURTH_ITEM = (4, 'fourth', '')
# Check if exist attribute with specific value
print(TestEnum.is_valid(1)) # True
print(TestEnum.is_valid(8)) # False
# Check if exist attribute with specific name
print(TestEnum.is_valid('first')) # False
print(TestEnum.is_valid('third')) # True
# Get attribute description (as parameter use attribute name or value)
print(TestEnum.desc(1)) # ''
print(TestEnum.desc(2)) # 'Description for second item'
print(TestEnum.desc('third')) # 'Description for third item'
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
Hashes for easy_enum-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14392876234de11b0361995e0ae242c811e4a936734626004464f5e3c6f5b9f4 |
|
MD5 | 910df24679e23042294fa9d1deb64e94 |
|
BLAKE2b-256 | e85367466b29a840567738f0319def7e86f8ab833566e9fa83bcd3b4893f6b0e |