Skip to main content

A small library for adding total orderings to enums

Project description

ordered_enum

CI Downloads

ordered_enum is a small library for adding (total) orderings to enum.Enums.

It provides two ordering behaviors:

  • ordered_enum.OrderedEnum: total ordering by definition
  • ordered_enum.ValueOrderedEnum: "total" ordering by member values

Installation

ordered_enum requires Python 3.9 or newer.

pip3 install ordered_enum

Usage

To use ordered_enum, just use OrderedEnum or ValueOrderedEnum as your parent class:

from ordered_enum import OrderedEnum


class State(OrderedEnum):
    Disabled = 4
    Loaded = 3
    Waiting = 2
    Running = 1
    Dead = 0


assert(State.Disabled < State.Loaded)
assert(sorted([State.Dead, State.Waiting]) == [State.Waiting, State.Dead])

OrderedEnum doesn't require @enum.unique (or unique values at all); it uses the order of definition to impose an ordering between members.

If you'd like to impose an ordering based on member values, you can use ValueOrderedEnum instead:

import enum
from ordered_enum import ValueOrderedEnum


@enum.unique
class State(ValueOrderedEnum):
    Disabled = 4
    Loaded = 3
    Waiting = 2
    Running = 1
    Dead = 0


assert(State.Disabled > State.Loaded)
assert(sorted([State.Waiting, State.Dead]) == [State.Dead, State.Waiting])

ValueOrderedEnum does require unique values, which can be enforced via @enum.unique. Failing to make a ValueOrderedEnum's values unique will result in a silently broken ordering.

Caveats

As mentioned above, ordered_enum.OrderedEnum provides an ordering of enum values based on their order of definition in the class. This means that:

  1. Enum values doesn't have to be unique for ordered_enum to work
  2. Enum values can be heterogeneously typed
  3. Moving enum values around changes their ordering

Therefore, you should either not depend on a specific ordering or ensure that your order of definition is the order you'd like.

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

ordered_enum-0.0.10.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

ordered_enum-0.0.10-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file ordered_enum-0.0.10.tar.gz.

File metadata

  • Download URL: ordered_enum-0.0.10.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ordered_enum-0.0.10.tar.gz
Algorithm Hash digest
SHA256 6979a367a7f252353eef4d5cd4c3c00c2178a421b52cc5f7cb2ee0d412d7f05b
MD5 056ea92895e1f264e2755b34af182c07
BLAKE2b-256 34208d165f16c2ef9d5bc945571898a0c99d3755bc401a1c57517318ba43a58f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordered_enum-0.0.10.tar.gz:

Publisher: release.yml on woodruffw/ordered_enum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordered_enum-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: ordered_enum-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ordered_enum-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 f398723003f43bc19ef0e308f687149814a9889e7b81dd1947c556daa94d547e
MD5 6ab423b487ee137a735d97534b900968
BLAKE2b-256 6c3155f391e6039353ccb97072ab990f21bde06676d1df295310cca51141d510

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordered_enum-0.0.10-py3-none-any.whl:

Publisher: release.yml on woodruffw/ordered_enum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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