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.
Sessions¶
Work with nodes inside a session. The easiest way is a with block, which
starts the session and ends it when the block is left, even after an
exception:
from sympathy.app import interactive
with interactive.session():
library = interactive.load_library()
...
load_library and node start a session by themselves if none is
running, but such a session lasts until end_session is called. Until then
it keeps its license seat, see Licensing.
Loading the Library¶
from sympathy.app import interactive
with interactive.session():
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.
with interactive.session():
library = interactive.load_library()
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.
with interactive.session():
library = interactive.load_library()
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.
with interactive.session():
library = interactive.load_library()
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.
with interactive.session():
library = interactive.load_library()
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.
with interactive.session():
library = interactive.load_library()
random_table0 = library.node('Random Table')
rt_output0 = random_table0.execute()
random_table1 = library.node('Random Table')
rt_output1 = random_table1.execute()
vjoin_table = library.node('VJoin Table')
vj_output = vjoin_table.execute(rt_output0 + rt_output1)
Licensing¶
A session counts as one running instance of Sympathy for Data, just like the
GUI or sympathy cli. On an Enterprise Floating license it uses one seat,
and on an Enterprise Server license it counts as one copy toward the
license’s limit. See License Management for the license types.
The seat is taken when the first node is executed. That node may wait up to 30 seconds for the licensing server to answer, and it is refused if every seat is in use. If the server has not answered within 30 seconds, the node still runs if the license on this computer was active when it was last checked; otherwise it is refused with a message saying that the license is still being checked, and you can try again in a moment.
The seat is kept for as long as nodes keep running. After about an hour without a node, the seat is given up. The next node then takes a seat again, which may make it wait up to 30 seconds, and it is refused if every seat is in use.
The license is checked again as nodes run, so a license that is suspended or expires, or a computer that has been offline for longer than the license allows, makes later nodes in the session be refused.
The seat is given back when end_session is called or when a
with interactive.session(): block is left. Otherwise, for example when a
script or a notebook stops without ending its session, the seat comes back
after about an hour, or when Sympathy for Data is next started on that
computer.
Reference¶
- 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.
- 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: ParameterRoot¶
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 seat. A session that is never ended keeps its seat until it has run no node for about an hour.
Consider using
session()to ensure that this gets called at the end of the session.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). If a session has already been started, this argument has no effect.Runs
start_session()if not already in a session. Prefer calling this inside asession()block, so that a floating license seat is given back, seeend_session().
- sympathy.app.interactive.node(node_cls: Type[BasicNode]) SyiNode[source]¶
Get a
SyiNodefrom a node class.Runs
start_session()to initiate a session if needed. Prefer calling this inside asession()block, so that a floating license seat is given back, seeend_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.
Prefer
session(), which makes sure thatend_session()gets called at the end of the session, giving back a floating license seat.Calling this if already in a session does nothing.