Skip to main content

Custom Enum classes for Python 3.4

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

travis coveralls Latest Version Downloads Supported Python versions MIT License

What

Custom Enum classes for the Python 3.4 enum module.

Install

$ pip install enum34-custom

Usage

MultiValueEnum

Enum subclass where a member can be any iterable (even a generator, except str). You can reference a member by any of its element in the associated iterable. It might be usable for e.g. Equivalence Class Partitioning (ECP/EC testing).

from enum34_custom import MultiValueEnum

class Suit(MultiValueEnum):
    CLUBS =    '♣', 'c', 'C'
    DIAMONDS = '♦', 'd', 'D'
    HEARTS =   '♥', 'h', 'H'
    SPADES =   '♠', 's', 'S'

>>> print(Suit.CLUBS)
Suit.CLUBS

>>> Suit.CLUBS
<Suit.CLUBS: ('♣', 'c', 'C')>

>>> Suit('c')
<Suit.CLUBS: ('♣', 'c', 'C')>

>>> Suit('c') is Suit('C') is Suit('♣') is Suit.CLUBS
True

>>> import pickle
>>> pickle.loads(pickle.dumps(Suit('c'))) is Suit('♣')
True

>>> Suit('L')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/walkman/Projects/enum34-custom/enum34_custom.py", line 19, in __call__
    return super().__call__(suit)
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/enum.py", line 222, in __call__
    return cls.__new__(cls, value)
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/enum.py", line 457, in __new__
    raise ValueError("%s is not a valid %s" % (value, cls.__name__))
ValueError: L is not a valid Suit

>>> list(Suit)
[<Suit.CLUBS: ('♣', 'c', 'C')>,
 <Suit.DIAMONDS: ('♦', 'd', 'D')>,
 <Suit.HEARTS: ('♥', 'h', 'H')>,
 <Suit.SPADES: ('♠', 's', 'S')>]
  • Generators will immediately be exhausted at class creation time!

  • To conform to the standard library behavior, overlapping iterables are considered aliases, and works the same way as in stdlib (lookup will match the first declared element):

    >>> class MyOverLappingMVE(MultiValueEnum):
    ...     A = (0, 1, 2, 3, 4)
    ...     B = (4, 5, 6, 7, 8)
    >>> MyOverLappingMVE(4)
    <MyOverLappingMVE.A: (0, 1, 2, 3, 4)>

    If you want to make sure, no overlapping elements are present between members, you can use the no_overlap decorator:

    >>> from enum34_custom import MultiValueEnum, no_overlap
    
    >>> @no_overlap
    ... class NoOverLappingEnum(MultiValueEnum):
    ...     A = (1, 2, 3)
    ...     B = (3, 4, 5)
    ...
    /Users/walkman/Projects/enum34-custom/enum34_custom.py in no_overlap(multienum)
         55                                   (alias, name, intersection) in duplicates])
         56         raise ValueError('common element found in {!r}: {}'
    ---> 57                          .format(multienum, alias_details))
         58     return multienum
         59
    
    ValueError: common element found in <enum 'NoOverLappingEnum'>: B & A -> {3}
  • Beware with storing lots of data, every value will stored twice (MultiValueEnum stores values internally in a set for faster lookups)

  • If you declare a dict as a value, keys will be looked up (as expected)

CaseInsensitiveMultiValueEnum

This works the same way as MultiValueEnum except if a member’s value contains a str, those will be compared in a case-insensitive member.

Consider the following example:

class SimpleMultiValueEnum(MultiValueEnum):
    one = 1, 'one'
    two = 2, 'two'

>>> SimpleMultiValueEnum('One')
/usr/local/Cellar/python3/3.4.1_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/enum.py in __new__(cls, value)
    455                 if member.value == value:
    456                     return member
--> 457         raise ValueError("%s is not a valid %s" % (value, cls.__name__))
    458
    459     def __repr__(self):

ValueError: One is not a valid SimpleMultiValueEnum

While:

class CaseInsensitiveMVE(CaseInsensitiveMultiValueEnum):
    one = 1, 'one'
    two = 2, 'two'

>>> CaseInsensitiveMVE('One')
<CaseInsensitiveMVE.one: (1, 'one')>

StrEnum

Members of this enum are also instances of str and directly comparable to strings. str type is forced at declaration. Works the same way as described in Python Enum documentation, except for checking type.

CaseInsensitiveStrEnum

Same as StrEnum, but members stored as uppercase, and comparing to them is case insensitive also:

from enum34_custom import CaseInsensitiveStrEnum
class MyCaseInsensitiveStrEnum(CaseInsensitiveStrEnum):
    one = 'a'
    two = 'b'

>>> MyCaseInsensitiveStrEnum('a') == 'A'
True
>>> MyCaseInsensitiveStrEnum.one == 'a'
True

Testing

$ python setup.py test

Or install package in development mode and test with py.test:

$ pip install -e .
$ py.test

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

enum34-custom-0.6.5.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

enum34_custom-0.6.5-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file enum34-custom-0.6.5.tar.gz.

File metadata

  • Download URL: enum34-custom-0.6.5.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for enum34-custom-0.6.5.tar.gz
Algorithm Hash digest
SHA256 6ecb401bb19ef74431b7319b4e9e76327ab23837af1f843530fefd6051686e04
MD5 f0b8887ad6c93667587596b8b9379d36
BLAKE2b-256 31b05dfe0395d3624cdd9492c168a3ab42a4eddea3da17cb7fc59eb1ea8fb251

See more details on using hashes here.

File details

Details for the file enum34_custom-0.6.5-py3-none-any.whl.

File metadata

File hashes

Hashes for enum34_custom-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 93ff632f5213914549591dabf14fbec884fc639049dd9049fab5866ec76b4ca4
MD5 196a0cc3c57a6777d54e3c225d21f692
BLAKE2b-256 a49f222637397c638dd00fc29bd73a1f6adc2c8dd5037518fa0cb3f7afa0746d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page