pyiron_contrib.protocol.utils.event module

class pyiron_contrib.protocol.utils.event.Event[source]

Bases: Synchronization

A which implements a signal slot system. It executes event handlers synchronized. An event handler may be EventHandler object, function or a lambda. Something callable at least.

If you pass a callable as event handler it must have a __name__ attribute. The handlers name will be eventHandler.name if it is a EventHandler object or a functions.__name__ attribute.

Lambdas do not get an identifier. So you cannot remove them except by clear_handlers(), but this resets all handlers.

suppressed

A flag that indicates if the event object should block the fire calls. If set to True signals will be blocked.

Type

bool

add_event_handler(handler)[source]

Adds a event handler to event. Each handler is identified by a string. The handlers identifier will be eventHandler.name if it is a EventHandler object or a functions.__name__ attribute. Lambdas do not get an identifier. To fire this handler explicitly use:

event.fire_handler(identifier)

You can shortcut this function by using:

event += lambda args: do_something(arg)
event += EventHandler('my_handler_name', my_awesome_handler_func)
event += my_object.my_awesome_handler_func
Parameters

handler (EventHandler, function or lambda) – the callable handler object

Raises
  • TypeError – If handler is no instance of EventHandler a function or a lambda

  • KeyError – If handlers identifier is already registered

clear_handlers()[source]

Removes all handlers, even registered lambda s

fire(*args, **kwargs)[source]

Causes all registered handlers to execute

Note

The arguments specified in *args and **kwargs must be consistent with the handlers signatures

Parameters
  • *args – Arguments passed to the handler functions

  • **kwargs – Keyword arguments passed to the handler functions

fire_handler(handler_name, *args, **kwargs)[source]

Fires only a specific registered hander. Only handlers with a name can be fired explicitly.

Theses handlers may be EventHandler objects or named functions

Parameters
  • handler_name (str) – The identifier of the handler If the underlying event handler is of type EventHandler the name is event_handler_obj.name In case of a function the identifier is my_handler_func.__name__

  • *args – Arguments passed to the handler function

  • **kwargs – Keyword arguments passed to the handler function

property handler_count

Counts how many handlers are registered

Returns

The total number of handlers

Return type

int

property handler_keys

` Creates a list with the names of all handlers

Returns

a list with all handler names

Return type

list of str

Type

`Property

has_handler(handler)[source]

Method to check if a certain event handler is already registered at the event

Parameters

handler (function, str or EventHandler) – The handler or its name to check

Returns

True if handler is available else False

Return type

bool

property named_handlers

` All handlers which are registered with a name

Returns

a list of all named handlers (handler with an identifier)

Return type

list

Type

`Property

remove_event_handler(handler)[source]

Removes the given event handler from the event

You can also shortcut this function by using:

event -= 'my_handlers_identifier_string'
event -= my_event_handler_object
event -= my_object.my_awesome_handler_func

Note

You cannot remove lambda expressions explicitly.

Parameters

handler (EventHandler, function or lambda) – the callable handler object

Raises
  • TypeError – If handler is no instance of EventHandler a function or a lambda

  • KeyError – If handlers name is not registered

set_event_handler(handler)[source]

Reassigns a event handler

Parameters

handler (EventHandler, function or lambda) – the callable handler object

Raises
  • TypeError – If handler is no instance of EventHandler a function or a lambda

  • KeyError – If handlers name is not registered

property unnamed_handlers

` List of all lambdas

Returns

a list of all registered lambdas

Return type

list of function

Type

`Property

class pyiron_contrib.protocol.utils.event.EventHandler(name, func)[source]

Bases: object

Utility class to identify a callback classs with a name.

name

The name of the event handler

Type

str

func

The function which will be executed

Type

function

class pyiron_contrib.protocol.utils.event.Synchronization[source]

Bases: object

Helper class which creates a mutex.

pyiron_contrib.protocol.utils.event.synchronized(name)[source]

Function that creates a lock object and stores in the callers __dict__. Wrappes method for synchronized execution :param name: name of the callable to wrap

Returns

the wrapped function

Return type

function