lenskit.lazy#

Types and functions for lazy values. These are used mostly for pipeline inputs.

Classes#

Lazy

Type for lazily-computed values.

Functions#

lazy_value(value)

Create a lazy wrapper for an already-computed value.

lazy_thunk(thunk)

Create a lazy value that calls the provided function to get the value as

Module Contents#

class lenskit.lazy.Lazy[T]#

Bases: Protocol

Type for lazily-computed values.

This is frequently used in pipeline components. If a pipeline component may or may not need one of its inputs, declare the type with this to only run it as needed:

def my_component(input: str, backup: Lazy[str]) -> str:
    if input == 'invalid':
        return backup.get()
    else:
        return input
Stability:
Caller (see Stability Levels).
get()#

Get the value behind this lazy instance.

Note

When used as a pipeline input this method invokes upstream components if they have not yet been run. Therefore, it may fail if one of the required components fails or pipeline data checks fail.

Raises:
  • Exception – Exceptions raised by sources of the lazy data (e.g. a thunk, or upstream components) may be raised when this method is called.

  • SkipComponent

    Internal exception raised to indicate that no value is available and the calling component should be skipped. Components generally do not need to handle this directly, as it is used to signal the pipeline runner.

    Only raised when used as a pipeline input.

Return type:

T

lenskit.lazy.lazy_value[T](value)#

Create a lazy wrapper for an already-computed value.

Parameters:

value (T) – The value to wrap in a lazy object.

Returns:

A lazy wrapper for the already-computed value.

Return type:

Lazy[T]

lenskit.lazy.lazy_thunk[T](thunk)#

Create a lazy value that calls the provided function to get the value as needed.

Parameters:

thunk (Callable[[], T]) – The function to call to supply a value. Will only be called once.

Returns:

A Lazy that will call the provided function.

Return type:

Lazy[T]