backports.strenum
A backport of (copy and paste from) python 3.10’s StrEnum class for >=3.8:
Base class for creating enumerated constants that are also subclasses of str.
See the design discussion, and Ethan Furman’s first and second PR with this implementation.
A slightly different implementation would likely be compatible with lower python versions; PRs are welcome if they pass the test suite. The existing (reference) implementation should still be the one used on supported versions.
Install with pip install backports.strenum, and use with:
try:
# be ready for 3.10 when it drops
from enum import StrEnum
except ImportError:
from backports.strenum import StrEnum
class MyStrEnum(StrEnum):
POTATO = "potato"
ORANGE = "orange"
SPADE = "spade"
MyStrEnum.POTATO == "potato" # True
MyStrEnum.ORANGE.upper() == "ORANGE" # True
str(MyStrEnum.SPADE) == "spade" # True
StrEnum
The second variation of Enum that is provided is also a subclass of str. Members of a StrEnum can be compared to strings; by extension, string enumerations of different types can also be compared to each other. StrEnum exists to help avoid the problem of getting an incorrect member:
>>> class Directions(StrEnum): ... NORTH = 'north', # notice the trailing comma ... SOUTH = 'south'
Before StrEnum, Directions.NORTH would have been the tuple ('north',).
Creating members that are mixed with other data types
When subclassing other data types, such as int or str, with an Enum, all values after the = are passed to that data type’s constructor. For example:
>>> class MyEnum(IntEnum): ... example = '11', 16 # '11' will be interpreted as a hexadecimal ... # number >>> MyEnum.example <MyEnum.example: 17>
StrEnum and str.__str__
An important difference between StrEnum and other Enums is the __str__ method; because StrEnum members are strings, some parts of Python will read the string data directly, while others will call str(). To make those two operations have the same result, StrEnum.__str__ will be the same as str.__str__ so that str(StrEnum.member) == StrEnum.member is true.
Metadata
Release files for backports.strenum 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| backports.strenum-1.0.4.tar.gz | 10.5 kB | Without provenance | Details for backports.strenum-1.0.4.tar.gz |
Release files / backports.strenum-1.0.4.tar.gz
| Download URL | backports.strenum-1.0.4.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
037d11693d5282c82898f504848e698d8428a77cf6028c08a5ae123fa256f9c6
|
|
BLAKE2b-256 checksum How to use checksums |
b9129e7009db51a569716b6ec9bb4636fb01936851a48a605f3e560ec96b0399
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6
|
Security for backports.strenum 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.