Skip to main content

Advanced Enumerations (compatible with Python's stdlib Enum) and NamedTuples

Project description

An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over.

A NamedTuple is a class-based, fixed-length tuple with a name for each possible position accessible using attribute-access notation.

Module Contents

This module defines five enumeration classes that can be used to define unique sets of names and values, one Enum class decorator, and one named tuple class

Enum

Base class for creating enumerated constants.

IntEnum

Base class for creating enumerated constants that are also subclasses of int.

AutoNumberEnum

Derived class that automatically assigns an int value to each member.

OrderedEnum

Derived class that adds <, <=, >=, and > methods to an Enum.

UniqueEnum

Derived class that ensures only one name is bound to any one value.

unique

Enum class decorator that ensures only one name is bound to any one value.

NamedTuple

Base class for creating NamedTuples, either by subclassing or via it’s functional API.

Creating an Enum

Enumerations can be created using the class syntax, which makes them easy to read and write. To define an enumeration, subclass Enum as follows:

>>> from aenum import Enum
>>> class Color(Enum):
...     red = 1
...     green = 2
...     blue = 3

The Enum class is also callable, providing the following functional API:

>>> Animal = Enum('Animal', 'ant bee cat dog')
>>> Animal
<enum 'Animal'>
>>> Animal.ant
<Animal.ant: 1>
>>> Animal.ant.value
1
>>> list(Animal)
[<Animal.ant: 1>, <Animal.bee: 2>, <Animal.cat: 3>, <Animal.dog: 4>]

Creating NamedTuples

Simple

The most common way to create a new NamedTuple will be via the functional API:

>>> from aenum import NamedTuple
>>> Book = NamedTuple('Book', 'title author genre', module=__name__)

Advanced

The simple method of creating NamedTuples requires always specifying all possible arguments when creating instances; failure to do so will raise exceptions.

However, it is possible to specify both docstrings and default values when creating a NamedTuple using the class method:

>>> class Point(NamedTuple):
...     x = 0, 'horizontal coordinate', 0
...     y = 1, 'vertical coordinate', 0
...
>>> Point()
Point(x=0, y=0)

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

aenum-1.0.zip (60.8 kB view details)

Uploaded Source

aenum-1.0.tar.gz (58.1 kB view details)

Uploaded Source

File details

Details for the file aenum-1.0.zip.

File metadata

  • Download URL: aenum-1.0.zip
  • Upload date:
  • Size: 60.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aenum-1.0.zip
Algorithm Hash digest
SHA256 2eca757c29891d67362e2a8cf70a691d4c1debd1ea97ac627db08cecb87a8409
MD5 4c74546bead5d2edf9a34d0f9a803769
BLAKE2b-256 1fff0ab47e2ea546c297a2ce82b029483282be20951db2058987d4b7f148ccc1

See more details on using hashes here.

File details

Details for the file aenum-1.0.tar.gz.

File metadata

  • Download URL: aenum-1.0.tar.gz
  • Upload date:
  • Size: 58.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aenum-1.0.tar.gz
Algorithm Hash digest
SHA256 437ccd02c85fece5b245231ebd978ac1d16acb80178ea56ab3b073145083bf03
MD5 01d3a6943041bfba8f8cd297654224a2
BLAKE2b-256 85144c185cce3ec9201b42b222ada944df7f77bd158a51ed5d473131f4d80b62

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