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.
Metadata
Release files for bivium 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bivium-0.0.3.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bivium-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.9 kB
Release files / bivium-0.0.3.tar.gz
| Download URL | bivium-0.0.3.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d6b113c30c54ad0e18e863388e94baae41ea032cdcd4325fc63255cce07a9a02
|
|
BLAKE2b-256 checksum How to use checksums |
4a165461398eb241dd710d25d532b048b1d34ea0de8670a88dcaf736f1a82215
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / bivium-0.0.3-py3-none-any.whl
| Download URL | bivium-0.0.3-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f1eff79c6e0a4700cb8db387b30f9fb4846841bb8f49ef3fe77211152f967ec1
|
|
BLAKE2b-256 checksum How to use checksums |
d38060d401129ef2e9c54fd2938d58b07b9080dfd3b3c9e6491c1e717e744f4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|