Skip to main content

Elegant implementation of Enum which inherits from str or bytes

Project description

AnyStrEnum

Python 3.7 PyPi Package Version

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


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)

Uploaded Source

Built Distribution

AnyStrEnum-0.1.0-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page