All Projects → ofek → venum

ofek / venum

Licence: MIT License
Verifiably better, validated Enum for Python

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to venum

Immutable Tuple
Immutable finite list objects with constant-time equality testing (===) and no memory leaks.
Stars: ✭ 29 (-6.45%)
Mutual labels:  immutable, functional
finger-tree
🌵 Finger tree data structure for JavaScript
Stars: ✭ 20 (-35.48%)
Mutual labels:  immutable, functional
Html
A Virtual DOM based templating-engine for PHP
Stars: ✭ 86 (+177.42%)
Mutual labels:  immutable, functional
Fpp
Functional PHP Preprocessor - Generate Immutable Data Types
Stars: ✭ 282 (+809.68%)
Mutual labels:  immutable, functional
babl
JSON templating on steroids
Stars: ✭ 29 (-6.45%)
Mutual labels:  immutable, functional
Phpfn
Functional PHP Toolstet: Centralized monorepository for all libraries
Stars: ✭ 19 (-38.71%)
Mutual labels:  immutable, functional
Typed
The TypeScript Standard Library
Stars: ✭ 124 (+300%)
Mutual labels:  immutable, functional
Pyrsistent
Persistent/Immutable/Functional data structures for Python
Stars: ✭ 1,621 (+5129.03%)
Mutual labels:  immutable, functional
js-data-structures
🌿 Data structures for JavaScript
Stars: ✭ 56 (+80.65%)
Mutual labels:  immutable, functional
grand central
State-management and action-dispatching for Ruby apps
Stars: ✭ 20 (-35.48%)
Mutual labels:  immutable, functional
Typed Immutable
Immutable and structurally typed data
Stars: ✭ 263 (+748.39%)
Mutual labels:  immutable, functional
treecko
A collection of functional and immutable helpers for working with tree data structures.
Stars: ✭ 31 (+0%)
Mutual labels:  immutable, functional
Switzerland
🇨🇭Switzerland takes a functional approach to Web Components by applying middleware to your components. Supports Redux, attribute mutations, CSS variables, React-esque setState/state, etc… out-of-the-box, along with Shadow DOM for style encapsulation and Custom Elements for interoperability.
Stars: ✭ 261 (+741.94%)
Mutual labels:  immutable, functional
Partial.lenses
Partial lenses is a comprehensive, high-performance optics library for JavaScript
Stars: ✭ 846 (+2629.03%)
Mutual labels:  immutable, functional
transmute
kind of like lodash but works with Immutable
Stars: ✭ 35 (+12.9%)
Mutual labels:  immutable, functional
peds
Type safe persistent/immutable data structures for Go
Stars: ✭ 57 (+83.87%)
Mutual labels:  immutable, functional
fastener
Functional Zipper for manipulating JSON
Stars: ✭ 54 (+74.19%)
Mutual labels:  immutable, functional
Swiftz-Validation
A data structure for validations. It implements the applicative functor interface
Stars: ✭ 15 (-51.61%)
Mutual labels:  functional
fefe
Validate, sanitize and transform values with proper TypeScript types and zero dependencies.
Stars: ✭ 34 (+9.68%)
Mutual labels:  functional
react-stateful-component
Functional stateful React components with sideEffect support
Stars: ✭ 19 (-38.71%)
Mutual labels:  functional

venum

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].