Skip to main content

Practical utilities for Python enums — lookup, validation, listing, and serialization

Project description

philiprehberger-enum-tools

Tests PyPI version License

Practical utilities for Python enums — lookup, validation, listing, and serialization.

Installation

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

Development

pip install -e .
python -m pytest tests/ -v

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

philiprehberger_enum_tools-0.1.6.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_enum_tools-0.1.6-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_enum_tools-0.1.6.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_enum_tools-0.1.6.tar.gz
Algorithm Hash digest
SHA256 19bc2228c63406301abc92e19f573076cdbd882a30e5ef592af5f031564222d0
MD5 b7d5c2eab28172f0e23888912864a580
BLAKE2b-256 47812e5c24a3ee61c7e8f138b5ec9d59a1d585b3beb2b2b2bef5be83933e1676

See more details on using hashes here.

File details

Details for the file philiprehberger_enum_tools-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_enum_tools-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8b2f621012390e5f95be55e5f6d2afd4faa8f3db23bad1a17c969f18c42377a4
MD5 48a4b415474fc4816b4929b904397143
BLAKE2b-256 fb53acec055b8dd2f16460fcc1814bec10037f8df9182aaf9b336b72798a5495

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page