Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ravencentric/stringenum

A small, dependency-free library offering additional enum.StrEnum subclasses and a backport for older Python versions.
https://github.com/ravencentric/stringenum

enum strenum

Last synced: about 8 hours ago
JSON representation

A small, dependency-free library offering additional enum.StrEnum subclasses and a backport for older Python versions.

Awesome Lists containing this project

README

        

# stringenum

[![PyPI - Version](https://img.shields.io/pypi/v/stringenum?link=https%3A%2F%2Fpypi.org%2Fproject%2Fstringenum%2F)](https://pypi.org/project/stringenum/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/stringenum)
![License](https://img.shields.io/github/license/Ravencentric/stringenum)
![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)
![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)

![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/Ravencentric/stringenum/release.yml)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/ravencentric/stringenum/tests.yml?label=tests)
[![codecov](https://codecov.io/gh/Ravencentric/stringenum/graph/badge.svg?token=812Q3UZG7O)](https://codecov.io/gh/Ravencentric/stringenum)

## Table Of Contents

* [About](#about)
* [Installation](#installation)
* [Usage](#usage)
* [License](#license)

# About

A small, dependency-free library offering additional [enum.StrEnum](https://docs.python.org/3/library/enum.html#enum.StrEnum) subclasses and a backport for older Python versions.

## Installation

`stringenum` is available on [PyPI](https://pypi.org/project/stringenum/), so you can simply use [pip](https://github.com/pypa/pip) to install it.

```sh
pip install stringenum
```

# Usage

- `stringenum.StrEnum` - A backport of [`enum.StrEnum`](https://docs.python.org/3/library/enum.html#enum.StrEnum). While `StrEnum` was added in Python 3.11, version 3.12 brought changes to the `__contains__` method in `EnumType`, which also impacts `StrEnum`. `stringenum.StrEnum` includes this `__contains__` update from Python 3.12.

- `enum.StrEnum` on Python <=3.11
```py
>>> class Color(enum.StrEnum):
... RED = "RED"
... GREEN = "GREEN"

>>> Color.RED in Color
True
>>> "RED" in Color
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for 'in': 'str' and 'EnumType'
```

- `enum.StrEnum` on Python >=3.12
```py
>>> class Color(enum.StrEnum):
... RED = "RED"
... GREEN = "GREEN"

>>> Color.RED in Color
True
>>> "RED" in Color
True
>>> 12 in Color
False
```

- `stringenum.StrEnum` on Python >=3.9
```py
>>> class Color(stringenum.StrEnum):
... RED = "RED"
... GREEN = "GREEN"

>>> Color.RED in Color
True
>>> "RED" in Color
True
>>> 12 in Color
False
```

- `stringenum.DuplicateFreeStrEnum` - A subclass of `StrEnum` that ensures all members have unique values and names, raising a `ValueError` if duplicates are found.

```py
>>> class Fruits(DuplicateFreeStrEnum):
... APPLE = "apple"
... BANANA = "banana"
... ORANGE = "apple"
...
Traceback (most recent call last):
...
ValueError: Duplicate values are not allowed in Fruits:
```

- `stringenum.CaseInsensitiveStrEnum` - A subclass of `DuplicateFreeStrEnum` that supports case-insensitive lookup.

```py
>>> class Pet(CaseInsensitiveStrEnum):
... CAT = "meow"
... DOG = "bark"

>>> Pet("Meow")

>>> Pet("BARK")

>>> Pet["Cat"]

>>> Pet["dog"]

```

- `stringenum.DoubleSidedStrEnum` - A subclass of `DuplicateFreeStrEnum` that supports double-sided lookup, allowing both member values and member names to be used for lookups.

```py
>>> class Status(DoubleSidedStrEnum):
... PENDING = "waiting"
... REJECTED = "denied"

>>> Status("PENDING")

>>> Status("waiting")

>>> Status["REJECTED"]

>>> Status["denied"]

```

- `stringenum.DoubleSidedCaseInsensitiveStrEnum` - A subclass of `DuplicateFreeStrEnum` that supports case-insenitive double-sided lookup, allowing both member values and member names to be used for lookups.

```py
>>> class Status(DoubleSidedCaseInsensitiveStrEnum):
... PENDING = "waiting"
... REJECTED = "denied"

>>> Status("pending")

>>> Status("Waiting")

>>> Status["Rejected"]

>>> Status["DenieD"]

```

## License

Distributed under the [MIT](https://choosealicense.com/licenses/mit/) License. See [LICENSE](https://github.com/Ravencentric/stringenum/blob/main/LICENSE) for more information.