Skip to main content

Enumerator type supporting multiple equivalent names

Project description

An enumeration type supporting multiple equivalent names.

Rationale

I find the types defined in the built-in enum type in Python 3 to be clumsy and hard to use for my purposes. Further, I have a project which repeatedly requires me to resolve strings to numbers and then back to sometimes different strings, based on the context. This module is the result of that.

The implementation subclasses int, which allows for the value of the enumeration to be stored in a minimal amount of space, while providing a number of attributes of the object for converting it to different names.

Usage

Subclasses should define the class attribute _members as a tuple of tuples. [1] Every member of _members should be a tuple of strings containing a name which equates to the enumerated value. The value will be inferred from the member’s position in the tuple (equal to the subscript). Optionally, a subclass may define _fields, a tuple of field names which correspond to the order the enumeration names appear in within the _members attribute.

The enumerated value can be instantiated with a plain int value or with any of the names provided in _members, either using the appropriate field name as a keyword or, so long as it is unique across all fields, as an un-keyworded argument. There is no test to ensure that names are unique – if a name appears multiple times in the _members tuples, the behavior is undefined.

By default, the first in each individual enumerated tuple will be returned if the value is cast as a string (using the str() function, for instance). For compatability with the types in the enum module, the same value will be returned using the name attribute. The other names are available as a tuple in the _names attribute, and if _fields was defined, will be available as the value of attributes corresponding to the relevant field name in _fields.

If more than one argument is given on instantiation, either keyworded or not, a ValueError exception will be raised unless all arguments resolve to the same value.

The MultiEnum type makes an attempt to be idempotent – i.e., if x is an MultiEnum, x is MultiEnum(x) should be true. However, in the case of multiple init values, idempotency is only preserved for the first positional parameter. x is MultiEnum(a, x) will not be true, even if a and x are equal.

Example

>>> from multienum import MultiEnum
>>> class SampleEnum(MultiEnum):
...   _members = (("zero", "zip", "zéro", "cero"),
...               ("one", "ace", "une", "uno"),
...               ("two", "deuce", "deux", "dos"))
...   _fields = ('english', 'slang', 'french', 'spanish')
>>> val1 = SampleEnum("one")
>>> int(val1)
1
>>> str(val1)
'one'
>>> val1.name
'one'
>>> val1.spanish
'uno'
>>> val2 = SampleEnum(slang="deuce")
>>> int(val2)
2
>>> try:
...   se = SampleEnum("two", spanish="cero")
... except ValueError:
...   print("Value mismatch")
Value mismatch

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

multienum-0.2.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file multienum-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for multienum-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b85f40101bd156c93b5fd98fe9fccb80cc0912546450d6e89e25dcf26eae7f78
MD5 f6d78b6441e29ba107fd0edb0e3121bf
BLAKE2b-256 d93d8583563abe020c3ac51e6ccb137d04fd851cc5f8b34e88973ea565301444

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