Library Reference

larch.reactive

larch.reactive.atomic()[source]

Returns an atomic decorator context

larch.reactive.untouched()[source]

Returns an untouched decorator context

larch.reactive.touched()[source]

Returns a touched decorator context

larch.reactive.silent()[source]

Returns a slient decorator context

larch.reactive.reactive(cls)[source]

A class decorator making a class reactive.

larch.reactive.rule(level=0)[source]

A decorator converting a method to a rule.

Parameters:

level (int) – A “hard” priority level. Rules with smaller levels will allways be called before rules of greater levels.

larch.reactive.reactive(cls)[source]

A class decorator making a class reactive.

larch.reactive.cells_of(reactive)[source]

Returns all cell containers of an reactive object.

Parameters:

reactive – A reactive object.

Returns:

All reactive cell containers of reactive.

Return type:

list

larch.reactive.old(obj)[source]

Returns an old-agent of obj. This function is helpfull within a rule, to access the old cell values before the current atomic operation.

Parameters:

obj (Reactive) – A reactive object.

Returns:

An agent object offering the same attributes as obj, with the old cell values.

larch.reactive.cell(obj, value=None)[source]

Returns a cell-agent of obj. This function can be used to manipulate the cell containers of obj.

Parameters:

obj (Reactive) – A reactive object.

Returns:

An agent object offering the same attributes as obj, with the cell containers as attribute values.

larch.reactive.call_outside(func, *args, **kwargs)[source]

Postpones the execution of func until the current atomic operation has finished. If called outside an atomic operation func is executed immediate.

Parameters:
  • func (callable) – The callable to execute.

  • *args – Arguments to pass to func

  • **kwargs – Keyword arguments to pass to func

Returns:

the given func

Return type:

callable

class larch.reactive.Cell(default=None)[source]

A descriptor to assign cell containers to objects.

Parameters:

default_val – The default value of the cell.

class larch.reactive.TypeCell(default_val, converter=None)[source]

A cell the coerces an input cell to the type of its default value.

Parameters:
  • default_val – The default value of the cell.

  • converter (callable) – If not None, the converter will be used to to coerce any value to the right type. Otherwise type(default_val) will be used as converter.

class larch.reactive.MakeCell(factory, *args, **kwargs)[source]

A cell that creates the default value with a factory during construction of the reactive object.

Parameters:
  • factory (callable) – A factory that uses args and kwargs to create the default object.

  • *args – argument list for factory

  • **kwargs – keyword arguments for factory

Examples

>>> @reactive
class ListContainer(object):
    values = MakeCell(list, (1, 2, 3))
class larch.reactive.MakeTypeCell(factory, *args, **kwargs)[source]
class larch.reactive.SimpleReactive(*args, **kwargs)[source]

The base class for reactive objects that can hold cells and/or rules. Can also be used in multiple inheritance trees.

class larch.reactive.Pointer(state_or_obj=None)[source]

A placeholder to an object attributes and index chain (a generalization to attrgetter/itemgetter of module operator) to construct a pointer you call

>>> pointer = Pointer(obj).a[0]

to get a pointer’s value you call

>>> value = pointer()  # same as obj.a[0]

to set a pointer’s value you call

>>> pointer(value)  # same as obj.a[0] = value
class larch.reactive.PointerExpression(root=<weakref at 0x7c0a50c5a2a0; to 'MetaNothing' at 0x2385a6e0 (NOTHING)>, path=(), accessors=())[source]

A pointer expression

classmethod call(func, *args, **kwargs)[source]

calls func with args

larch.reactive.merge_pointers(*pointers)[source]

Merges several pointers together.

Parameters:

pointers – multiple pointers to merge

Returns:

a chained pointer object.

Return type:

Pointer

Example

>>> p1 = Pointer(obj1).a.b
>>> p2 = Pointer(obj2).c.d
>>> p3 = merge_pointers(p1, p2)
>>> p3 == Pointer(obj1).a.b.c.d
class larch.reactive.pcore.ReactiveContext[source]

A ReactiveContext is the core driver for reactive behaviour. It manages the subject - observer relation between cells and rules, and cares for the execution order of rules.

Only one ReativeContext exists in a program that can be accessed by:

>>> larch.reactive.rcontext
property inside_rule

True if the current frame is inside a rule.

Type:

bool

property inside_touch

True if the current frame is inside a touch context.

Type:

bool

property rounds

the count of atomic operations, since program start.

Type:

int

property transaction_level

count of recursive atomic operations.

Type:

int

class larch.reactive.pcore.Container(value)[source]

A cell container that stores a cell’s value.

get_observers()

Returns the subject’s observers

get_value()[source]

Returns the container’s value

set_value(value)[source]

Sets the containers value.

larch.reactive.rcollections