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.2.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.2-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lazily-0.10.2.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.2.tar.gz
Algorithm Hash digest
SHA256 cad9cf97f86062a92d6c51aa8fc2e9161b504aa21539d074138cfbd65c80ec1f
MD5 78a761b86c9c87d49d4dd689805acabc
BLAKE2b-256 f07b890a2393f4153df145880f0ab98a2bd17715a7b2558966bfd4ab12726ff9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lazily-0.10.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2044243bdd258c2eab53284abca7160da3973803bba3ac0eb30d4274e9180c87
MD5 23a23bfe09d58a40d1bf71362483a143
BLAKE2b-256 b3fb33f5257d13b66def57d1da0318e1fde835257e766694092441a06b823b18

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