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.7 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.8.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

ordered_enum-0.0.8-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ordered_enum-0.0.8.tar.gz
Algorithm Hash digest
SHA256 6f4f1523cc6e68967b350efa905cd80d4f3de5542a1b8ca1574b1159d30dfc45
MD5 05508fab671b2195914ca7bb1197a7ae
BLAKE2b-256 053601197d5890411e83c05285c3326197962ec240d2ec29cd31a6e9151a7764

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ordered_enum-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for ordered_enum-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 8670ecebc3f5849751cac0ab3097f0e52549eae44efbf2643f572bb48cec7520
MD5 f0c8c4c1bfd7b458357c269c97ad42b3
BLAKE2b-256 5a854e61cc89b90ec5962e4d46b30fccb4b341d1eeb2c0e8da230fc1967310e3

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