Skip to main content

Python's Enum with extra powers to play nice with labels and choices fields

Project description

Choices Enum

https://img.shields.io/pypi/v/choicesenum.svg https://travis-ci.org/loggi/python-choicesenum.svg?branch=master Documentation Status

Python’s Enum with extra powers to play nice with labels and choices fields.

Installation

Install choicesenum using pip:

$ pip install choicesenum

Features

  • An ChoicesEnum that can be used to create constant groups.

  • ChoicesEnum can define labels to be used in choices fields.

  • Django fields included: EnumCharField and EnumIntegerField.

  • Support (tested) for Python 2.7, 3.4, 3.5 and 3.6.

  • Support (tested) for Django 1.6.1 (with south), 1.7, 1.8, 1.9, 1.10, 1.11 and 2.0.

Usage examples

Example with HttpStatuses:

class HttpStatuses(ChoicesEnum):
    OK = 200
    BAD_REQUEST = 400
    UNAUTHORIZED = 401
    FORBIDDEN = 403

All Enum types can be compared against their values:

assert HttpStatuses.OK == 200
assert HttpStatuses.BAD_REQUEST == 400
assert HttpStatuses.UNAUTHORIZED == 401
assert HttpStatuses.FORBIDDEN == 403

status_code = HttpStatuses.OK
assert 200 <= status_code <= 300

All Enum types have by default a display derived from the enum identifier:

assert HttpStatuses.OK.display == 'Ok'
assert HttpStatuses.BAD_REQUEST.display == 'Bad request'
assert HttpStatuses.UNAUTHORIZED.display == 'Unauthorized'
assert HttpStatuses.FORBIDDEN.display == 'Forbidden'

You can easily define your own custom display for an Enum item using a tuple:

class HttpStatuses(ChoicesEnum):
    OK = 200, 'Everything is fine'
    BAD_REQUEST = 400, 'You did a mistake'
    UNAUTHORIZED = 401, 'I know your IP'
    FORBIDDEN = 403

assert HttpStatuses.OK.display == 'Everything is fine'
assert HttpStatuses.BAD_REQUEST.display == 'You did a mistake'
assert HttpStatuses.UNAUTHORIZED.display == 'I know your IP'
assert HttpStatuses.FORBIDDEN.display == 'Forbidden'

You can declare custom properties and methods:

class HttpStatuses(ChoicesEnum):
    OK = 200, 'Everything is fine'
    BAD_REQUEST = 400, 'You did a mistake'
    UNAUTHORIZED = 401, 'I know your IP'
    FORBIDDEN = 403

    @property
    def is_error(self):
        return self >= self.BAD_REQUEST

assert HttpStatuses.OK.is_error is False
assert HttpStatuses.BAD_REQUEST.is_error is True
assert HttpStatuses.UNAUTHORIZED.is_error is True

Example with Colors:

from choicesenum import ChoicesEnum

class Colors(ChoicesEnum):
    # For fixed order in  py2.7, py3.4+ are ordered by default
    _order_ = 'RED GREEN BLUE'

    RED = '#f00', 'Vermelho'
    GREEN = '#0f0', 'Verde'
    BLUE = '#00f', 'Azul'

assert Colors.RED == '#f00'
assert Colors.GREEN == '#0f0'
assert Colors.BLUE == '#00f'

assert Colors.RED.display == 'Vermelho'
assert Colors.GREEN.display == 'Verde'
assert Colors.BLUE.display == 'Azul'

Use .choices() method to receive a list of tuples (item, display):

# choices
assert list(Colors.choices()) == [
    ('#f00', 'Vermelho'),
    ('#0f0', 'Verde'),
    ('#00f', 'Azul'),
]

For each enum item, a dynamic property is_<enum_item> is generated to allow quick boolean checks:

color = Colors.RED
assert color.is_red
assert not color.is_blue
assert not color.is_green

if color.is_red:
    print 'Is red!'

The enum item can be used whenever the value is needed:

assert u'Currrent color is {c} ({c.display})'.format(c=color) ==\
       u'Currrent color is #f00 (Vermelho)'

Even in dicts and sets, as it shares the same hash() from his value:

