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.3.0.0.tar.gz (7.4 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.3.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: inheritance_dict-0.3.0.0.tar.gz
  • Upload date:
  • Size: 7.4 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.3.0.0.tar.gz
Algorithm Hash digest
SHA256 330439d9bdf4ecf52bcc10d985ade8323030dbbfbf66013283582312432b74b6
MD5 2a848d6ae36aff3ea98624d59c96764c
BLAKE2b-256 9472b886ad6fa6b10419b50adb4c750807ca4ff100086dc9013c254e79a15754

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for inheritance_dict-0.3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ad290bb295cf438a7412fb6e783f039c878ed162db74c5b0c2b5aa89a5ee1ac
MD5 78de3f5e3dddda87ca2acb14a2c85e07
BLAKE2b-256 49ceb04f52301c700cb19695744fb94b1fe342c7147d03fc15b0734c6147f20e

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