Looking for the value associated with the most specific super type in the dictionary
Project description
inheritance_dict
inheritance_dict is a small package where one can map types to values. If one then performs a lookup, it walks down the Method Resolution Order (MRO) looking for a match, and thus returns the value associated with the most specific superclass of the type.
Example
Imagine the following inheritance dictionary:
from inheritance_dict import InheritanceDict
example = InheritanceDict({object: 1, int: 2})
now we can query it with:
example[object] # 1
example[int] # 2
example[bool] # 2
example[float] # 1
example[str] # 1
It thus for each type tries to find the value that is associated with the most specific key-value pair for that type.
The main application is making mappings between types. For example, in Django the mapping between model fields, and serializer fields, resource fields, etc. is often done through such pattern. We thus aim to encapsulate the logic.
The dictionary can also contain non-type items. For lookups with keys where the key is not a type, it will try to lookup the key, just like it normally does.
The package also provides a TypeConvertingInheritanceDict which will, if the key is not found, and the key is not a type itself, try a second time with the type of the object. So if we have:
from inheritance_dict import TypeConvertingInheritanceDict
example2 = TypeConvertingInheritanceDict({object: 1, int: 2})
we can query with:
example2['A'] # 1
example2[0+1j] # 1
example2[3] # 2
There are also Fallback variants of these like FallbackInheritanceDict and FallbackTypeConvertingInheritanceDict, which allow to query with a tuple of keys, like:
from inheritance_dict import FallbackInheritanceDict
example3 = FallbackInheritanceDict({object: 1, int: 2})
then one can query with:
example3[complex, int] # 1
example3[int, complex] # 2
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file inheritance_dict-0.5.0.0.tar.gz.
File metadata
- Download URL: inheritance_dict-0.5.0.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d0bb71a3d9b18dcaf76d05befe1fa9348e3badd2b1a46b6ee91279cebd5ced2
|
|
| MD5 |
9ed7f10757bed1323ffd3ca6f43bd9c6
|
|
| BLAKE2b-256 |
fb8c16cc1ede9cdb74c809e97f8e7240e4006acf2998a7a33d5467bf41c18c35
|
File details
Details for the file inheritance_dict-0.5.0.0-py3-none-any.whl.
File metadata
- Download URL: inheritance_dict-0.5.0.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63b95a8aa703b2d843f60eb8522a63bc00a2c35c6a3ab38ae3a2eaac050131fc
|
|
| MD5 |
5f9c092dc6cddef1323a024cab140c86
|
|
| BLAKE2b-256 |
634667ff9e2cac4331ad3833fdfe9595a79d9e0aef3a7bb983c10b6160e2d380
|