Skip to main content

Verifiably better, validated Enum

Project description

https://img.shields.io/pypi/v/venum.svg?style=flat-square https://img.shields.io/travis/ofek/venum.svg?branch=master&style=flat-square https://img.shields.io/codecov/c/github/ofek/venum.svg?style=flat-square https://img.shields.io/pypi/pyversions/venum.svg?style=flat-square https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square

venum provides an Enum that is actually just a namedtuple, but easier to create. This means an Enum doesn’t have to be defined before program execution (similar to the functional API) and members are truly immutable (can’t dynamically add new ones). Also, this saves a bit of memory over the stdlib’s Enum.

Usage

>>> from venum import Enum
>>>
>>> ContentTypes = Enum(
...     ('JSON', 'application/json; charset=utf-8'),
...     ('HTML', 'text/html; charset=utf-8'),
...     ('JS', 'text/javascript; charset=utf-8'),
...     ('XML', 'application/xml'),
...     ('TEXT', 'text/plain; charset=utf-8'),
...     ('JPEG', 'image/jpeg'),
...     ('PNG', 'image/png'),
...     ('YAML', 'application/x-yaml'),
...     name='ContentTypes'
... )
>>> ContentTypes
ContentTypes(JSON='application/json; charset=UTF-8', HTML='text/html; charset=utf-8', JS='text/javascript; charset=utf-8', XML='application/xml', TEXT='text/plain; charset=utf-8', JPEG='image/jpeg', PNG='image/png', YAML='application/x-yaml')

Attribute lookup

No need for .value.

>>> from venum import Enum
>>>
>>> sample = Enum(('BLUE', 1), ('RED', 2))
>>> sample
Enum(BLUE=1, RED=2)
>>> sample.BLUE
1

Comparison by value

>>> from venum import Enum
>>>
>>> sample = Enum(('SPADES', 1))
>>> sample.SPADES == 1
True

Memory-efficiency

This example was run on a 64-bit machine.

Note that stdlib’s Enum by itself uses 1056 bytes with each member requiring 56 bytes, whereas namedtuple Enum uses 888 bytes with each member requiring only 16 bytes.

>>> from sys import getsizeof
>>> from enum import Enum as StdEnum
>>> from venum import Enum
>>>
>>> class SomeEnum(StdEnum):
...     BLUE = 3
>>>
>>> getsizeof(SomeEnum.__class__)
1056
>>> getsizeof(Enum(('BLUE', 3)).__class__)
888

Installation

venum is distributed on PyPI as a universal wheel and is available on Linux/macOS and Windows and supports Python 2.7/3.3+ and PyPy.

$ pip install venum

Final words

That’s really all there is to it, but if you’re keen on seeing more words that begin with the letter V, here’s V’s monologue from V for Vendetta.

“Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a by-gone vexation, stands vivified and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition.

The only verdict is vengeance; a vendetta, held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous.

Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it’s my very good honor to meet you and you may call me V.”

—V

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

venum-1.1.0-py2.py3-none-any.whl (6.7 kB view hashes)

Uploaded Python 2 Python 3

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