d = {
    HttpStatuses.OK.value: "nice",
    HttpStatuses.BAD_REQUEST.value: "bad",
    401: "Don't do this",
}
assert d[HttpStatuses.OK] == "nice"
assert d[HttpStatuses.OK.value] == "nice"
assert d[HttpStatuses.OK] == d[HttpStatuses.OK.value]
assert d[HttpStatuses.UNAUTHORIZED] == d[401]

Usage with the custom Django fields:

from django.db import models
from choicesenum.django.fields import EnumCharField

class ColorModel(models.Model):
    color = EnumCharField(
        max_length=100,
        enum=Colors,
        default=Colors.GREEN,
    )

instance = ColorModel()
assert instance.color ==  Colors.GREEN
assert instance.color.is_green is True
assert instance.color.value == Colors.GREEN.value == '#0f0'
assert instance.color.display == Colors.GREEN.display

instance.color = '#f00'
assert instance.color == '#f00'
assert instance.color.value == '#f00'
assert instance.color.display == 'Vermelho'

Is guaranteed that the field value is always a ChoicesEnum item. Pay attention that the field will only accept valid values for the Enum in use, so if your field allow null, your enum should also:

from django.db import models
from choicesenum import ChoicesEnum
from choicesenum.django.fields import EnumIntegerField

class UserStatus(ChoicesEnum):
    UNDEFINED = None
    PENDING = 1
    ACTIVE = 2
    INACTIVE = 3
    DELETED = 4


class User(models.Model):
    status = EnumIntegerField(enum=UserStatus, null=True, )

instance = User()
assert instance.status.is_undefined is True
assert instance.status.value is None
assert instance.status == UserStatus.UNDEFINED
assert instance.status.display == 'Undefined'

# again...
instance.status = None
assert instance.status.is_undefined is True

Usage with Graphene Enums:

UserStatusEnum = graphene.Enum.from_enum(UserStatus)

History

0.3.0 (2018-06-22)

  • Official Django 2.0 support (0.2.2 just works fine too).

  • ChoicesEnum sharing the same hash() as his value. Can be used to retrieve/restore items in dicts (d[enum] == d[enum.value]).

0.2.2 (2017-12-01)

  • Fix: Support queries through select_related with no None value defined (thanks @klette).

0.2.1 (2017-09-30)

  • Fix South migrations for Django 1.6.

0.2.0 (2017-09-11)

  • ChoicesEnum items are comparable by their values (==, !=, >, >=, <, <=) (thanks @jodal).

  • +``ChoicesEnum.values``: Returns all the Enum’s raw values (eq: [x.value for x in Enum]).

0.1.7 (2017-09-10)

  • Fix: ChoicesEnum is now hashable (thanks @jodal).

0.1.6 (2017-09-08)

  • Fix: Proxy __len__ calls to the inner enum value.

0.1.5 (2017-09-05)

  • +ChoicesEnum.description: Alias for label, allow enum descriptors to be used by Graphene.

0.1.4 (2017-08-28)

  • Fix South migrations for Django 1.6.

  • ChoicesEnum repr can be used to reconstruct an instance (item == eval(repr(item))).

0.1.3 (2017-08-28)

  • Fix sdist not including sub-modules (django contrib).

0.1.2 (2017-08-27)

  • README fixes and improvements.

0.1.0 (2017-08-27)

  • First release on PyPI.

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

choicesenum-0.3.0.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

choicesenum-0.3.0-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file choicesenum-0.3.0.tar.gz.

File metadata

  • Download URL: choicesenum-0.3.0.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for choicesenum-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7c18ffc4683180718aef93e4369ca4b6313f94d4eb9c6c67db563b1ad5ce91af
MD5 78386849e8ba95d761ee66b4ec24f8b1
BLAKE2b-256 59c22b2ef182f5dc4a56e3debfdb516913568e08465cef83f540953c6e0eba1a

See more details on using hashes here.

File details

Details for the file choicesenum-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for choicesenum-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e102b60c557b3f9b92b9848b8aff4331dec62e13c266dff00de3fb1d60e98ba1
MD5 a9446a77295f67891f6cc45b853d1e0b
BLAKE2b-256 62f5a95652fb23bfa69f703cd9d9a054ff10554af456d8354054fb19cfeb2e35

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