Objects

The fundamental base class for the key components of MxDC is the Object. It is a subclass of GObject.Object and provides several key features such as Signals registration and connection to callbacks, typed properties, and all other features supported by GObject.Object.

Signals are a system for creating named events and registering callbacks to them. mxdc.Signal is an alias for GObject.Signal

Example

The following is an example of an object supporting two signals with different numbers of arguments, how to connect to callbacks and trigger events.

Note

An appropriate main-loop is required. These examples can be tried out using an ipython shell with the Gtk3 main loop active. This can be activated using the IPython magic command %gui gtk3.

from mxdc import Object, Signal

class Door(Object):

    class Signals:
        opened = Signal('opened', arg_types=(bool,))
        locked = Signal('locked', arg_types=(bool, str))


def on_lock(obj, state, message):
    print('Door {}locked, {}.'.format('un' if not state else '', message))

def on_opened(obj, state):
    print('Door {}.'.format('opened' if state else 'closed'))
>>> door = Door()
>>> door.connect('opened', on_opened)
17L
>>> door.connect('locked', on_lock)
18L
>>> door.emit('opened', True)
Door opened.

>>> door.emit('opened', False)
Door closed.

>>> door.emit('locked', True, 'come back later')
Door locked, come back later.

>>> # set state and emit multiple signals simultaneously
>>> door.set_state(locked=(True, 'access forbidden'), opened=False)
Door locked, access forbidden.
Door closed.

>>> door.get_state('locked')
(True, "access forbidden")

API Details

class mxdc.Object

Base Class for event-aware objects in MxDC which can emit signals and register callbacks.

Signals are defined through the Signals attribute class as follows:

class Signals:
    name = Signal('name', arg_types=(str,))
    ready = Signal('ready', arg_types=(bool, str))
emit(signal: str, *args, force=False)

Emit the signal. Signal emissions are thread safe and will be handled in the main thread.

Parameters
  • signal – Signal name

  • args – list of signal parameters

  • force – if True emit the signal even if the value is the same as before

get_state(item: str)

Get a specific state by key. The key is transformed so that underscores are replaced with hyphens (i.e, ‘event_name’ is translated to ‘event-name’)

Parameters

item – state key

get_states()

Obtain a copy of the internal state dictionary. The returned dictionary is not neccessarily usable as kwargs for set_state due to the ‘_’ to ‘-’ transformation of the keys.

set_state(*args, **kwargs)

Set the state of the object and emit the corresponding signal.

Parameters
  • args – list of strings corresponding to non-value signal names

  • kwargs – name, value pairs corresponding to signal name and signal arguments.