PyBackport: Enumerations¶
py_back.enum ¶
Backported enum types.
All enumerations can be imported just like the ones provided by python.
from py_back import enum
class Number(enum.IntEnum):
"""Enumeration using the original 'IntEnum' call"""
ONE = enum.auto()
TWO = 2
class Animal(enum.StrEnum):
"""Supported original 'StrEnum' for python versions < 3.11"""
CAT = enum.auto()
DOG = "dog"
Importing the py_back.enum module ensures that only the required classes are
backported.
IntEnum ¶
Bases: ReprEnum, _enum.IntEnum
Enum where members are also (and must be) ints.
IntEnum is the same as Enum, but its members are also integers and
can be used anywhere that an integer can be used.
Backports
Python 3.11:
Class inherits from ReprEnum to leave the str() and format() to
the builtin class.
Notes
__str__()
is now int.__str__() to better support the replacement of existing
constants use-case.
__format__()
was already int.__format__() for that same reason.
IntFlag ¶
Bases: ReprEnum, _enum.IntFlag
Support for integer-based Flags.
IntFlag is the same as Flag, but its members are also integers and can be used anywhere that an integer can be used.
Backports
Python 3.11:
Class inherits from ReprEnum to leave the str() and format() to
the builtin class.
Notes
__str__()
is now int.__str__() to better support the replacement of existing
constants use-case.
__format__()
was already int.__format__() for that same reason.
ReprEnum ¶
Bases: _enum.Enum
Updates repr, leaving str and format to the builtin class.
ReprEnum uses the repr() of Enum, but the str() of the mixed-in data type.
The class is used for any builtin type enum.
Backports
Python 3.11: The class was first defined.
StrEnum ¶
Enum where members are also (and must be) strings.
StrEnum is the same as
Enum, but
its members are also strings and can be used in most of the same places
that a string can be used.
Examples:
>>> from py_back import enum
>>> class Animal(enum.StrEnum):
... CAT = enum.auto()
... DOG = "dog"
...
>>> Animal.CAT
cat
>>> Animal.DOG.title()
'Dog'
>>> Animal.CAT == "cat"
True
>>> Animal.CAT + Animal.DOG
'catdog'
>>> " and ".join(list(Animals))
'cat and dog'
Notes
Using auto
results in the lower-cased member name as the value.
__new__ ¶
__new__(*values) -> StrEnum
Create new StrEnum.
Method copied from original enum.StrEnum code.
Values must already be of type str.