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.

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.1.0.0.tar.gz (5.7 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.1.0.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: inheritance_dict-0.1.0.0.tar.gz
  • Upload date:
  • Size: 5.7 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.1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a8a05441826fc4077f0c8a73cadb25e6e497403335710ccf610eacec91732d7
MD5 e579d92bfb3e08ee6780affc6dda0951
BLAKE2b-256 857d9de177e02ac25c4cb55fdd0d23735e4cb89a33e97550b02f87e3ebdbdcb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for inheritance_dict-0.1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 613b18d601a5d2077244670d07dfb436f3d74a3eebec767033a591a015160f0b
MD5 be18b53680a0d62d6ee37cbb60127f12
BLAKE2b-256 4cc0945f9d5dce5ccc4cc373b0cb12622f4dd5ebb41555f5d7befcce1a41a488

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