Skip to main content

Data structure for multi-dimensional enums

Project description

MatrixEnum Logo

MatrixEnum

Build Status PyPI - Python Version

MatrixEnum is a package that provides convenient extensions to python's builtin enums to allow for simple construction of enum multi-dimensional members.

from matrix_enum import MatrixEnum, Member

class MyEnum(MatrixEnum):
    ONE = Member(digit=1, title='one', roman='I')
    TWO = Member(digit=2, title='two', roman='II')

# Keeps classic enum functionality
>>> MyEnum.ONE is MyEnum.TWO
False
>>> MyEnum.ONE is MyEnum.ONE
True

# Automatically adds attributes to all enum members
>>> MyEnum.ONE.digit
1
>>> MyEnum.ONE.title
'one'

# `Member` attributes are reversible
>>> MyEnum.ONE is MyEnum(1)
True
>>> MyEnum.TWO is MyEnum('II')
True
>>> 1 in MyEnum
True
>>> 'two' in MyEnum
True

Installation

You can either install the package with pip:

$ pip install matrix_enum

or clone the repo and install:

$ git clone https://github.com/klaviyo/matrix_enum.git
$ pip install -e matrix_enum/

Local Development

For information on local development, testing, and contributing, see the Contribution guidelines for this project.


API

MatrixEnum and Members

MatrixEnum extends the basic python Enum with the following restrictions:

  • All members must be of type Member.
  • All Members must have the same attributes.
  • All values of Member attributes must be unique across ALL attributes.
  • Member attributes cannot use any reserved __dunder__ attributes or the names name or value.

Valid

The following is a valid MatrixEnum:

from matrix_enum import MatrixEnum, Member

class MyEnum(MatrixEnum):
    ONE = Member(digit=1, title='one', roman='I')
    TWO = Member(digit=2, title='two', roman='II')

Invalid Enums

All of the following are INVALID MatrixEnums and will raise a ValueError:

# INVALID: The members have to be of class Member
class NonMembers(MatrixEnum):
    ONE = 1
    TWO = 2

# INVALID: Members have the same value for 'digit'
class DuplicateValue(MatrixEnum):
    ONE = Member(digit=1)
    OTHER_ONE = Member(digit=1)

# INVALID: Members can't have the same value across different attributes
class DuplicateValue2(MatrixEnum):
    FOO = Member(digit=1, other_digit=2)
    BAR = Member(digit=3, other_digit=1)

# INVALID: Members have different attributes
class UnevenAttrs(MatrixEnum):
    ONE = Member(digit=1, title='one')
    TWO = Member(digit=2, roman='II')

Adding extras

You can add duplicated values to Members using the extra method.

These extras will be available as attributes to members but cannot be used to lookup enum members by value.

class AnimalEnum(MatrixEnum):
    CAT = Member(title='cat').extra(num_paws=4)
    DOG = Member(title='dog').extra(num_paws=4)
    FISH = Member(title='fish').extra(num_paws=0)

>>> AnimalEnum.CAT.num_paws
4
>>> AnimalEnum(4)
ValueError: 4 is not a valid AnimalEnum

Links


This package is owned and maintained by Klaviyo. Check out our eng blog.

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

matrix_enum-1.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

matrix_enum-1.1.0-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file matrix_enum-1.1.0.tar.gz.

File metadata

  • Download URL: matrix_enum-1.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for matrix_enum-1.1.0.tar.gz
Algorithm Hash digest
SHA256 59f8d56237451721f80deecd4c979b3c4dbca9ba5c48efa40c21f7b17289cdc1
MD5 70b6513488eb957f559688897ccbfea3
BLAKE2b-256 0aecc7cbd266d78415c103f428649909e4b00e730b3217a62fdc0c6b47d3257a

See more details on using hashes here.

File details

Details for the file matrix_enum-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: matrix_enum-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for matrix_enum-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 752aa6691bf360e6bb04fdbace00d800d9f50594c68c5ce83277307ce493fbf6
MD5 ef54a737392fa1e58b0e619d296c4de7
BLAKE2b-256 023ebc95ed37d368211be82343d239b955421ff5f46657c0950ea2a5d4d8ce6e

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