Skip to main content

Looking for the value associated with the most specific super type in the dictionary

Project description

inheritance_dict

PyPI - Version Coverage

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

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

inheritance_dict-0.4.0.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inheritance_dict-0.4.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file inheritance_dict-0.4.0.0.tar.gz.

File metadata

  • Download URL: inheritance_dict-0.4.0.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for inheritance_dict-0.4.0.0.tar.gz
Algorithm Hash digest
SHA256 54d8a27819eac7d3a0d2c292ed4a542402d77b38a39c754dbd972ac5a6506a63
MD5 d62e5fc53e75f3a8ee2d951022bdff70
BLAKE2b-256 1ca75c34098d1116a5f40c55abea16a7c36f777c16d0cb5d916f9cbff448c5d6

See more details on using hashes here.

File details

Details for the file inheritance_dict-0.4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for inheritance_dict-0.4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecf3702b3105e808807baef6bd47a0351a9ffce091d77f784681edb521f444a5
MD5 d780fe6f7203e4ce6073a544c2b5052f
BLAKE2b-256 715e9f950a68c9d535857579e970cc8965380b3288fa28141f2f4c42500c6d58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page