pyiron_contrib.protocol.utils.event module
- class pyiron_contrib.protocol.utils.event.Event[source]
Bases:
SynchronizationA which implements a signal slot system. It executes event handlers synchronized. An event handler may be
EventHandlerobject,functionor alambda. Somethingcallableat least.If you pass a
callableas event handler it must have a__name__attribute. The handlers name will beeventHandler.nameif it is aEventHandlerobject or afunctions.__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.nameif it is aEventHandlerobject or afunctions.__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,functionorlambda) – the callable handler object- Raises
TypeError – If handler is no instance of
EventHandlerafunctionor alambdaKeyError – If handlers identifier is already registered
- 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
EventHandlerobjects or namedfunctions- Parameters
handler_name (
str) – The identifier of the handler If the underlying event handler is of typeEventHandlerthe name isevent_handler_obj.nameIn case of afunctionthe identifier ismy_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
listofstr- Type
`Property
- has_handler(handler)[source]
Method to check if a certain event handler is already registered at the event
- Parameters
handler (
function,strorEventHandler) – 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
lambdaexpressions explicitly.- Parameters
handler (
EventHandler,functionorlambda) – the callable handler object- Raises
TypeError – If handler is no instance of
EventHandlerafunctionor alambdaKeyError – If handlers name is not registered
- set_event_handler(handler)[source]
Reassigns a event handler
- Parameters
handler (
EventHandler,functionorlambda) – the callable handler object- Raises
TypeError – If handler is no instance of
EventHandlerafunctionor alambdaKeyError – If handlers name is not registered
- class pyiron_contrib.protocol.utils.event.EventHandler(name, func)[source]
Bases:
objectUtility 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