Advanced Enumerations (compatible with Python's stdlib Enum), NamedTuples, and NamedConstants
76
Basic enumeration classes that provide the foundation for creating typed constants with optional integer or string behavior. These classes are fully compatible with Python's stdlib Enum while providing enhanced functionality.
The base enumeration class for creating enumerated constants. Provides a clean way to create sets of named values with automatic iteration, membership testing, and comparison support.
class Enum:
def __init__(self, value):
"""
Create an enum member.
Args:
value: The value to associate with this enum member
"""
def __new__(cls, value):
"""Create and return a new enum member."""
def __str__(self):
"""Return the name of the enum member."""
def __repr__(self):
"""Return the representation of the enum member."""
@property
def name(self):
"""The name of the enum member."""
@property
def value(self):
"""The value of the enum member."""
@classmethod
def __members__(cls):
"""Return a mapping of member names to members."""
def __call__(self, value):
"""Return the enum member matching the given value."""from aenum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
# Access members
print(Color.RED) # Color.RED
print(Color.RED.name) # 'RED'
print(Color.RED.value) # 1
# Iteration
for color in Color:
print(color)
# Membership testing
print(Color.RED in Color) # True
# Functional API
Animal = Enum('Animal', 'ANT BEE CAT DOG')
print(Animal.CAT) # Animal.CATEnumeration that subclasses int, allowing enum members to be used anywhere integers are expected while maintaining enum identity and behavior.
class IntEnum(int, Enum):
def __init__(self, value):
"""
Create an integer enum member.
Args:
value (int): Integer value for the enum member
"""
def __new__(cls, value):
"""Create and return a new integer enum member."""from aenum import IntEnum
class Priority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
# Integer operations
print(Priority.HIGH + 1) # 4
print(Priority.HIGH > Priority.LOW) # True
print(Priority.MEDIUM * 2) # 4
# Still maintains enum identity
print(Priority.HIGH) # Priority.HIGH
print(isinstance(Priority.HIGH, int)) # TrueEnumeration that subclasses str, allowing enum members to be used anywhere strings are expected while maintaining enum identity and behavior. Particularly useful for API constants and serialization.
class StrEnum(str, Enum):
def __init__(self, value):
"""
Create a string enum member.
Args:
value (str): String value for the enum member
"""
def __new__(cls, value):
"""Create and return a new string enum member."""from aenum import StrEnum
class Status(StrEnum):
PENDING = 'pending'
PROCESSING = 'processing'
COMPLETE = 'complete'
# String operations
print(Status.PENDING.upper()) # 'PENDING'
print('Status: ' + Status.PENDING) # 'Status: pending'
print(Status.PENDING == 'pending') # True
# JSON serialization friendly
import json
data = {'status': Status.PENDING}
print(json.dumps(data)) # {"status": "pending"}Enumeration that only changes the repr() method while leaving str() and format() to the mixed-in type. Useful when you want enum identity but string behavior in most contexts.
class ReprEnum(Enum):
def __repr__(self):
"""Return enum-style representation."""
def __str__(self):
"""Return the value as string."""
def __format__(self, format_spec):
"""Format using the value's format method."""from aenum import ReprEnum
class Color(ReprEnum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
# Different repr vs str behavior
print(repr(Color.RED)) # Color.RED
print(str(Color.RED)) # 'red'
print(f"Color: {Color.RED}") # 'Color: red'from aenum import Enum
class Planet(Enum):
MERCURY = (3.303e+23, 2.4397e6)
VENUS = (4.869e+24, 6.0518e6)
EARTH = (5.976e+24, 6.37814e6)
def __init__(self, mass, radius):
self.mass = mass # in kilograms
self.radius = radius # in meters
@property
def surface_gravity(self):
# universal gravitational constant (m3 kg-1 s-2)
G = 6.67300E-11
return G * self.mass / (self.radius * self.radius)from aenum import Enum
# Simple functional creation
Color = Enum('Color', 'RED GREEN BLUE')
# With custom values
Color = Enum('Color', [('RED', 1), ('GREEN', 2), ('BLUE', 3)])
# From dictionary
color_dict = {'RED': 1, 'GREEN': 2, 'BLUE': 3}
Color = Enum('Color', color_dict)from aenum import StrEnum, IntEnum
class LogLevel(IntEnum):
DEBUG = 10
INFO = 20
WARNING = 30
ERROR = 40
CRITICAL = 50
class Environment(StrEnum):
DEVELOPMENT = 'dev'
STAGING = 'staging'
PRODUCTION = 'prod'from aenum import Enum
class OrderStatus(Enum):
PENDING = 'pending'
CONFIRMED = 'confirmed'
SHIPPED = 'shipped'
DELIVERED = 'delivered'
CANCELLED = 'cancelled'
def can_transition_to(self, new_status):
transitions = {
self.PENDING: [self.CONFIRMED, self.CANCELLED],
self.CONFIRMED: [self.SHIPPED, self.CANCELLED],
self.SHIPPED: [self.DELIVERED],
self.DELIVERED: [],
self.CANCELLED: []
}
return new_status in transitions.get(self, [])Install with Tessl CLI
npx tessl i tessl/pypi-aenumevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10