Skip to content

Code instrumentation

The Python implementation of the protocol relies on the automatic code instrumentation that is implemented through the command function decorator. This decorator registers all necessary information when a method is called and offers convenient conversion functions. This works by inspecting the declared type of a method (using function annotations) and checking of the provided type has the right type. If this is not the case, the command search for a converter among those registered.

import gsp

@gsp.io.register("float", "int")
def float_to_int(value): return int(value)

class Foo(gsp.Object):
    @gsp.io.command("CREATE")
    def __init__(self, value : int):
        gsp.Object.__init__(self)
        self.value = value

foos = Foo(1), Foo(2)
print(gsp.io.queue("active"))
CommandQueue("default", active, read-write) : 2 command(s)
  - Command #1: Foo(id=1)/CREATE(…)
  - Command #2: Foo(id=2)/CREATE(…)

queue

queue(name='default')

Return a new or existing command queue. There is a special name "active" that relates to the current command queue. Each time a queue is created, it becomes automatically the active one.

record

record(state: bool = False)

Activate (state=True) or deactivate (state=False) global command recording (for all command queues).

command

command(name=None)

Function decorator that creates a command when the function is called and optionally record it. The name of the method can can be overriden with the provided name.

register

register(src_types: str | tuple[str, ...], dst_type: str)

Function decorator that registers a converter from src_types to dst_type

unregister

unregister(src_types: str | tuple[str, ...], dst_type: str)

Unregister converters from src_types to dst_type.

convert

convert(value: object, dst_type: str)

Return a converter from value type to dst_type, if there exists such a converter.