Elegant implementation of Enum which inherits from str or bytes
Project description
AnyStrEnum
Elegant implementation of Enum which inherits from str or bytes
Features
Easy assignment with type hinting (No need to use auto() or another stubs)
Automatic name generation with support of custom converters or separators
Method to filter members (contains, contained_by, startswith, endswith)
Custom str and bytes types support
Installation
$ pip install AnyStrEnum
Examples
As simple as this
from anystrenum import StrEnum
class Season(StrEnum):
spring: str
summer: str
autumn: str
winter: str
print(Season.summer)
print(isinstance(Season.summer, str))
# summer
# True
Using custom words separator
from anystrenum import StrEnum
class Region(StrEnum, sep='-'):
us_east_1: str
us_west_1: str
ca_central_1: str
cn_northwest_1: str
eu_central_1: str
eu_west_1: str
eu_north_2: str
sa_east_1: str
print(f'If you just specify the general endpoint,\n'
f'Amazon directs your request to the {Region.us_east_1} endpoint')
# If you just specify the general endpoint,
# Amazon directs your request to the us-east-1 endpoint'
Using str converter and custom words separator
from anystrenum import StrEnum
class ContentType(StrEnum, converter=lambda s: s.replace('_', '/', 1), sep='-'):
application_json: str
application_octet_stream: str
application_x_json_stream: str
audio_mpeg: str
audio_pcm: str
audio_ogg: str
print(f'In RFC 2046 "{ContentType.application_octet_stream}"'
f'is defined as "arbitrary binary data"')
# In RFC 2046 "application/octet-stream" is defined as "arbitrary binary data"
As you can see from an example, first the name will be converted with our lambda function and then, remaining underscores will be replaced with given separator
Filtering enum members
Using enums from previous examples
result = ContentType.filter(contains='-', startswith='a', endswith='m')
print(*result, sep=', ')
# application/octet-stream, application/x-json-stream
result = ContentType.filter(contained_in='Usually content type for MP3 is audio/mpeg')
print(*result, sep=', ')
# audio/mpeg
result = Region.filter(startswith='eu', endswith='1')
print(*result, sep=', ')
# eu-central-1, eu-west-1
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
AnyStrEnum-0.1.0.tar.gz
(4.5 kB
view hashes)
Built Distribution
Close
Hashes for AnyStrEnum-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ad70584824a5f88f9ca6a3b05f66528ed77dbcb439623b0dcb426f147e00ea8 |
|
MD5 | 7b85e18e503ed216cd9914e8d4c0a0a2 |
|
BLAKE2b-256 | 8f8bd8670cba0dce6d08712e1520875ba881df6ffb36c5e351986a444177bdc7 |