Giter Club home page Giter Club logo

richenum's Introduction

richenum

Build Status

Latest PyPI Version Python versions Pypi Downloads

About

A enum library for Python.

enum
A simple enum implementation that maps a "variable" to a constant.
RichEnum
An enum implementation that offers more functionality than a basic enum, hence the name: RichEnum. Provided functionality include specifying a canonical name and a display name. The canonical name should be used if you need to do a lookup or reference in your code. The display name should be used if you need to display text to a user.
OrderedRichEnum
Exactly like RichEnum but also has an index specified for each enum value. Also, iteration over an OrderedRichEnum will be sorted (ascending) by the enum value's index.

Links

Installation

$ pip install richenum

Example Usage

enum

>>> from richenum import enum
>>> MY_ENUM = enum(FOO=1, BAR=2)
>>> MY_ENUM.FOO
1
>>> MY_ENUM.BAR
2

RichEnum

>>> from richenum import RichEnum, RichEnumValue
>>> class MyRichEnum(RichEnum):
...    FOO = RichEnumValue(canonical_name="foo", display_name="Foo")
...    BAR = RichEnumValue(canonical_name="bar", display_name="Bar")
...
>>> MyRichEnum.FOO
RichEnumValue - canonical_name: 'foo'  display_name: 'Foo'
>>> MyRichEnum.from_canonical("foo")
RichEnumValue - canonical_name: 'foo'  display_name: 'Foo'

OrderedRichEnum

>>> from richenum import OrderedRichEnum, OrderedRichEnumValue
>>> class MyOrderedRichEnum(OrderedRichEnum):
...    FOO = OrderedRichEnumValue(index=1, canonical_name="foo", display_name="Foo")
...    BAR = OrderedRichEnumValue(index=2, canonical_name="bar", display_name="Bar")
...
>>> MyOrderedRichEnum.FOO
OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
>>> MyOrderedRichEnum.from_canonical("foo")
OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
>>> MyOrderedRichEnum.from_index(1)
OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'

Related Packages

django-richenum

Makes RichEnum and OrderedRichEnum available in as model fields and form fields in Django.

enum
Starting with Python 3.4, there is a standard library for enumerations. This class has a similar API, but is not directly compatible with that class.

Contributing

  1. Fork the repo from GitHub.
  2. Make your changes.
  3. Add unittests for your changes.
  4. Run pep8, pyflakes, and pylint to make sure your changes follow the Python style guide and doesn't have any errors.
  5. Add yourself to the AUTHORS file (in alphabetical order).
  6. Send a pull request from your fork to the main repo.

richenum's People

Contributors

afahim avatar akshayjshah avatar asherf avatar cbueb avatar csizmaziakiki avatar jemiahlee avatar lopezm1 avatar matthewspeck avatar radaron avatar samv avatar wyguy444 avatar

Stargazers

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

Watchers

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

richenum's Issues

Support aliases

Features:

  • Aliases defined in the RichEnumValue
  • Aliases are unique for a given RichEnumValue
  • RichEnum metaclass enforces that aliases are unique across all RichEnumValues in the given RichEnum
  • Backwards compatible with existing RichEnums

LookupError not actually returned

@rbm added a custom exception class for bad lookups, which I think is great. The problem is that it doesn't currently behave as a built-in LookupError, though the error message printed makes it look like it does.

If you change the test on line 76 of test_rich_enums.py to be:

        with self.assertRaises(LookupError):

instead of just using the custom class type (EnumLookupError), you get an error raised in the test:

======================================================================
ERROR: test_generic_lookup (tests.richenum.test_rich_enums.RichEnumTestSuite)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jeremiahlee/projects/richenum/tests/richenum/test_rich_enums.py", line 77, in test_generic_lookup
    Vegetable.lookup('flavor', 'yum')
  File "/Users/jeremiahlee/projects/richenum/src/richenum/enums.py", line 284, in lookup
    % (field, value, cls)
LookupError: Could not find member matching flavor = yum in enum <class 'tests.richenum.test_rich_enums.Vegetable'>

This is throwing something that has a __name__ of LookupError, but isn't actually the built-in LookupError, which I think is confusing to users of the library (e.g., me). I know that we could also catch Vegetable.LookupError (which isn't obvious from the error message above) but I think it also makes sense to allow a plain LookupError to be caught.

PR forthcoming.

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.