Skip to main content

A library for lazy evaluation with context caching

Project description

lazily

A Python library for lazy evaluation with context caching.

Installation

pip install lazily

Example usage

from lazily import cell, slot

# Cells hold a value that can be updated.
name = cell()


# Slots are functions that depend on cells and other slots.
@slot
def greeting(ctx: dict) -> str:
    print("Calculating greeting...")
    return f"Hello, {name(ctx).value}!"


# A cell can also have a default value.
@cell
def response(ctx: dict) -> str:
    return "How are you?"


@slot
def greeting_and_response(ctx: dict) -> str:
    print("Calculating greeting_and_response...")
    return f"{greeting(ctx)} {response(ctx).value}"


ctx = {}

name(ctx).value = "World"

# First access: runs the function
print(greeting(ctx))
# Calculating greeting...
# 'Hello, World!'

# Second access: uses cache (no print)
print(greeting(ctx))
# 'Hello, World!'

# Dependencies also access cached values
print(greeting_and_response(ctx))
# Calculating greeting_and_response...
# 'Hello, World! How are you?'

# Dependencies also cached
print(greeting_and_response(ctx))
# 'Hello, World! How are you?'

# Update cell: invalidates cache
name(ctx).value = "Lazily"

# Access again: re-runs the function
print(greeting_and_response(ctx))
# Calculating greeting_and_response...
# Calculating greeting...
# 'Hello, Lazily! How are you?'

# Another access: uses cache
print(greeting_and_response(ctx))
# 'Hello, Lazily! How are you?'

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

lazily-0.8.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

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

lazily-0.8.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file lazily-0.8.0.tar.gz.

File metadata

  • Download URL: lazily-0.8.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for lazily-0.8.0.tar.gz
Algorithm Hash digest
SHA256 a1ef0893857e6bb8f3ec5e36c51d31dd3301b6e930f11cb49caa958c8744f742
MD5 28596b77af691e2570541dd0c25afd2e
BLAKE2b-256 8ba07c678d75ea66a5b68b2ec8a78bf4db2f445b41d4bfcdfc1c28e2fb1edbef

See more details on using hashes here.

File details

Details for the file lazily-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: lazily-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for lazily-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66b0c1e2bba1c010fa515aad0618b88ea644d2fd64255c81fc56dbb79f3aafe5
MD5 bda5ca6531dc5f43261e56b8ac9582c2
BLAKE2b-256 aead1dd17f0626ca6c90ee1616055a2ec5dd213bbd13c86e9a08523be2e47835

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