Skip to main content

pyEnum

Build Status Coverage Status PyPI Status Python Version

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:

  1. Run with sudo to install pyEnum and dependencies globally

  2. Specify the --user option to install locally into your home directory (export “~/.local/bin” into PATH variable if haven’t).

  3. Run the command in a virtualenv local to a specific project working set.

Usage

Following example is showing how easy you can use this Enum in your code:

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

# Use get method with default value if want skip exception
print(TestEnum.get(8))         # None
print(TestEnum.get('eight'))   # None
print(TestEnum.get(8, 'eight')) # 'eight'

# Check if exist attribute with specific value
print(1 in TestEnum)           # True
print(8 in TestEnum)           # False

# Check if exist attribute with specific name
print('first' in TestEnum)     # False
print('third' in TestEnum)     # 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'

# Get count of all attributes
print(len(TestEnum))           # 4

# Get list with all attributes name
names = [item[0] for item in TestEnum]
print(names)                   # ['FIRST_ITEM', 'SECOND_ITEM', 'third', 'fourth']

# Get list with all attributes value
values = [item[1] for item in TestEnum]
print(values)                  # [1, 2, 3, 4]

# Read all items
for name, value, desc in TestEnum:
    print('{} = {} ({})'.format(name, value, desc))

Metadata

Release files for easy-enum 0.3.0

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

Source distribution (sdist)

Source distribution for easy-enum 0.3.0
File Size Uploaded
easy_enum-0.3.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for easy-enum 0.3.0
File Interpreter ABI Platform
easy_enum-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / easy_enum-0.3.0.tar.gz

Download URL easy_enum-0.3.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
aa9baffd64ba0421ae9abcee045347e7d783b105ccce38caf55fd80c21f95b6a
BLAKE2b-256 checksum
How to use checksums
a6ee5a432f5c91037ba0a289d4e79751c7c88d23a315aa1017656d15364865ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.8

Release files / easy_enum-0.3.0-py3-none-any.whl

Download URL easy_enum-0.3.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
29dcabaa6ab151f0026f68b9a2cb58ae3e4dc6f14edb4a0ee70d12a07c512603
BLAKE2b-256 checksum
How to use checksums
ae028cacde00c54689169f048295179e5356876a392975297e33f0b4c7021e0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.8

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

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