Skip to content

vtf io binder

srccircumflex edited this page Apr 24, 2023 · 8 revisions

↑ vtf-io

binder

The objects of the module offer a handling of the input dependent execution of functions, over references which can be defined as types/classes or as instances/objects. The objects in the module iodata are worked out accordingly for this.

The main object Binder uses internally the Binding class as memory and handle for bound functions to a reference.

Binder[ Binding ] -> BindItem

Different modes can be specified for the binding to define how or at which position the function is to be stored in the binding. In addition, each binding also contains a protected memory, this cannot be modified by BindItem's. The dynamics through BindItem's can be waived with the initialization of the Binder.

Module contents

Objects

class binder.Binder

A manager to bind functions to reference objects and types. Consists of two caches, one for bindings to instances and one for bindings to types.

If alter_bindings is True, Binder uses the alternative resources-saving version BindingT instead of Binding; in this mode NO BindItem's are returned when binding, these are for the unbinding and rebinding of a function.

If a Binding to a reference already exists, it is extended. When executing the functions found then, the following one always receives the applicable input and the return value of the previously executed function; for the function that is executed first, this is always the input and None.

The bindings are kept in tuples, so they are ordered and are queried one after the other when searching. By default, the search process in a cache type is stopped at the first match.

classcache: tuple[Binding] | tuple

instancecache: dict[int, tuple[Binding]]

__call__(__input) -> bool

The gate for the execution of the functions bound to __input. For optimization, it uses get_match with the lru_cache(20) to get the bound functions. Returns whether something was executed.

__getitem__(class_or_instance) -> Binding

Returns Binding to class_or_instance.

raises:
  • KeyError: If class_or_instance does not match.

__init__(find_all_matches=False, find_instance_match_only=False, find_class_match_first=False, alter_bindings=False)

If all matching bindings are to be found on an input, find_all_matches must be True. Otherwise the search process will be aborted on the occurrence of the first class and/or instance match. If find_instance_match_only is set to True, the search process for types is not executed when an instance hit occurs. find_class_match_first defines the order in which the hits are to be ordered and thus executed. As described above, the resource-saving type BindingT can be used instead of the dynamic Binding via alter_bindings.

bind(class_or_instance, func, mode="x", _index=0) -> BindItem | None

Bind the execution of func to the Binding / BindingT, in mode:
  • "a" (append):
    Append func to the execution order.
  • "i" (insert):
    Insert func into the _index location in the execution order.
  • "r" (replace):
    Place func in the _index location in the execution order.
  • "x" (exclusive)
    Assign func exclusive, except of the protected bindings.
  • "~a" (append to the protected bindings)
  • "~i" (insert into the protected bindings)
  • "~r" (replace a protected binding)

func gets the applicable object and the return value of a previously executed function in the binding.

Returns:
  • BindItem or
  • None in "~" mode or if the alternate Bindings are in use.

bindchain(class_or_instance, *funcs, mode="x", _index=0) -> BindChainItem | None

Bind the execution of the chain funcs to the Binding / BindingT, in mode:
  • "a" (append):
    Append the chain to the execution order.
  • "i" (insert):
    Insert the chain into the _index location in the execution order.
  • "r" (replace):
    Start at the _index location to insert the functions in the chain into the execution order.
  • "x" (exclusive)
    Assign the function chain exclusive, except of the protected bindings.
  • "~a" (append to the protected bindings)
  • "~i" (insert into the protected bindings)
  • "~r" (replace protected bindings)

Each func in funcs gets the applicable object and the return value of a previously executed function in the binding.

Returns:
  • BindChainItem or
  • None in "~" mode or if the alternate Bindings are in use.

get_binding(class_or_instance) -> Binding | None

Returns the Binding to class_or_instance or None.
@lru_cache(20)
get_match(item) -> tuple[list[Binding], list[Binding]] | tuple[list[Binding]] | None
Return the bindings for item or None. Positive result can be a one-tuple or double-tuple; the order of the double-tuple depends on the parameter find_class_match_first.

init_binder() -> None

Initializes the Binder.

send(__input) -> bool

The gate for the execution of the functions bound to __input. For optimization, it uses get_match with the lru_cache(20) to get the bound functions. Returns whether something was executed.
Handler

class binder.Binding

A manager for functions bound to class_or_instance. Consists of two memories for functions, one for dynamic operations (functions can be unbound and rebound via the BindItem (returned on binding), or assigned exclusively) and a protected one which is independent of the dynamic one and can only be extended or initiated. The functions of the protected memory are executed before the functions in the dynamic cache when called.

bindings: dict[int, Callable[ [Key | Mouse | Reply | Char | EscSegment, Any], Any]]

