Skip to main content

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

Project description

stringenum

PyPI - Version PyPI - Python Version License Checked with mypy Ruff

GitHub Workflow Status (with event) GitHub Actions Workflow Status codecov

Table Of Contents

About

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

Installation

stringenum is available on PyPI, so you can simply use pip to install it.

pip install stringenum

Usage

  • stringenum.StrEnum - A backport of 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

      >>> 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

      >>> 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

      >>> 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.

    >>> class Fruits(DuplicateFreeStrEnum):
    ...     APPLE = "apple"
    ...     BANANA = "banana"
    ...     ORANGE = "apple"
    ...
    Traceback (most recent call last):
      ...
    ValueError: Duplicate values are not allowed in Fruits: <Fruits.ORANGE: 'apple'>
    
  • stringenum.CaseInsensitiveStrEnum - A subclass of DuplicateFreeStrEnum that supports case-insensitive lookup.

    >>> class Pet(CaseInsensitiveStrEnum):
    ...     CAT = "meow"
    ...     DOG = "bark"
    
    >>> Pet("Meow")
    <Pet.CAT: 'meow'>
    
    >>> Pet("BARK")     
    <Pet.DOG: 'bark'>
    
    >>> Pet["Cat"]
    <Pet.CAT: 'meow'>
    
    >>> Pet["dog"] 
    <Pet.DOG: 'bark'>
    
  • stringenum.DoubleSidedStrEnum - A subclass of DuplicateFreeStrEnum that supports double-sided lookup, allowing both member values and member names to be used for lookups.

    >>> class Status(DoubleSidedStrEnum):
    ...     PENDING = "waiting"
    ...     REJECTED = "denied"
    
    >>> Status("PENDING")
    <Status.PENDING: 'waiting'>
    
    >>> Status("waiting")
    <Status.PENDING: 'waiting'>
    
    >>> Status["REJECTED"]
    <Status.REJECTED: 'denied'>
    
    >>> Status["denied"]
    <Status.REJECTED: '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.

    >>> class Status(DoubleSidedCaseInsensitiveStrEnum):
    ...     PENDING = "waiting"
    ...     REJECTED = "denied"
    
    >>> Status("pending")
    <Status.PENDING: 'waiting'>
    
    >>> Status("Waiting")
    <Status.PENDING: 'waiting'>
    
    >>> Status["Rejected"]
    <Status.REJECTED: 'denied'>
    
    >>> Status["DenieD"]
    <Status.REJECTED: 'denied'>
    

License

Distributed under the MIT License. See LICENSE for more information.

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

stringenum-0.4.0.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

stringenum-0.4.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file stringenum-0.4.0.tar.gz.

File metadata

  • Download URL: stringenum-0.4.0.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for stringenum-0.4.0.tar.gz
Algorithm Hash digest
SHA256 14e8ae778e244806b4fd1f3435e6fcddc4c143462461e2f7c9e7f89e06806261
MD5 720f1c6393478d179ab774952dbf97ed
BLAKE2b-256 f213d0eb4b29be7b1dab167103f79b4bf89a56996f89798bcfae2164710fdd97

See more details on using hashes here.

Provenance

The following attestation bundles were made for stringenum-0.4.0.tar.gz:

Publisher: release.yml on Ravencentric/stringenum

Attestations:

File details

Details for the file stringenum-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: stringenum-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for stringenum-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad44e09dee62227ebb2d58eb4a727e74a06ce310672de55d5fef282956bb41f1
MD5 ca7103b9e3abf1468299a12f3527cc3b
BLAKE2b-256 8188a1e351d1f2d201b2495a35c57c71de450a3df9981aa6aefc1587d4d1b8a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for stringenum-0.4.0-py3-none-any.whl:

Publisher: release.yml on Ravencentric/stringenum

Attestations:

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