Skip to main content

A small library for adding total orderings to enums

Project description

ordered_enum

Build Status 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.6 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.7.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

ordered_enum-0.0.7-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ordered_enum-0.0.7.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for ordered_enum-0.0.7.tar.gz
Algorithm Hash digest
SHA256 c06595263d09dc31d60006d212692dccaf623125774efd84bc01f90d3cb71102
MD5 9ddb40c6f5ea365b846e3018aea398a4
BLAKE2b-256 7f3cc680570a8b19575ba4936cdf647a5bae21f99047025e01989f591889eb83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ordered_enum-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d39168064e555d74771854e7561f36711faa32f9044f190de6f1c4f34cc4c396
MD5 46851383e7f8e2323565ccc9136566c1
BLAKE2b-256 1f5ba8ae9f2de9ed1b1f8269979f35c8fe0c41595736bde2b1a8c8b621791c1a

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