Skip to main content

String-based Enum class that automatically assigns values

Project description

A utility for producing enums with automatic names

This package provides an extension of python Enum objects that automatically assigns values to members. This uses the auto() feature to assign text values to the enums instead of having to manually set them.

Specifying your enum

For example, you might create an enum with this like so:

class Aliens(AutoNameEnum):
    JAWA = auto()
    EWOK = auto()
    HUTT = auto()
    PYKE = auto()

Getting values

Using the class, verify the value of JAWA would be 'JAWA':

>>> print(Aliens.JAWA.value)
'JAWA'

You may also get the same value by just using the name of the item:

>>> print(Aliens.JAWA)
'JAWA'

Iterating

Python enums may be iterated over:

for alien in Aliens:
    print(f"name: {alien.name}, value: {alien.value}")

For more information on enums (and the auto method), see [the official docs] (https://docs.python.org/3/library/enum.html)

Mixins

There are two mixins provided that change the behavior of auto():

  • LowerCaseMixin: values produced by auto() are in all lower-case
  • TitleCaseMixin: values produced by auto() will be in title- case (lower-case except for first letter)

When these mixins are used, they must be included after AutoNameEnum in the class inheritance declaration:

class Aliens(AutoNameEnum, TitleCaseMixin)
    JAWA = auto()
    EWOK = auto()
    HUTT = auto()
    PYKE = auto()

Documented members with autodoc()

The autodoc() function works like auto(), but also attaches a description to each enum member. This is useful for self-documenting enums where members need human-readable explanations:

class Aliens(AutoNameEnum):
    JAWA = autodoc("A small, rodent-like alien from Tatooine")
    EWOK = autodoc("A furry, diminutive alien from the forest moon of Endor")
    HUTT = autodoc("A large, slug-like alien from Nal Hutta")
    PYKE = autodoc("A secretive, spice-dealing alien from Oba Diah")

The value works just like auto():

>>> print(Aliens.JAWA.value)
'JAWA'

Access the description via the .description property:

>>> print(Aliens.JAWA.description)
'A small, rodent-like alien from Tatooine'

You can mix auto() and autodoc() in the same enum. Members created with auto() will return None for .description:

class Aliens(AutoNameEnum):
    JAWA = autodoc("A small, rodent-like alien from Tatooine")
    EWOK = auto()
    HUTT = autodoc("A large, slug-like alien from Nal Hutta")
>>> print(Aliens.JAWA.description)
'A small, rodent-like alien from Tatooine'
>>> print(Aliens.EWOK.description)
None

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

auto_name_enum-4.0.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

auto_name_enum-4.0.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file auto_name_enum-4.0.0.tar.gz.

File metadata

  • Download URL: auto_name_enum-4.0.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for auto_name_enum-4.0.0.tar.gz
Algorithm Hash digest
SHA256 567c8df4f150dab2ef549c450d4a10fd84bf0eef76218ee7c715e0933a8d9df5
MD5 bb608cb6d6d80fe263070ca019ef3b54
BLAKE2b-256 93544eb6a4b5595aebae6209c8a7d9866c02776e15e67bb71c64db38b461ffd9

See more details on using hashes here.

File details

Details for the file auto_name_enum-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: auto_name_enum-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for auto_name_enum-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed5d2b18239fa9b43f0c85eb4a118736ce0e01148fbe785d28fdac25945d915f
MD5 f91644a2b3b21ebf181b6f0301120755
BLAKE2b-256 cc61c20282be44acf81ca5eda7a6f3ea8f5fef6a1c2fe160eacb7bda033520c6

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