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 alambda
. Somethingcallable
at least.If you pass a
callable
as event handler it must have a__name__
attribute. The handlers name will beeventHandler.name
if it is aEventHandler
object 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.name
if it is aEventHandler
object 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
,function
orlambda
) – the callable handler object- Raises
TypeError – If handler is no instance of
EventHandler
afunction
or alambda
KeyError – 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
EventHandler
objects or namedfunctions
- Parameters
handler_name (
str
) – The identifier of the handler If the underlying event handler is of typeEventHandler
the name isevent_handler_obj.name
In case of afunction
the 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
list
ofstr
- Type
`Property
- has_handler(handler)[source]
Method to check if a certain event handler is already registered at the event
- Parameters
handler (
function
,str
orEventHandler
) – 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
orlambda
) – the callable handler object- Raises
TypeError – If handler is no instance of
EventHandler
afunction
or alambda
KeyError – If handlers name is not registered
- set_event_handler(handler)[source]
Reassigns a event handler
- Parameters
handler (
EventHandler
,function
orlambda
) – the callable handler object- Raises
TypeError – If handler is no instance of
EventHandler
afunction
or alambda
KeyError – If handlers name is not registered
- 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