heros.event

Classes

Callback

Represent a callback function.

CallbackStorage

Store all callbacks.

EventHandler

Base class for event handlers.

LocalEventHandler

Handles event connections for a specific instance.

RemoteEventHandler

Handles remote events for a specific instance.

EventDescriptor

A descriptor to handle instance-specific event connections.

LocalEventDescriptor

Descriptor of @event decorated methods of a LocalHERO.

RemoteEventDescriptor

Descriptor of remote representations of events in a RemoteHERO.

Functions

remote_hero_method_to_str(→ str)

event(func)

Decorator for events.

Module Contents

heros.event.remote_hero_method_to_str(func: callable) str
class heros.event.Callback(func: callable, origin: str = None)

Represent a callback function.

func
origin = None
__eq__(other) bool

Check for equality with other Callback.

Parameters:

other – other callback instance.

Returns:

equality result.

Return type:

bool

__call__(*args, **kwargs)

Call the callback function.

__hash__()

Generate a hash value for this callback using the name in case of remote hero methods or the callable itself for builtins or local callables.

Returns:

calculated hash.

Return type:

int

to_dict() dict

Generate a dictionary representation of this callback.

Returns:

dictionary with keys: name, origin, is_remote_hero and func

Return type:

dict

class heros.event.CallbackStorage

Store all callbacks.

_callbacks
__iter__()

Generate an iteration for this iterable.

__contains__(func: callable) bool

Implements the in operation for this class.

append(func: callable, origin: str = None) str

Append a given callable to the storage.

Parameters:
  • funccallable to append.

  • originstr (default: None) indicating the origin of the callback.

Returns:

name of the callback.

Return type:

str

remove(func: callable) bool

Remove a callable from storage.

Parameters:

funccallable to remove.

Returns:

truth value indicating if the callable was a callback.

Return type:

bool

is_callback(func: callable) bool

Check if given callable is a callback.

Parameters:

funccallable to check.

Returns:

callable is a callback

Return type:

bool

get_callbacks() list

Get a list of all callbacks dictionaries.

Returns:

dictionary representation of all callbacks

Return type:

list

class heros.event.EventHandler

Base class for event handlers.

abstractmethod connect(callback: callable)

Connect a callback function.

abstractmethod disconnect(callback: callable)

Disconnect a callback function.

abstractmethod is_callback(func: callable) bool

Check if func is a callback

abstractmethod get_callbacks()

Return all callbacks

class heros.event.LocalEventHandler(instance, func)

Bases: EventHandler

Handles event connections for a specific instance.

instance
func
callbacks
__call__(*args, **kwargs)

Call the original function and trigger callbacks.

connect(callback: callable, origin: str = None) str

Connect a callback function to be triggered when the method is called.

Parameters:
  • callbackcallable to connect.

  • origin – (optional) str indicting origin of this callback.

Returns:

name of the callback.

Return type:

str

disconnect(callback: callable) bool

Disconnect a callback function.

Parameters:

callbackcallable to disconnect.

Returns:

truth value indicating if the callable was a callback.

Return type:

bool

is_callback(func: callable) bool

Check if given callable is already a registered callback.

Parameters:

callbackcallable to check.

Returns:

truth value indicating if the callable is a callback.

Return type:

bool

get_callbacks() list

Return a list of registered callback functions.

Returns:

dictionary representation of all callbacks

Return type:

list

class heros.event.RemoteEventHandler(instance, func: callable = None)

Bases: EventHandler

Handles remote events for a specific instance.

instance
callbacks
__call__(payload)

Call the original function and trigger callbacks.

connect(callback: callable) str

Connect a callback function to be triggered when the method is called.

Parameters:
  • callbackcallable to connect.

  • origin – (optional) str indicting origin of this callback.

Returns:

name of the callback.

Return type:

str

disconnect(callback: callable) None

Disconnect a callback function.

Parameters:

callbackcallable to disconnect.

Returns:

truth value indicating if the callable was a callback.

Return type:

bool

is_callback(func: callable) bool

Check if given callable is already a registered callback.

Parameters:

callbackcallable to check.

Returns:

truth value indicating if the callable is a callback.

Return type:

bool

get_callbacks() list

Return a list of registered callback functions.

Returns:

dictionary representation of all callbacks

Return type:

list

class heros.event.EventDescriptor(func: callable = None)

A descriptor to handle instance-specific event connections.

func = None
_instances
static _get_event_handler_cls()
Abstractmethod:

__get__(instance, owner)

Ensure the method and event-handling functions are bound to the instance.

Parameters:
  • self – the EventDescriptor instance

  • instance – the owning LocalHERO/RemoteHERO.

  • owner

class heros.event.LocalEventDescriptor(func: callable = None)

Bases: EventDescriptor

Descriptor of @event decorated methods of a LocalHERO.

static _get_event_handler_cls()
class heros.event.RemoteEventDescriptor(func: callable = None)

Bases: EventDescriptor

Descriptor of remote representations of events in a RemoteHERO.

static _get_event_handler_cls()
heros.event.event(func: callable)

Decorator for events.

Note

Only use on methods bound to objects.