A declarative and flexible approach to Python enums with preprocessing, validation, and more
Project description
DeclarativeEnum
A declarative and flexible approach to Python enums that supports preprocessing, validation, namespacing, and direct value access.
Features
- Value preprocessing and validation
- Auto-numbering support
- Namespace prefixing
- Pattern formatting
- Value type enforcement
- Method chaining with direct access
- Aliases support
Installation
pip install declarativeenum
Basic Usage
from declarativeenum import DeclarativeEnum
# Basic enum with direct string values
class Colors(DeclarativeEnum):
RED
BLUE
GREEN
assert Colors.RED.value == "RED"
# Enum with preprocessing
class Headers(DeclarativeEnum):
__preprocess__ = lambda x: f"X-Custom-{x.lower()}"
TRACKING
VERSION
assert Headers.TRACKING.value == "X-Custom-tracking"
# Enum with type conversion and validation
class Ports(DeclarativeEnum):
__type__ = int
__validate__ = lambda x: 0 <= x <= 65535
HTTP = "80"
HTTPS = "443"
assert isinstance(Ports.HTTP.value, int)
assert Ports.HTTPS.value == 443
# Enum with direct value access
class StatusCode(DeclarativeEnum):
__directaccess__ = True
OK = 200
NOT_FOUND = 404
# Direct comparison works
assert StatusCode.OK == 200
Advanced Features
Preprocessing and Validation
class ValidatedEnum(DeclarativeEnum):
__preprocess__ = lambda x: x.upper()
__validate__ = lambda x: len(x) <= 10
__type__ = str
SHORT
ALSO_SHORT
Pattern Formatting
class APIEndpoints(DeclarativeEnum):
__pattern__ = "/api/v1/{}"
__namespace__ = "users"
PROFILE # becomes "/api/v1/users/profile"
SETTINGS # becomes "/api/v1/users/settings"
Auto-numbering
class OrderedEnum(DeclarativeEnum):
__autonumber__ = 100 # Start from 100
FIRST # 100
SECOND # 101
THIRD # 102
Method Chaining with Direct Access
class Headers(DeclarativeEnum):
__directaccess__ = True
CONTENT_TYPE = "application/json"
assert Headers.CONTENT_TYPE.upper() == "APPLICATION/JSON"
License
MIT License - see LICENSE file for 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
declarativeenum-1.0.0.tar.gz
(6.9 kB
view details)
Built Distribution
File details
Details for the file declarativeenum-1.0.0.tar.gz
.
File metadata
- Download URL: declarativeenum-1.0.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9873eb36acc15f9f4ac3d5ba07f8119b5f078fa8c0b71e16908e5e0e430309ad |
|
MD5 | 785c59ad25d72118e619561378ca01f7 |
|
BLAKE2b-256 | ebd4d0563651948584682155155cc70702b2a61522e58947e3a911d880094c73 |
File details
Details for the file declarativeenum-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: declarativeenum-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b5bf0e8098355151a05e0b4927931da27bc830b189686729d3ab93aba0f6612 |
|
MD5 | 5a529f4b1d071734d92026422a631cf9 |
|
BLAKE2b-256 | c3788ab486d6167a1c72e86f9a2f4ccf4e77640df838aab2335f19f8710083c1 |