Advanced Enumerations (compatible with Python's stdlib Enum), NamedTuples, and NamedConstants
76
Advanced Enumerations (compatible with Python's stdlib Enum), NamedTuples, and NamedConstants. This library provides enhanced enumeration capabilities that extend Python's standard library functionality with features like multiple values, auto-numbering, bitwise operations, and unique constraints.
pip install aenumfrom aenum import Enum, IntEnum, StrEnum, Flag, IntFlagBasic imports for most use cases:
from aenum import Enum, auto, uniquefrom aenum import Enum, IntEnum, StrEnum, auto
# Basic enumeration
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
# String enumeration (useful for APIs)
class Status(StrEnum):
PENDING = 'pending'
PROCESSING = 'processing'
COMPLETE = 'complete'
# Integer enumeration (sortable, comparable)
class Priority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
# Using auto for automatic value assignment
class Direction(Enum):
NORTH = auto()
SOUTH = auto()
EAST = auto()
WEST = auto()
# Usage examples
print(Color.RED) # Color.RED
print(Status.PENDING) # 'pending'
print(Priority.HIGH > Priority.LOW) # True
print(Direction.NORTH.value) # 1
# Functional API for simple cases
Animal = Enum('Animal', 'ANT BEE CAT DOG')
print(Animal.CAT) # Animal.CATThe aenum library is organized around these key components:
Enum, IntEnum, StrEnum provide the foundation with stdlib compatibilityFlag, IntFlag enable bitwise combinable enumerationsBasic enumeration classes that provide the foundation for creating typed constants with optional integer or string behavior.
class Enum:
def __init__(self, value): ...
class IntEnum(int, Enum):
def __init__(self, value): ...
class StrEnum(str, Enum):
def __init__(self, value): ...
class ReprEnum(Enum):
def __repr__(self): ...Flag enumerations that support bitwise operations for combining multiple values, commonly used for permissions, feature toggles, and configuration options.
class Flag(Enum):
def __or__(self, other): ...
def __and__(self, other): ...
def __xor__(self, other): ...
def __invert__(self): ...
class IntFlag(int, Flag):
def __init__(self, value): ...
class FlagBoundary(Enum):
STRICT: ...
CONFORM: ...
EJECT: ...
KEEP: ...Specialized enumeration classes that provide automatic numbering, ordering, uniqueness constraints, and multi-value support for complex enumeration needs.
class AutoNumberEnum(Enum):
def _generate_next_value_(name, start, count, last_values): ...
class AutoEnum(Enum):
"""Auto-enum that uses _generate_next_value_ for missing values (Python 3 only)."""
class OrderedEnum(Enum):
def __lt__(self, other): ...
def __le__(self, other): ...
def __gt__(self, other): ...
def __ge__(self, other): ...
class UniqueEnum(Enum): ...
class MultiValueEnum(Enum): ...
class AddValueEnum(Enum):
def _generate_next_value_(name, start, count, last_values): ...Enhanced tuple implementation with named fields, default values, docstrings, and methods for creating structured data containers.
def NamedTuple(typename, fields, **kwargs):
"""
Create a new NamedTuple class.
Args:
typename (str): Name of the new class
fields (str | list): Field names as string or list
**kwargs: Additional options
Returns:
type: New NamedTuple class
"""
class TupleSize(Enum):
fixed: ...
minimum: ...
variable: ...Helper functions, decorators, and classes for working with enumerations including uniqueness validation, runtime extension, conversion utilities, and named constants.
def unique(enumeration):
"""Decorator to ensure enum members have unique values."""
def extend_enum(enumeration, name, *args, **kwds):
"""Add new members to an existing enumeration."""
def export(enum_class, namespace=None):
"""Export enum members to a namespace."""
class auto:
"""Placeholder class for automatic value assignment."""
class NamedConstant:
"""Base class for named constants."""
class constant:
"""Descriptor for constant values."""Utilities and Advanced Features
# Core type definitions used across the API
EnumMeta = EnumType # Metaclass for Enum classes
class _EnumArgSpec:
args: list
varargs: str
varkw: str
defaults: tuple
# Sentinel values
class _auto_null: ...
class no_arg: ...
undefined: object
# Helper types for advanced features
class Member: ...
class NonMember: ...
class skip: ...
nonmember = skip
# Property types
class enum_property(property): ...Install with Tessl CLI
npx tessl i tessl/pypi-aenum