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. - validate_mapping_keys: Ensure that a provided mapping includes all enum values, raising an error for any missing mappings.
- map: Map enum members to values based on the provided dictionary.
- keys: Retrieve the keys of the enum class as a list or as a KeysView.
- values: Retrieve the values of the enum class as a list or as a ValuesView.
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'
Ensuring Completeness of Mappings with validate_mapping_keys
Validate that a provided mapping covers all enum members.
# Assuming a partial mapping for demonstration
partial_mapping = {'RED': 'Rouge', 'GREEN': 'Vert'}
try:
Color.validate_mapping_keys(partial_mapping)
except ValueError as e:
print(e)
# Expected output: Missing mappings for: BLUE
Mapping Enum Members with map
Map enum members to values based on the provided dictionary. This map is validated to ensure that all of the enum keys are used and there are no extra.
Note: At this time, the value types are not validated, just the key exists.
# Define the key mapping
key_mapping = {
TestEnum.VALUE_1: "some_new_value",
TestEnum.VALUE_2: "another_new_value",
TestEnum.VALUE_3: "a new value"
}
# Perform the mapping and validate
mapped_values = TestEnum.map(key_mapping)
# ----- The above is equivalent to the following: -----
# Validate the mapped values
expected_values = {
TestEnum.VALUE_1.value: "some_new_value",
TestEnum.VALUE_2.value: "another_new_value",
TestEnum.VALUE_3.value: "a new value"
}
assert mapped_values == expected_values
Retrieving Keys and Values with keys()
and values()
Retrieve the keys and values of the enum class as a list or as a KeysView
or ValuesView
.
Get keys as a list
keys_list = Color.keys()
Get keys as a KeysView
keys_view = Color.keys(as_list=False)
Get values as a list
values_list = Color.values()
Get values as a ValuesView
values_view = Color.values(as_list=False)
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
Built Distribution
Hashes for enum_with_dict-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e88737198d758e573f45a81b8f08c1102353c03dd1d29ede45015dc1a04b7b6 |
|
MD5 | 0a90ce5d38e624208ebcef34fbefc840 |
|
BLAKE2b-256 | f312b653e838b7a16cf01399d00e70e84c59a8731dc775b05a2218e67df91f40 |