Skip to main content

thunk-dict

Dictionaries that enable the lazy evaluation and memoization of callable entries. thunk-dict is meant to serve as a convenient wrapper the lazy evaluation of potentially computationally expensive calls.

Use case

Because Python eagerly evaluates expressions, it is not possible include calls to functions within a dictionary without evaluating those calls first. To mimic lazy evaluation, one may turn to tricks like:

dictionary = {
  "key1": computationally_expensive_function
}

# Call the function later on
result = dictionary["key1"]()

or alternatively, if you also wanted to pass in arguments later on,

dictionary = {
  "key1": lambda *args, **kwargs: computationally_expensive_function(*args, **kwargs)
}

# Call the function later on
result = dictionary["key1"]("blah", parameter=100)

However, this approach suffers from two issues:

  1. It complicates the typing of your dictionaries and its entries and creates boilerplate (as in the second example).
  2. Reaccess requires the computationally expensive functions to be called again (i.e. the call isn't memoized)

thunk-dict attempts to resolve both of these issues.

from thunk_dict import ThunkDict
dictionary = ThunkDict({
  "key1": computationally_expensive_function
})

# Get result from call via simple access
result = dictionary["key1"]

# Subsequent accesses use the memoized result.

For noncallable entries, thunk-dict works just like a regular dict. thunk-dict also features the same API as regular Python dictionaries, meaning you don't have to sacrifice anything by using its wrapper.

Features

  1. Convenient thunk-like behavior in dictionaries
  2. Works for any objects that register with dict
  3. Very low overhead

Installation

From PyPi

pip install thunk-dict

From source distribution

git clone https://github.com/kevalii/thunk-dict.git
cd https://github.com/kevalii/thunk-dict.git
pip install .

Metadata

Release files for thunk-dict 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thunk-dict 0.1.1
File Size Uploaded Provenance Actions
thunk_dict-0.1.1.tar.gz 2.8 kB Without provenance Details for thunk_dict-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for thunk-dict 0.1.1
File Interpreter ABI Platform Actions
thunk_dict-0.1.1-py3-none-any.whl Python 3 none any Details for thunk_dict-0.1.1-py3-none-any.whl

Total release size: 6.7 kB

Release files / thunk_dict-0.1.1.tar.gz

Download URL thunk_dict-0.1.1.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fb2c60db3adbdef50fa7d6df2ba0a6cf2418a39a441e68f74ba8b18ea0a0008a
BLAKE2b-256 checksum
How to use checksums
1a01127f0dd557d6ad9b71d9fa3ab0b1fbfa5d6ee8228839b0d7fb0b95495ed0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release files / thunk_dict-0.1.1-py3-none-any.whl

Download URL thunk_dict-0.1.1-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7bf022ae5b86cf9850753885afd064bf02729b26dfbcfe957b99ad2915bae3a
BLAKE2b-256 checksum
How to use checksums
def0c72bc948fe0619995266a6e91f909ea998ea0f8df6142443f5c1d847f3cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Security for thunk-dict 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page