Nodes in python

If you want to work with Library nodes from python code, use the interactive module. The interactive module is intended for experimentation, scripting, and test and aims to make it convenient to work in IPython or similar.

The code example below demonstrates how to load the interactive module.

from sympathy.app import interactive

Interactive relies on sympathy.api in order to produce port data such as ADAF or Table, but using sympathy.api explicitly is not required.

Loading the Library

from sympathy.app import interactive

library = interactive.load_library()

load_library may produce warnings similarly to the ones produced when running Sympathy for Data as GUI or CLI.

Loading nodes

When the Library has been loaded you are ready to begin loading nodes. The nodes can be loaded by nodeid or name and if no match is found the method will also attempt to do a fuzzy match of the provided name. If more than one node is matched, then a KeyError is produced listing which nodes that match the given name. Matching the node name is often good enough, it should certainly be unique within a library and is easy to read compared to the full nodeid.

random_table = library.node('Random Table')

Working with configurations

There are two different ways of configuring nodes: graphical and programmatic. When working interactively it is often a good start to use the graphical interface, the programmatic interface is more useful for automation and tests. Some nodes have very complex configurations that can be hard to get right and, for those cases, the graphical interface is recommended.

Graphical interface

The code example below demonstrates how to launch the configuration GUI for a Random Table node. The node remembers its configuration and the changes will have effect when the node is executed and in other cases when its configuration is used.

random_table = library.node('Random Table')
random_table.configure()

Programmatic interface

The code example below demonstrates how to set the column_entries attributes of a Random Table node to the value 3. This change will make the node produce 3 random columns instead of the default 5, when executed.

random_table = library.node('Random Table')
random_table.parameters['column_entries'].value = 3

Working with nodes

Nodes store the changes made during configure and when the parameters are changed. They produce a list of data elements when executed and expect a list of data elements as input, this makes it possible to easily connect the data between nodes. Note that the ordering of inputs and outputs is important and should match the declaration order in the node definition.

The code example below demonstrates how to use the result produced by one node as input for another.

random_table = library.node('Random Table')
rt_output = random_table.execute()

item_to_list = library.node('Item to List')
itl_output = item_to_list.execute(rt_output)

assert itl_output[0][0].equal_to(rt_output[0])

The code example below demonstrates how to use the result produced by multiple nodes as input for another.

random_table0 = library.node('Random Table')
rt_output0 = random_table.execute()

random_table1 = library.node('Random Table')
rt_output1 = random_table.execute()

vjoin_table = library.node('VJoin Table')
vj_output = vjoin_table.execute(rt_output0 + rt_output1)

Reference

exception sympathy.app.interactive.InteractiveNotNodeError[source]
class sympathy.app.interactive.SyiLibrary(context, library, name_library, paths)[source]

A library of nodes that can be configured and executed in Python code.

Should not be instantiated directly. Instead call load_library().

node(nid, fuzzy_names=True) SyiNode[source]

Attempt to find nid in the library.

Argument nid can be either a node id or a node name. If no matching node can be found a KeyError is raised.

If fuzzy_names is True (the default) and nid doesn’t match any node exactly, it is used as a pattern that the node name must match. The characters of the pattern must appear in the node name in the same order as in the pattern, but must not be of the same case, and may have other characters in between them. If multiple nodes match the pattern a KeyError is raised.

nodeids() list[str][source]

Return a list of all nodeids in this library.

class sympathy.app.interactive.SyiNode(context, node, parameters, filename)[source]

A Sympathy node which can be configured and executed from Python code.

Should not be instantiated directly. Instead use SyiLibrary.node().

configure(inputs=None)[source]

Configure node by launching parameter GUI.

Requires that gui=True was passed to load_library().

execute(inputs: list[Any] | None = None) list[Any][source]

Execute node.

If the node has input ports, those should be passed in a list. Any output ports are returned in a list. E.g.:

>>> node.execute([table1, table2])
[<sympathy.typeutils.table.Table at 0x000000000000>]
property parameters

Node parameters

Can be modified to programmatically configure the node.

sympathy.app.interactive.end_session()[source]

End a session, tearing down the setup performed by start_session().

This function needs to be called in order to return a floating license.

Consider using session() to ensure that this gets called at the end of the sesssion.

Calling this if not in a session does nothing.

sympathy.app.interactive.load_library(gui: bool = True, library_id: str | None = None) SyiLibrary[source]

Load the node library.

If library_id is None, then all node libraries available in Sympathy will be loaded. Otherwise load only the library with the specified library id.

In order to be able to launch node configuration GUIs using SyiNode.configure(), gui must be True (the default). Setting gui to False might be required for scripts running without a graphical environment (e.g. headless servers).

Runs start_session() if not already in a session.

sympathy.app.interactive.node(node_cls: Type[BasicNode]) SyiNode[source]

Get a SyiNode from a node class.

Runs start_session() if not already in a session.

sympathy.app.interactive.session()[source]

Context manager for starting and ending a session.

This context manager can be used to ensure that end_session() is always called at the end of the session, even if there is an exception.

Example:

from sympathy.app import interactive
with interactive.session():
    library = interactive.load_library()
    node = library.node('Random Table')
    print(node.execute()[0])
sympathy.app.interactive.start_session()[source]

Start a session.

Consider using session() instead to ensure that end_session() gets called at the end of the sesssion.

Calling this if not in a session does nothing.