Giter VIP home page Giter VIP logo

ordered_enum's Introduction

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.

ordered_enum's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar iliastsa avatar sasacocic avatar woodruffw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ordered_enum's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.