Skip to main content

StrEnum

Build Status

StrEnum is a Python enum.Enum that inherits from str to complement enum.IntEnum in the standard library. Supports python 3.6+.

Installation

You can use pip to install.

pip install StrEnum

Usage

from enum import auto
from strenum import StrEnum


class HttpMethod(StrEnum):
    GET = auto()
    HEAD = auto()
    POST = auto()
    PUT = auto()
    DELETE = auto()
    CONNECT = auto()
    OPTIONS = auto()
    TRACE = auto()
    PATCH = auto()


assert HttpMethod.GET == "GET"

## You can use StrEnum values just like strings:

import urllib.request

req = urllib.request.Request('https://www.python.org/', method=HttpMethod.HEAD)
with urllib.request.urlopen(req) as response:
   html = response.read()

assert len(html) == 0 # HEAD requests do not (usually) include a body

There are classes whose auto() value folds each member name to upper or lower case:

from enum import auto
from strenum import LowercaseStrEnum, UppercaseStrEnum

class Tag(LowercaseStrEnum):
    Head = auto()
    Body = auto()
    Div = auto()

assert Tag.Head == "head"
assert Tag.Body == "body"
assert Tag.Div == "div"

class HttpMethod(UppercaseStrEnum):
    Get = auto()
    Head = auto()
    Post = auto()

assert HttpMethod.Get == "GET"
assert HttpMethod.Head == "HEAD"
assert HttpMethod.Post == "POST"

As well as classes whose auto() value converts each member name to camelCase, PascalCase, kebab-case and snake_case:

from enum import auto
from strenum import CamelCaseStrEnum, PascalCaseStrEnum
from strenum import KebabCaseStrEnum, SnakeCaseStrEnum

class CamelTestEnum(CamelCaseStrEnum):
    OneTwoThree = auto()


class PascalTestEnum(PascalCaseStrEnum):
    OneTwoThree = auto()


class KebabTestEnum(KebabCaseStrEnum):
    OneTwoThree = auto()


class SnakeTestEnum(SnakeCaseStrEnum):
    OneTwoThree = auto()

assert CamelTestEnum.OneTwoThree == "oneTwoThree"
assert PascalTestEnum.OneTwoThree == "OneTwoThree"
assert KebabTestEnum.OneTwoThree == "one-two-three"
assert SnakeTestEnum.OneTwoThree == "one_two_three"

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please ensure tests pass before submitting a PR. This repository uses Black and Pylint for consistency. Both are run automatically as part of the test suite.

Running the tests

Tests can be run using make:

make test

This will create a virutal environment, install the module and its test dependencies and run the tests. Alternatively you can do the same thing manually:

python3 -m venv .venv
.venv/bin/pip install .[test]
.venv/bin/pytest

License

MIT

N.B. Starting with Python 3.10, enum.StrEnum is available in the standard library. This implementation is not a drop-in replacement for the standard library implementation. Sepcifically, the Python devs have decided to case fold name to lowercase by default when auto() is used which I think violates the principle of least surprise.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

StrEnum-0.4.5.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

StrEnum-0.4.5-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file StrEnum-0.4.5.tar.gz.

File metadata

  • Download URL: StrEnum-0.4.5.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.0 importlib_metadata/3.7.3 packaging/20.9 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.6

File hashes

Hashes for StrEnum-0.4.5.tar.gz
Algorithm Hash digest
SHA256 7fe752a2c95328cb1882abd352a890df0242688f635ee3ce8211b8c273c1f294
MD5 20b2f13996da233a1f4ac08c0ef454bf
BLAKE2b-256 4143d1b536f8a242cc37f8e116a287c990bc50199be27a308d9505a956123e55

See more details on using hashes here.

File details

Details for the file StrEnum-0.4.5-py3-none-any.whl.

File metadata

  • Download URL: StrEnum-0.4.5-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.0 importlib_metadata/3.7.3 packaging/20.9 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.6

File hashes

Hashes for StrEnum-0.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8a42e59ba34e2decc6668172926f9c43b0741a283e97481921b0fab6a1c509a5
MD5 28e5ea9e025c42ac6ddeb1724566b489
BLAKE2b-256 a95d75aca3deebf16a781068a4250719c7076d902444261619ab5a54e4cccac8

See more details on using hashes here.

Supported by

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