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.11.0.tar.gz (21.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.11.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lazily-0.11.0.tar.gz
Algorithm Hash digest
SHA256 12857e9abc0b135f1ed42ff2316af34d4df3da41f61a0bbdcefb05fc13c2580c
MD5 6b63fad451950b674457393148d6aff7
BLAKE2b-256 87028434e6557fc46701a563dbaf19cdc47de996421ad5ef295004029189592a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lazily-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 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.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44ff56c9a893d200713a0f7db530a93c3689f4d37716696f8ae1ea073682ac59
MD5 831f65ea5b2b4ef4df6888e97cca8456
BLAKE2b-256 65e008c630f4458d5eaaec798f742b39dbdb3b893193956c48e82e8fb9ff86e8

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