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.2.0.0.tar.gz (6.3 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.2.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: inheritance_dict-0.2.0.0.tar.gz
  • Upload date:
  • Size: 6.3 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.2.0.0.tar.gz
Algorithm Hash digest
SHA256 05f2d6bb7ff24014a78dff46b845cf2bcabbf07ba545c817131083283197d29d
MD5 1874bee4250de09ae28725088a7a6bf6
BLAKE2b-256 47ccab50fce417c483ecd1af437ad115c84fd664d3efc6f23134396c8f0958ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for inheritance_dict-0.2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 612638fbc5a03d154d6566893732bb8ec79970e178d39ef85cd4732c1bd0c4f9
MD5 98786c84b826ff3a2ddd8983fd316c5f
BLAKE2b-256 0feb4127a9ae324138025cac93fb5de9ea868ec973df682a1ab2ee042eb5b1f2

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