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 CellSlot, cell, slot

# Cells hold a value that can be updated.
name = CellSlot[dict, dict, str]()


# 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 CellSlot 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.10.1.tar.gz (18.4 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.10.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lazily-0.10.1.tar.gz
Algorithm Hash digest
SHA256 2e04cd70f03f9a3ba966f8635c2ebc64b2a999e2d00ed75864e53e01390a1d7a
MD5 6f49fbbb31707edc73edd2e258957a77
BLAKE2b-256 7f1ef770d7ef6d2a208599f17db27b9fa78917c792bf91b9842d5b5515df28a0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for lazily-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 18ebf9b444e06308fed5fa6eb9fbe569e893ab6ad4d47488a25ed53bc7d01232
MD5 fb97aa5091e29d846eec0ac042d311f2
BLAKE2b-256 9d832b66caa229228a8ee580b33f09e54aa68e1bd6e630e3cb56e58cb5be3d3d

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