Skip to main content

Extremely simple to use callable memoization decorator library.

Project description

belljar 🫙

Mid-execution memoization for dynamic state.

Standard caching fails when your function relies on hidden or changing state (like a database cursor or an open file). belljar solves this by letting you build the cache key while the function runs.

uv add belljar

The Difference

Concept functools.lru_cache belljar
When is it checked? Before the function runs. Mid-execution, exactly when you tell it to.
What defines identity? Static function arguments. Function source code + runtime state.
Handling Mutable State Fails. Returns stale/wrong data. Succeeds. Hashes current state dynamically.

Usage

You need exactly three primitives: @store to set the boundary, include() to build identity, and check() to short-circuit.

import belljar
import io

# A simulated file. The object stays the same, but its internal cursor moves.
log_file = io.StringIO("chunk1 chunk2 chunk3")

@belljar.store()  # Automatically seeds identity using this function's source code
def process_chunk(file_handle):
    # 1. Add the file's exact runtime cursor position to the identity hash
    belljar.include(file_handle.tell())

    # 2. If we've processed from this exact position before, STOP.
    # The function aborts right here and returns the saved result from disk.
    belljar.check()

    # 3. Otherwise, do the heavy processing
    print("Doing heavy work...")
    return file_handle.read(6)

# Call 1: Reads "chunk1", saves to disk. (Takes time)
process_chunk(log_file)

# Call 2: Reads "chunk2", saves to disk. (Takes time)
process_chunk(log_file)

# If we reset the file and run it again:
log_file.seek(0)

# Call 3: belljar.check() detects the cursor is at 0, aborts the function,
# and instantly returns the cached "chunk1".
process_chunk(log_file)

Core Mechanics

  • Auto-Invalidation: belljar hashes your actual source code. If you edit the function, the cache invalidates automatically.
  • Disk Persistent: Caches are saved to a .jar/ directory by default, surviving script restarts. Pass a path to change it: @store(Path("/tmp/cache")).
  • Deep Serialization: Powered by dill (not pickle), meaning it safely handles lambdas, nested classes, and complex closures.
  • Async Ready: @store() works on async def functions too, and concurrent calls keep their identities isolated.

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

belljar-4.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

belljar-4.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file belljar-4.1.0.tar.gz.

File metadata

  • Download URL: belljar-4.1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for belljar-4.1.0.tar.gz
Algorithm Hash digest
SHA256 683cc92217ec0f6b8ef74a5f893e590b92e4e5d44eddb4fb7d99faed33468e51
MD5 6e8f6cccac63434d4d16eccaad1b05df
BLAKE2b-256 a98a0ea27609dab88bca47375dfab86a02724f775d39ab211137b58d38d3d198

See more details on using hashes here.

File details

Details for the file belljar-4.1.0-py3-none-any.whl.

File metadata

  • Download URL: belljar-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for belljar-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50f0694a13940e7139a15f042a7c9ccb11ab54f1b94b327415a7bd71fd57fc10
MD5 8c6a63be418f0f8f97e5955c15df0432
BLAKE2b-256 106331db79156058529e2699c37aecdc04ac718de64ac4a0d521702d5bc9a12b

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