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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bivium-0.0.3.tar.gz
.
File metadata
- Download URL: bivium-0.0.3.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6b113c30c54ad0e18e863388e94baae41ea032cdcd4325fc63255cce07a9a02 |
|
MD5 | 56227d1af6a8eb2f24ddaca340a6dc58 |
|
BLAKE2b-256 | 4a165461398eb241dd710d25d532b048b1d34ea0de8670a88dcaf736f1a82215 |
File details
Details for the file bivium-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: bivium-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1eff79c6e0a4700cb8db387b30f9fb4846841bb8f49ef3fe77211152f967ec1 |
|
MD5 | e0e5cc9eeb2a171c6e218af17ac756a7 |
|
BLAKE2b-256 | d38060d401129ef2e9c54fd2938d58b07b9080dfd3b3c9e6491c1e717e744f4f |