Skip to main content

Two-way enum-like lookups

Project description

Bivium - Two-way Enum-like lookups

pip install bivium

Bivium is a enum-like immutable object you can inherit from to define two-way lookups. Create a class like this:

from bivium import Bivium

class Test(Bivium):
    A = 1
    B = 2
    C = 3

Then, you can do lookups on the data like this:

Test.A     # 1
Test["A"]  # 1
Test(1)    # A

Bivium does not recognize dunder or sunder methods (i.e. __abc__ or _abc_) or anything that looks like it, so this will not work:

class Test(Bivium):
    _A_ = 1    # nope
    __A__ = 1  # double nope

You can set the optional _default_key_ and _default_value_ attributes, which will be returned if no keys or values are found when using Test[key] or Test(value) respectively.

class Test(Bivium):
    A = 1
    B = 2
    C = 3

    _default_key_ = "foo"
    _default_value_ = "bar"

Test["X"]  # "foo"
Test("Y")  # "bar"

If they are not set, ValueErrors are raised for missing data. Attribute access (e.g. Test.A) will always raise an error for missing data.


There is also and alternative mutable version of Bivium, which behaves more like a dict. It implements all container type methods, and some of them also in the reverse direction.

from bivium import BiviumAlt
from bivium.alt import Bivium

This version should be instantiated:

test = Bivium({"A": 1, "B": 2}, C=3)

Notice that you can use both a mapping or kwargs to instantiate the Bivium. Note that a KeyError will be raised if you try to pass the same key in the mapping and kwargs.

Also, if any keys in the mapping or kwargs have the same value, the init will raise a ValueError. Similalry, ValueErrors will be raised later if you try to add a value under some key which already is the value for some other key.

Bivium alt still has _default_key_ and _default_value_, but you should probably use .get() and .rget() methods instead.

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

bivium-0.0.3.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

bivium-0.0.3-py3-none-any.whl (6.0 kB view hashes)

Uploaded 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