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


@cell
def name(ctx: dict) -> str:
    return "World"


@slot
def greeting(ctx: dict) -> str:
    print("Calculating greeting...")
    return f"Hello, {name(ctx).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 = {}

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

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

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

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

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

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

# Another access: uses cache
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.5.0.tar.gz (16.6 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.5.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lazily-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e6395e682473534b8345fd5dc062eac20ca42d2542894a078f1f9fab68de2e88
MD5 bb01efc7b9605a65b3c62cadf26f6c7f
BLAKE2b-256 ef8d2b26b2681d19230508b2bea700923291cf822a902d8e577aefb5f0904e7e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lazily-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad88d3a5ea5de7a697aca1b1305717b7cb6ff141b61d3984f1f0508b5f03edc1
MD5 ce3a576bed76ce60ec969d3b03b5aa1d
BLAKE2b-256 5e7a4ed7121f87db9215373bcb52f182333d533b01110f545c8ed9de707fc08e

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