call_order: list[int]

pb: tuple[Callable[ [Key | Mouse | Reply | Char | EscSegment, Any], Any]] | tuple

reference: type | object

__call__(other, prev_rval, *, _comp=True) -> tuple[bool, Any]

Compare other with the reference item of the binding if _comp is True, then execute the bound functions if the comparison is positive.

Returns the comparison result and the function return value.

__eq__(other) -> bool

__init__(class_or_instance)

Create a binding to the type (Type[Key | Mouse | Reply | Char | EscSegment]) or object (Key | Mouse | Reply | Char | EscSegment).

__len__() -> int

Number of bound functions, excl. protected.

bind(__f, mode="x", _index=0) -> BindItem | None

Bind the execution of func to the Binding / BindingT, in mode:
  • "a" (append):
    Append func to the execution order.
  • "i" (insert):
    Insert func into the _index location in the execution order.
  • "r" (replace):
    Place func in the _index location in the execution order.
  • "x" (exclusive)
    Assign func exclusive, except of the protected bindings.
  • "~a" (append to the protected bindings)
  • "~i" (insert into the protected bindings)
  • "~r" (replace a protected binding)

func gets the applicable object and the return value of a previously executed function in the binding.

Returns:
  • BindItem or
  • None in "~" mode or if the alternate Bindings are in use.

init_binding() -> None

Initializes the Binding.

init_protected() -> None

Initialize the memory for protected bindings.

class binder.BindingT(Binding)

A more resource efficient version of Binding. Replaces the handling of bound functions with a dictionary and order-list with a tuple. At the cost of dynamic; bindings generally do not return BindItem.

bindings: tuple[Callable[ [Key | Mouse | Reply | Char | EscSegment, Any], Any]] | tuple

call_order = None

__call__(other, prev_rval, *, _comp=True) -> tuple[bool, Any]

Compare other with the reference item of the binding if _comp is True, then execute the bound functions if the comparison is positive.

Returns the comparison result and the function return value.

__init__(class_or_instance)

Create a binding to the type (Type[Key | Mouse | Reply | Char | EscSegment]) or object (Key | Mouse | Reply | Char | EscSegment).

bind(__f, mode="x", _index=0) -> None

Bind the execution of func to the Binding / BindingT, in mode:
  • "a" (append):
    Append func to the execution order.
  • "i" (insert):
    Insert func into the _index location in the execution order.
  • "r" (replace):
    Place func in the _index location in the execution order.
  • "x" (exclusive)
    Assign func exclusive, except of the protected bindings.
  • "~a" (append to the protected bindings)
  • "~i" (insert into the protected bindings)
  • "~r" (replace a protected binding)

func gets the applicable object and the return value of a previously executed function in the binding.

Returns:
  • BindItem or
  • None in "~" mode or if the alternate Bindings are in use.

init_binding() -> None

Initializes the Binding.
Items

class binder.BindChainItem(tuple[BindItem])

A container and manager for multiple BindItem's. Returned by Binder.bindchain().

purge_binding() -> None

Initialize the binding. (ALL bound functions are removed from the binding, except the protected).

range() -> tuple[int, int]

Returns the position range of the chain in the execution order.

rebind(mode="x", _index=0) -> None

Rebind the execution of the functions in the chain to the Binding; in mode:
  • "a" (append):
    Append the chain to the execution order.
  • "i" (insert):
    Insert the chain into the _index location in the execution order.
  • "r" (replace):
    Start at the _index location to insert the functions in the chain into the execution order.
  • "x" (exclusive)
    Assign the function chain exclusive, except of the protected bindings.

unbind() -> None

Unbind all functions in the chain.

class binder.BindItem

An item for handling a bound function. This can be used to unbind or rebind the function.

__binding__: Binding

func: Callable[ [Key | Mouse | Reply | Char | EscSegment, Any], Any]

id: int

__init__(__binding__, __f, _id)

index() -> int

Returns the position in the execution order.

purge_binding() -> None

Initialize the binding. (ALL bound functions are removed from the binding, except the protected).

rebind(mode="x", _index=0) -> None

Rebind the execution of __f to the Binding; in mode:
  • "a" (append):
    Append func to the execution order.
  • "i" (insert):
    Insert func into the _index location in the execution order.
  • "r" (replace):
    Place func in the _index location in the execution order.
  • "x" (exclusive)
    Assign func exclusive, except of the protected bindings.

unbind() -> None

Unbind the function.

Date: 15 Nov 2022
Version: 0.1
Author: Adrian Hoefflin [srccircumflex]
Doc-Generator: "pyiStructure-RSTGenerator" <prototype>

Clone this wiki locally