StrEnum


NameStrEnum JSON
Version 0.4.15 PyPI version JSON
download
home_pagehttps://github.com/irgeek/StrEnum
SummaryAn Enum that inherits from str.
upload_time2023-06-29 22:02:58
maintainer
docs_urlNone
authorJames Sinclair
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # StrEnum

[![Build Status](https://github.com/irgeek/StrEnum/workflows/Python%20package/badge.svg)](https://github.com/irgeek/StrEnum/actions)

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

## Installation

You can use [pip](https://pip.pypa.io/en/stable/) to install.

```bash
pip install StrEnum
```

## Usage

```python
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:

```python
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, snake_case and MACRO_CASE:

```python
from enum import auto
from strenum import CamelCaseStrEnum, PascalCaseStrEnum
from strenum import KebabCaseStrEnum, SnakeCaseStrEnum
from strenum import MacroCaseStrEnum


class CamelTestEnum(CamelCaseStrEnum):
    OneTwoThree = auto()


class PascalTestEnum(PascalCaseStrEnum):
    OneTwoThree = auto()


class KebabTestEnum(KebabCaseStrEnum):
    OneTwoThree = auto()


class SnakeTestEnum(SnakeCaseStrEnum):
    OneTwoThree = auto()


class MacroTestEnum(MacroCaseStrEnum):
    OneTwoThree = auto()


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

As with any Enum you can, of course, manually assign values.

```python
from strenum import StrEnum


class Shape(StrEnum):
    CIRCLE = "Circle"


assert Shape.CIRCLE == "Circle"
```

Doing this with the case-changing classes, though, won't manipulate
values--whatever you assign is the value they end up with.

```python
from strenum import KebabCaseStrEnum


class Shape(KebabCaseStrEnum):
    CIRCLE = "Circle"


# This will raise an AssertionError because the value wasn't converted to kebab-case.
assert Shape.CIRCLE == "circle"
```

## 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](https://black.readthedocs.io/en/stable/) and
[Pylint](https://www.pylint.org/) 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](https://choosealicense.com/licenses/mit/)

**N.B. Starting with Python 3.11, `enum.StrEnum` is available in the standard
library. This implementation is _not_ a drop-in replacement for the standard
library implementation. Specifically, 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.**

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/irgeek/StrEnum",
    "name": "StrEnum",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "James Sinclair",
    "author_email": "james@nurfherder.com",
    "download_url": "https://files.pythonhosted.org/packages/85/ad/430fb60d90e1d112a62ff57bdd1f286ec73a2a0331272febfddd21f330e1/StrEnum-0.4.15.tar.gz",
    "platform": null,
    "description": "# StrEnum\n\n[![Build Status](https://github.com/irgeek/StrEnum/workflows/Python%20package/badge.svg)](https://github.com/irgeek/StrEnum/actions)\n\nStrEnum is a Python `enum.Enum` that inherits from `str` to complement\n`enum.IntEnum` in the standard library. Supports python 3.7+.\n\n## Installation\n\nYou can use [pip](https://pip.pypa.io/en/stable/) to install.\n\n```bash\npip install StrEnum\n```\n\n## Usage\n\n```python\nfrom enum import auto\nfrom strenum import StrEnum\n\n\nclass HttpMethod(StrEnum):\n    GET = auto()\n    HEAD = auto()\n    POST = auto()\n    PUT = auto()\n    DELETE = auto()\n    CONNECT = auto()\n    OPTIONS = auto()\n    TRACE = auto()\n    PATCH = auto()\n\n\nassert HttpMethod.GET == \"GET\"\n\n# You can use StrEnum values just like strings:\n\nimport urllib.request\n\nreq = urllib.request.Request('https://www.python.org/', method=HttpMethod.HEAD)\nwith urllib.request.urlopen(req) as response:\n    html = response.read()\n\nassert len(html) == 0  # HEAD requests do not (usually) include a body\n```\n\nThere are classes whose `auto()` value folds each member name to upper or lower\ncase:\n\n```python\nfrom enum import auto\nfrom strenum import LowercaseStrEnum, UppercaseStrEnum\n\n\nclass Tag(LowercaseStrEnum):\n    Head = auto()\n    Body = auto()\n    Div = auto()\n\n\nassert Tag.Head == \"head\"\nassert Tag.Body == \"body\"\nassert Tag.Div == \"div\"\n\n\nclass HttpMethod(UppercaseStrEnum):\n    Get = auto()\n    Head = auto()\n    Post = auto()\n\n\nassert HttpMethod.Get == \"GET\"\nassert HttpMethod.Head == \"HEAD\"\nassert HttpMethod.Post == \"POST\"\n```\n\nAs well as classes whose `auto()` value converts each member name to camelCase,\nPascalCase, kebab-case, snake_case and MACRO_CASE:\n\n```python\nfrom enum import auto\nfrom strenum import CamelCaseStrEnum, PascalCaseStrEnum\nfrom strenum import KebabCaseStrEnum, SnakeCaseStrEnum\nfrom strenum import MacroCaseStrEnum\n\n\nclass CamelTestEnum(CamelCaseStrEnum):\n    OneTwoThree = auto()\n\n\nclass PascalTestEnum(PascalCaseStrEnum):\n    OneTwoThree = auto()\n\n\nclass KebabTestEnum(KebabCaseStrEnum):\n    OneTwoThree = auto()\n\n\nclass SnakeTestEnum(SnakeCaseStrEnum):\n    OneTwoThree = auto()\n\n\nclass MacroTestEnum(MacroCaseStrEnum):\n    OneTwoThree = auto()\n\n\nassert CamelTestEnum.OneTwoThree == \"oneTwoThree\"\nassert PascalTestEnum.OneTwoThree == \"OneTwoThree\"\nassert KebabTestEnum.OneTwoThree == \"one-two-three\"\nassert SnakeTestEnum.OneTwoThree == \"one_two_three\"\nassert MacroTestEnum.OneTwoThree == \"ONE_TWO_THREE\"\n```\n\nAs with any Enum you can, of course, manually assign values.\n\n```python\nfrom strenum import StrEnum\n\n\nclass Shape(StrEnum):\n    CIRCLE = \"Circle\"\n\n\nassert Shape.CIRCLE == \"Circle\"\n```\n\nDoing this with the case-changing classes, though, won't manipulate\nvalues--whatever you assign is the value they end up with.\n\n```python\nfrom strenum import KebabCaseStrEnum\n\n\nclass Shape(KebabCaseStrEnum):\n    CIRCLE = \"Circle\"\n\n\n# This will raise an AssertionError because the value wasn't converted to kebab-case.\nassert Shape.CIRCLE == \"circle\"\n```\n\n## Contributing\n\nPull requests are welcome. For major changes, please open an issue first to\ndiscuss what you would like to change.\n\nPlease ensure tests pass before submitting a PR. This repository uses\n[Black](https://black.readthedocs.io/en/stable/) and\n[Pylint](https://www.pylint.org/) for consistency. Both are run automatically\nas part of the test suite.\n\n## Running the tests\n\nTests can be run using `make`:\n\n```\nmake test\n```\n\nThis will create a virutal environment, install the module and its test\ndependencies and run the tests. Alternatively you can do the same thing\nmanually:\n\n```\npython3 -m venv .venv\n.venv/bin/pip install .[test]\n.venv/bin/pytest\n```\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n**N.B. Starting with Python 3.11, `enum.StrEnum` is available in the standard\nlibrary. This implementation is _not_ a drop-in replacement for the standard\nlibrary implementation. Specifically, the Python devs have decided to case fold\nname to lowercase by default when `auto()` is used which I think violates the\nprinciple of least surprise.**\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "An Enum that inherits from str.",
    "version": "0.4.15",
    "project_urls": {
        "Homepage": "https://github.com/irgeek/StrEnum"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8169297302c5f5f59c862faa31e6cb9a4cd74721cd1e052b38e464c5b402df8b",
                "md5": "47a901c7ac420ca5c0524f1dd3b8ec35",
                "sha256": "a30cda4af7cc6b5bf52c8055bc4bf4b2b6b14a93b574626da33df53cf7740659"
            },
            "downloads": -1,
            "filename": "StrEnum-0.4.15-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "47a901c7ac420ca5c0524f1dd3b8ec35",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8851,
            "upload_time": "2023-06-29T22:02:56",
            "upload_time_iso_8601": "2023-06-29T22:02:56.947495Z",
            "url": "https://files.pythonhosted.org/packages/81/69/297302c5f5f59c862faa31e6cb9a4cd74721cd1e052b38e464c5b402df8b/StrEnum-0.4.15-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "85ad430fb60d90e1d112a62ff57bdd1f286ec73a2a0331272febfddd21f330e1",
                "md5": "aa5e934c299dac8731c6db4008deab4d",
                "sha256": "878fb5ab705442070e4dd1929bb5e2249511c0bcf2b0eeacf3bcd80875c82eff"
            },
            "downloads": -1,
            "filename": "StrEnum-0.4.15.tar.gz",
            "has_sig": false,
            "md5_digest": "aa5e934c299dac8731c6db4008deab4d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 23384,
            "upload_time": "2023-06-29T22:02:58",
            "upload_time_iso_8601": "2023-06-29T22:02:58.399318Z",
            "url": "https://files.pythonhosted.org/packages/85/ad/430fb60d90e1d112a62ff57bdd1f286ec73a2a0331272febfddd21f330e1/StrEnum-0.4.15.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-29 22:02:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "irgeek",
    "github_project": "StrEnum",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "strenum"
}
        
Elapsed time: 0.08433s