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.10.0.tar.gz (18.3 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.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lazily-0.10.0.tar.gz
  • Upload date:
  • Size: 18.3 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.0.tar.gz
Algorithm Hash digest
SHA256 96074408fe32b58cc543e7495eff01baf6078b35036f120df17e93bb14964a33
MD5 a65dade40423409d5ceba504e08b67c0
BLAKE2b-256 2cd08d68c2e3684436c988cedd0c3b9508245f4827df7ec2420e187f38b02f11

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lazily-0.10.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2437de3d9a8a9a3e5ff63aaa145936b798a865887f4a84c01c0d9ccc0b093f7
MD5 6436e5947811ad954d6b1d252b78045a
BLAKE2b-256 ae03577b36b2eea31d58d6af887d29a9c93eb4066e9794b94d4a125672fb2059

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