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 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.6.0.tar.gz
(16.8 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.6.0-py3-none-any.whl
(12.9 kB
view details)
File details
Details for the file lazily-0.6.0.tar.gz.
File metadata
- Download URL: lazily-0.6.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23dd4debb65f5e3c2a35365b26c0d39760c09b4196b5006f81f0bef272bbdf4a
|
|
| MD5 |
04fd350af963c76807a1a7f998698ec7
|
|
| BLAKE2b-256 |
a92ebdb0a598cb80347b785256594188d113b1493e811f7a1bf2fc3f71b8aa29
|
File details
Details for the file lazily-0.6.0-py3-none-any.whl.
File metadata
- Download URL: lazily-0.6.0-py3-none-any.whl
- Upload date:
- Size: 12.9 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 |
b79bb44fc7cf26b615d767f2f65a1b0c13ff8a19a9014a09c4d782632a9f8d99
|
|
| MD5 |
bc2bc8874d90f01a209e5d39db483a5c
|
|
| BLAKE2b-256 |
12dac2c2d4db8ced70c39af5d19969f24323cb7b74beaae5d568c5f7d777ded7
|