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.9.0.tar.gz (18.1 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.9.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lazily-0.9.0.tar.gz
Algorithm Hash digest
SHA256 d7c663710b3ed0d213d8375e7dc284e8837ad9a4d98b8473d0f2697c4b6ec43f
MD5 112e0b4437456a6b9eefdbb890a75b7e
BLAKE2b-256 1fea0d5ae6cb71cc79ca1876108bd2cebe1377eed44117c7db1169b270fbeb41

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lazily-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 13.2 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.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d22a107c06929615f6e72d888c205afaf99f412f743c2210c243b93e62ab9842
MD5 fe4a47bbf8b121d00872b1882005803b
BLAKE2b-256 145d635585db165593aa248b5aef4eae5bc6d780736642fc8c0c51a40d4e50e1

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