Enum with to_dict method.
Project description
EnumWithDict
EnumWithDict
is a Python package that extends the standard library's Enum
class to include to_dict
and get_initial
class methods. This enhancement allows for straightforward conversion of enum classes to dictionaries and provides easy access to the initial enum value, facilitating a more versatile use of enumerations in Python applications.
Features
- to_dict: Convert an enum class to a dictionary representation, mapping member names to their values.
- get_initial: Retrieve the first value defined in the enum, useful for cases where a default or initial value is needed.
- get: Mimics the dictionary
get
method, allowing retrieval of enum values with an optional default fallback.
Installation
Install EnumWithDict
using pip:
pip install enum_with_dict
Usage
Defining an Enum with EnumWithDict
from enum_with_dict import EnumWithDict
class Color(EnumWithDict):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
Converting an Enum to a Dictionary
color_dict = Color.to_dict()
print(color_dict)
# Output: {'RED': 'red', 'GREEN': 'green', 'BLUE': 'blue'}
Getting the Initial Enum Value
initial_color = Color.get_initial()
print(initial_color)
# Output: 'red'
Using the get
Method
Retrieve an enum value by its name, with an option to specify a default value if the name does not exist.
Get a value for an existing key
print(Color.get('RED')) # Output: 'red'
Get a value for a non-existing key with a default value
print(Color.get('PURPLE', default='unknown')) # Output: 'unknown'
Get a value for a non-existing key, falling back to the initial value
print(Color.get('PURPLE')) # Output: 'red'
LICENSE
EnumWithDict
is released under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
enum_with_dict-0.2.2.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for enum_with_dict-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8368414adf899ff9bc64cf0796f1efebc5a39006982cd6dcb8bf4c119bbddea0 |
|
MD5 | e7971b3883c5afebe23eec9ca64b388b |
|
BLAKE2b-256 | 82d82450886b9a7435114c3a54c53f6f310fe50c79226451ae89454887179164 |