omnipy.util.literal_enum_generator
| FUNCTION | DESCRIPTION |
|---|---|
generate_literal_enum_code |
Generate code for a LiteralEnum class based on a tuple of string values. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
ValueType |
|
ValueType
module-attribute
ValueType = (
Sequence[LiteralEnumInnerTypes]
| Mapping[str, LiteralEnumInnerTypes]
| MappingProxyType[str, LiteralEnumInnerTypes]
)
generate_literal_enum_code
generate_literal_enum_code(
values: ValueType,
docstrings: Mapping[LiteralEnumInnerTypes, Sequence[str]] | None = None,
include_imports: bool = True,
class_name: str = "NewLiteralEnum",
) -> str
Generate code for a LiteralEnum class based on a tuple of string values.
| PARAMETER | DESCRIPTION |
|---|---|
values
|
Either one of the following: 1. Tuple of objects that define the literal values for the enum, where each value will be converted to a valid attribute name 2. Dict of string keys to values, where the keys are used as attribute names
TYPE:
|
docstrings
|
Optional dictionary mapping values to their
corresponding docstrings. If provided, the docstrings will be
added to the generated class attributes. The keys should match
the values in
TYPE:
|
include_imports
|
If True, includes necessary imports for Literal and LiteralEnum (default: True)
TYPE:
|
class_name
|
The name of the generated class (default: 'GeneratedEnum')
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string containing the complete Python code for a LiteralEnum class |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no values are provided or if class_name is not a valid identifier |
Example
code = generate_literal_enum_code(('active', 'inactive', 'pending')) print(code) from typing import Literal
from omnipy.util.literal_enum import LiteralEnum
class GeneratedEnum(LiteralEnum[str]): Literals = Literal['active', 'inactive', 'pending'] ACTIVE: Literal['active'] = 'active' INACTIVE: Literal['inactive'] = 'inactive' PENDING: Literal['pending'] = 'pending'