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.2.1.zip (53.6 kB view details)

Uploaded Source

aenum-1.2.1.tar.gz (50.2 kB view details)

Uploaded Source

File details

Details for the file aenum-1.2.1.zip.

File metadata

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

File hashes

Hashes for aenum-1.2.1.zip
Algorithm Hash digest
SHA256 4fe587f9c475239a93c994ae7b08de161c5810ad8725eec1c529452d3868a859
MD5 bd99a224e48b49b1bc3c883697d5fa23
BLAKE2b-256 d31fa7fd6562aeab87a8dab8f668b6d74d05e41bcf74f2c01fad01310d52cc98

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for aenum-1.2.1.tar.gz
Algorithm Hash digest
SHA256 130180615875e21343b859cf085c54b968d9e9496ecd2007fe31577c3b3fe350
MD5 2a749ac4686ed18633dc774e2ee94d1b
BLAKE2b-256 01bcf1b15f6a310eadd68937e4aa7656b46cc64567e0c7709fa47b065dd5f257

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