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.

Usage examples

Example of HttpStatuses:

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

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

assert HttpStatuses.OK.display == 'Ok'
assert HttpStatuses.BAD_REQUEST.display == 'Bad request'  # <- nice!
assert HttpStatuses.UNAUTHORIZED.display == 'Unauthorized'
assert HttpStatuses.FORBIDDEN.display == 'Forbidden'

Example of 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 == Colors.RED
assert Colors.GREEN == Colors.GREEN
assert Colors.BLUE == Colors.BLUE

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

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

# dynamic `is_<enum_item>` attrs
assert Colors.RED.is_red
assert Colors.GREEN.is_green
assert Colors.BLUE.is_blue

assert not Colors.RED.is_blue
assert not Colors.RED.is_green

Using with 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
assert instance.color.display == Colors.GREEN.display

# the field value is allways an ``ChoicesEnum`` item
instance.color ==  '#f00'
assert instance.color.display == 'Vermelho'
assert instance.color.value == '#f00'

# and still can be used where the value is needed
assert instance.color == '#f00'
assert u'Currrent color is {0} ({0.display})'.format(instance.color) ==\
    u'Currrent color is #f00 (Vermelho)'

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 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'

History

0.1.1 (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.1.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

choicesenum-0.1.1-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for choicesenum-0.1.1.tar.gz
Algorithm Hash digest
SHA256 615bdf0039a9c3d2cc8e3f3778302c87153b1fc9a84d73b167a6d64a5693ff54
MD5 6af71be355b8f8c177283b8172118cf0
BLAKE2b-256 cfdd05d17b4674d2561163e8fbbcfff7d36902cd3143611b5aaf1c2aff38f31b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for choicesenum-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d9f86e075f3924ed909bebddfcb504911d98c2dc81d1e70de71a98553254b33d
MD5 7d41b9d1c66b90d03bf5adeadf9db100
BLAKE2b-256 bf35e77b3657fd3b26ad56aa54db977fb1f271e4817d01fb253ab89cc868a74e

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