Skip to main content

AnyStrEnum

Python 3.7 PyPi Package Version

Elegant implementation of Enum which inherits from str or bytes

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

Features

  • Easy members assignment with type hinting (No need to use auto() or other stubs)

  • Automatic value generation with support of custom converters or separators

  • Method to filter members (contains, contained_in, startswith, endswith, etc.)

  • Custom str and bytes types support

Installation

$ pip install AnyStrEnum

Examples

Using custom words separator

To automatically replace all underscores (_) in names to something more suitable, use sep parameter:

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(Region.us_east_1)
# us-east-1

Using str converter and custom words separator

If you need to apply to your names more changes, you can use converter parameter. Pass a function in here which will be called on every member

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(ContentType.application_octet_stream)
# application/octet-stream

As you can see from an example, firstly, names 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

print(ContentType.filter(contains='-', startswith='a', endswith='m'))
# {<ContentType.application_octet_stream: 'application/octet-stream'>,
# <ContentType.application_x_json_stream: 'application/x-json-stream'>}

print(ContentType.filter(contained_in='Usually content type for MP3 is audio/mpeg'))
# {<ContentType.audio_mpeg: 'audio/mpeg'>}

print(Region.filter(startswith='eu', endswith='1'))
# {<Region.eu_west_1: 'eu-west-1'>, <Region.eu_central_1: 'eu-central-1'>}

Release files for AnyStrEnum 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for AnyStrEnum 0.2.0
File Size Uploaded
AnyStrEnum-0.2.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for AnyStrEnum 0.2.0
File Interpreter ABI Platform
AnyStrEnum-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / AnyStrEnum-0.2.0.tar.gz

Download URL AnyStrEnum-0.2.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
675a804941d370d98b618f6a11f7ebffb9a633a32c63057ee37b1d6b3b6e4be7
BLAKE2b-256 checksum
How to use checksums
2163391b3edcd6129e32a57fce21b1c41cfd27b48330cfa9b974d6b309ba6fee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release files / AnyStrEnum-0.2.0-py3-none-any.whl

Download URL AnyStrEnum-0.2.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2eea8854c0202cb04e5122558db8f1d0c801269922b89a20109206d94877fd31
BLAKE2b-256 checksum
How to use checksums
70e22f009daccfb8af8896c712ba9ad4e053e4132b76c9a8557bb9642de79ba3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page