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/python-choicesenum.svg https://img.shields.io/travis/loggi/python-choicesenum.svg Documentation Status Updates

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

Work in progress.

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.

Usage examples

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

Example of HttpStatuses:

class HttpStatuses(ChoicesEnum):
    OK = 200
    BAD_REQUEST = (400, 'Bad request')
    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'

History

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.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

choicesenum-0.1.0-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for choicesenum-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a56c3018c6c5c9784f303e1e496794db4c9747fb4ef14a895d98db8768ea89d6
MD5 8ddf72cd6258e6a2700f9b02a9b875bf
BLAKE2b-256 b9190ee70df9404f2d00d4339ab0aa2e35ad4d632a1cc0e5ad6b738f6d807159

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for choicesenum-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1c77f866e608ddce776ddc634b7e8030b3c16d9efc84f26b9045e7e31a287b2d
MD5 dd810feebc065ed419db4e3b6d0a78bb
BLAKE2b-256 08b638c5d2d9221407bdfbd655de8cb41538ee84333dbe64979d0844b412849a

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