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
Release history Release notifications | RSS feed
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.7.0.tar.gz
(17.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
lazily-0.7.0-py3-none-any.whl
(13.5 kB
view details)
File details
Details for the file lazily-0.7.0.tar.gz.
File metadata
- Download URL: lazily-0.7.0.tar.gz
- Upload date:
- Size: 17.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
775900b595dfec61bf5419fe066cf3cfff7e1250dbea1fe2a321c67e6e45ca59
|
|
| MD5 |
b1d0f2b505ac70ecca7636ec74194b4a
|
|
| BLAKE2b-256 |
3e8cc1f4e593184caab9f971cc6066f0360c54a239ebce65f322df54680650d9
|
File details
Details for the file lazily-0.7.0-py3-none-any.whl.
File metadata
- Download URL: lazily-0.7.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9a307ab7169a7030e8cf356ed8d87b067fead2203a2aa9215b109e4a3345135
|
|
| MD5 |
d577b81614b541d206fb7b235719e676
|
|
| BLAKE2b-256 |
3bb424defb7a5dfd3bdc2eb6bb4cc5695b0a1b6c6b4228c6d5ad8bdd96c03110
|