Code Monkey home page Code Monkey logo

Comments (2)

tshabs avatar tshabs commented on July 18, 2024

Just trying to maintain compatibility where convenient. For enum could use https://pypi.python.org/pypi/enum34/ for py2 (providing it doesn't cause problems)

from essp.

0xjac avatar 0xjac commented on July 18, 2024

After some thinking I would advise to use the aenum package which is written by the same guy as the stdlib.enum and enum34 packages. It offers some advanced features and should solve all of the issues, including py2/py3 compatibility.

Here is a sample definition which suits your use case:

from aenum import IntEnum


class Status(IntEnum):
    _init_ = 'value', 'debug_message'
    
    OK = 1, "debug message ok"
    FAIL = 2, "debug message fail"

The _init_ takes a sequence of attributes to set on each Enum item. Then you just pass the actual values to each item.

Note that the first attribute passed to _init_ must be value.

I used IntEnum as the parent class in the example as this makes the enum a subclass of int as well but you can just use Enum if you prefer it.

In the end you should be able to use it like this with py2/py3:

assert Status.OK == Status(1)
assert Status.OK.name == 'OK'
assert Status.OK.value == 1
assert Status.OK.debug_message == "debug message ok"

from essp.

Related Issues (19)

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.