Practical utilities for Python enums — lookup, validation, listing, and serialization
Project description
philiprehberger-enum-tools
Practical utilities for Python enums — lookup, validation, listing, and serialization.
Install
pip install philiprehberger-enum-tools
Usage
from enum import Enum, auto
from philiprehberger_enum_tools import lookup, choices, values, names, validate
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
Safe Lookup
lookup(Color, "red") # Color.RED
lookup(Color, "yellow") # None
lookup(Color, "yellow", default=Color.RED) # Color.RED
Validation
validate(Color, "red") # Color.RED
validate(Color, "yellow") # ValueError: 'yellow' is not a valid Color value. Valid values: 'red', 'green', 'blue'
Listing
values(Color) # ['red', 'green', 'blue']
names(Color) # ['RED', 'GREEN', 'BLUE']
choices(Color) # [('red', 'RED'), ('green', 'GREEN'), ('blue', 'BLUE')]
AutoEnum
Enum subclass where auto() generates lowercase member names as values.
from philiprehberger_enum_tools import AutoEnum
from enum import auto
class Status(AutoEnum):
ACTIVE = auto()
INACTIVE = auto()
PENDING = auto()
Status.ACTIVE.value # 'active'
Status.PENDING.value # 'pending'
SerializableEnum
Enum that serializes to its value in JSON.
import json
from philiprehberger_enum_tools import SerializableEnum, SerializableEncoder
class Priority(SerializableEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
json.dumps({"priority": Priority.HIGH}, cls=SerializableEncoder)
# '{"priority": 3}'
API
| Function / Class | Description |
|---|---|
lookup(enum_class, value, *, default=None) |
Safe value-to-member lookup, returns default if not found |
choices(enum_class) |
List of (value, name) tuples for forms and CLIs |
values(enum_class) |
List of all member values |
names(enum_class) |
List of all member names |
validate(enum_class, value) |
Lookup or raise ValueError with valid options |
AutoEnum |
Enum subclass where auto() generates lowercase name as value |
SerializableEnum |
Enum that serializes to its value via SerializableEncoder |
SerializableEncoder |
JSON encoder supporting SerializableEnum members |
License
MIT
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_enum_tools-0.1.3.tar.gz.
File metadata
- Download URL: philiprehberger_enum_tools-0.1.3.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c75c840328782fb69ab7f0894b6210c2caf45b89ea312990f1e8bbf467f348d
|
|
| MD5 |
fb84ee50dd6e0483006f15a265ab63d5
|
|
| BLAKE2b-256 |
91dd41df0b70fc39dd1ac7ea6b24d98bd0e9b802cf4a6c59561cc2f4fffda398
|
File details
Details for the file philiprehberger_enum_tools-0.1.3-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_enum_tools-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
207400f30d4c5e2b468206533bd54b778ebe07947192c1e517601f4f819e5cf6
|
|
| MD5 |
4e4aaf37856df0c481c8fed6abe0532e
|
|
| BLAKE2b-256 |
e8c42d86940ebbaf3549e4a6aba35b90d132f064e5d98e60f0134d597bb2d54b
|