Skip to main content

Enums with arbitrary lookup tables

Project description

mapped-enum

PyPI version Python package

Enums that can easily map to and from arbitrary values of another type.

Summary

There are many cases in Python where you want to asscociate an Enum class with more meaningful values than the default integers from auto(). Python allows assignment of anything as a value of an enum member, but there is still no indication of what that value represents. When accessing tuple values, one can only refer to each tuple member by its index, creating further ambiguity.

mapped-enum seeks to solve that problem by dynamically populating the enum keys with custom value names. For example, you can have a DaysOfWeek enum with fields index, short_name, and full_name. When that enum is mapped using this module, accessing those values becomes as simple as:

>>> DaysOfWeek.MONDAY.to_short_name()
'mon'
>>> DaysOfWeek.TUESDAY.to_index()
1
>>> DaysOfWeek.FRIDAY.to_full_name()
'Friday'

Mapping in the other direction is just as easy:

>>> DaysOfWeek.from_short_name('mon')
<DaysOfWeek.MONDAY: (0, 'mon', 'Monday')>

Usage

This module provides the enum_map function, which is to be used as an annotation on a class inherting from Enum. The annotation takes one required argument keys, an array of strings representing the names of the fields to generate. The value of each enum member should be a tuple, with the data arranged such that each tuple member corresponds to a string name from the annotation argument.

For each key specified in the keys parameter, a method called to_<key> and a class method called from_<key> will be added to the enum. These convert to and from the enum values at the index of the keyword.

Generated methods

to_<key> is called on an enum member and takes no arguments. It returns the indexed member of the enum value tuple corresponding to the index of the keyword.

from_<key> is called directly on the enum class. It takes a single argument, the value to search for enum members with the value index corresponding to the index of the keyword. If there is no enum member that satisfies the requested value, it will return None.

Optional arguments

The annotation can take a number of optional arguments to modify its behavior.

Parameter name Type Purpose
to_prefix str An alternative prefix for the conversion methods instead of to_.
from_prefix str An alternative prefix for the lookup methods instead of from_.
allow_override bool If True, if the enum defines methods with the same name as a possible enum_map method, the enum_map method will be silently overridden. Otherwise, an error will be thrown.
multiple_from bool If True, calls to a from_ method with multiple members matching the search term will return all of those members; consequently, such a call with no matching members will return an empty list. If False, then calls to a from_ method will return the first matching term, or None if no match is found.

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

mapped-enum-0.3.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

mapped_enum-0.3.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file mapped-enum-0.3.2.tar.gz.

File metadata

  • Download URL: mapped-enum-0.3.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.48.0 CPython/3.7.3

File hashes

Hashes for mapped-enum-0.3.2.tar.gz
Algorithm Hash digest
SHA256 387bcd6af4c5eced6ca9a8a93bf2e950fd29d4b69d4c03660dc95fc4d65c00cd
MD5 fe4ec9611a424f8774f26f15fc7cb888
BLAKE2b-256 69d7b010be79a780180bf36b106e89a1d4a27e8fc7664303624ccc2896244f71

See more details on using hashes here.

File details

Details for the file mapped_enum-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: mapped_enum-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.48.0 CPython/3.7.3

File hashes

Hashes for mapped_enum-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 736a03c3e86e38de576764a1a50a27a471f673e5a37feb5f0b9ec89ba796e94d
MD5 d578ef19025f01a4d6533cb59e0cac0b
BLAKE2b-256 714d6e40e32acbedbb2e9e7249528532ea552cbde2b05f187c5bed21e9586459

See more details on using hashes here.

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