7.2.2.1. Base

class Base[source]

Methods

get_node Returns a sub node of a dispatcher.
plot Plots the Dispatcher with a graph in the DOT language with Graphviz.
search_node_description
web Creates a dispatcher Flask app.
__init__()

Initialize self. See help(type(self)) for accurate signature.

web(depth=-1, node_data=none, node_function=none, directory=None, sites=None, run=True)[source]

Creates a dispatcher Flask app.

Parameters:
  • depth (int, optional) – Depth of sub-dispatch plots. If negative all levels are plotted.
  • node_data (tuple[str], optional) – Data node attributes to view.
  • node_function (tuple[str], optional) – Function node attributes to view.
  • directory (str, optional) – Where is the generated Flask app root located?
  • sites (set[Site], optional) – A set of Site() to maintain alive the backend server.
  • run (bool, optional) – Run the backend server?
Returns:

A WebMap.

Return type:

WebMap

Example:

From a dispatcher like this:

>>> from schedula import Dispatcher
>>> dsp = Dispatcher(name='Dispatcher')
>>> def fun(a):
...     return a + 1, a - 1
>>> dsp.add_function('fun', fun, ['a'], ['b', 'c'])
'fun'

digraph dmap { graph [ratio=1] node [style=filled] label = <dmap> splines = ortho style = filled 195 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2">a</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=a] 196 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2">b</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=b] 197 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2">c</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=c] 198 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-4c58ed1f91d2ba767009c023dc42336977979c35/fun.html">fun</TD></TR></TABLE>> fillcolor=springgreen shape=box tooltip=fun] 198 -> 197 195 -> 198 198 -> 196 }

You can create a web server with the following steps:

>>> webmap = dsp.web()
>>> print("Starting...\n"); site = webmap.site().run(); site
Starting...
Site(WebMap([(Dispatcher, WebMap())]), host='localhost', ...)
>>> import requests
>>> url = '%s/%s/%s' % (site.url, dsp.name, fun.__name__)
>>> requests.post(url, json={'args': (0,)}).json()['return']
[1, -1]
>>> site.shutdown()  # Remember to shutdown the server.
True

Note

When Site() is garbage collected the server is shutdown automatically.

plot(workflow=None, view=True, depth=-1, name=none, comment=none, format=none, engine=none, encoding=none, graph_attr=none, node_attr=none, edge_attr=none, body=none, node_styles=none, node_data=none, node_function=none, edge_data=none, max_lines=none, max_width=none, directory=None, sites=None, index=False)[source]

Plots the Dispatcher with a graph in the DOT language with Graphviz.

Parameters:
  • workflow (bool, optional) – If True the latest solution will be plotted, otherwise the dmap.
  • view (bool, optional) – Open the rendered directed graph in the DOT language with the sys default opener.
  • edge_data (tuple[str], optional) – Edge attributes to view.
  • node_data (tuple[str], optional) – Data node attributes to view.
  • node_function (tuple[str], optional) – Function node attributes to view.
  • node_styles (dict[str|Token, dict[str, str]]) – Default node styles according to graphviz node attributes.
  • depth (int, optional) – Depth of sub-dispatch plots. If negative all levels are plotted.
  • name (str) – Graph name used in the source code.
  • comment (str) – Comment added to the first line of the source.
  • directory (str, optional) – (Sub)directory for source saving and rendering.
  • format (str, optional) – Rendering output format (‘pdf’, ‘png’, …).
  • engine (str, optional) – Layout command used (‘dot’, ‘neato’, …).
  • encoding (str, optional) – Encoding for saving the source.
  • graph_attr (dict, optional) – Dict of (attribute, value) pairs for the graph.
  • node_attr (dict, optional) – Dict of (attribute, value) pairs set for all nodes.
  • edge_attr (dict, optional) – Dict of (attribute, value) pairs set for all edges.
  • body (dict, optional) – Dict of (attribute, value) pairs to add to the graph body.
  • directory – Where is the generated Flask app root located?
  • sites (set[Site], optional) – A set of Site() to maintain alive the backend server.
  • index (bool, optional) – Add the site index as first page?
  • max_lines (int, optional) – Maximum number of lines for rendering node attributes.
  • max_width (int, optional) – Maximum number of characters in a line to render node attributes.
  • view – Open the main page of the site?
Returns:

A SiteMap.

Return type:

schedula.utils.drw.SiteMap

Example:

>>> from schedula import Dispatcher
>>> dsp = Dispatcher(name='Dispatcher')
>>> def fun(a):
...     return a + 1, a - 1
>>> dsp.add_function('fun', fun, ['a'], ['b', 'c'])
'fun'
>>> dsp.plot(view=False, graph_attr={'ratio': '1'})
SiteMap([(Dispatcher, SiteMap())])

digraph dmap { graph [ratio=1] node [style=filled] label = <dmap> splines = ortho style = filled 142 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2">a</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=a] 143 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2">b</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=b] 144 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2">c</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=c] 145 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-f55282f8d59c8bbcca8a2ef07b63000e69224eaf/fun.html">fun</TD></TR></TABLE>> fillcolor=springgreen shape=box tooltip=fun] 145 -> 144 142 -> 145 145 -> 143 }

get_node(*node_ids, node_attr=none)[source]

Returns a sub node of a dispatcher.

Parameters:
  • node_ids (str) – A sequence of node ids or a single node id. The id order identifies a dispatcher sub-level.
  • node_attr (str, None, optional) –

    Output node attr.

    If the searched node does not have this attribute, all its attributes are returned.

    When ‘auto’, returns the “default” attributes of the searched node, which are:

    • for data node: its output, and if not exists, all its attributes.
    • for function and sub-dispatcher nodes: the ‘function’ attribute.

    When ‘description’, returns the “description” of the searched node, searching also in function or sub-dispatcher input/output description.

    When ‘output’, returns the data node output.

    When ‘default_value’, returns the data node default value.

    When ‘value_type’, returns the data node value’s type.

    When None, returns the node attributes.

Returns:

Node attributes and its real path.

Return type:

(T, (str, ..))

Example:

digraph workflow { graph [ratio=1] node [style=filled] label = <workflow> splines = ortho style = filled 55 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-2d06435e5aab4ad97f99e77067ca941be8cd6776/Sub-dispatcher.html">Sub-dispatcher</TD></TR><TR><TD align="RIGHT" border="1">distance</TD><TD align="LEFT" border="1">1.0</TD></TR><TR><TD align="RIGHT" border="1">started</TD><TD align="LEFT" border="1">2018-10-08T22:02:34.581977</TD></TR><TR><TD align="RIGHT" border="1">duration</TD><TD align="LEFT" border="1">0:00:00.000371</TD></TR></TABLE>> fillcolor=yellow shape=note style=filled tooltip="Sub-dispatcher"] 56 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-2d06435e5aab4ad97f99e77067ca941be8cd6776/a-output.html">a</TD></TR><TR><TD align="RIGHT" border="1">distance</TD><TD align="LEFT" border="1">0.0</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=a] 57 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-2d06435e5aab4ad97f99e77067ca941be8cd6776/b-output.html">b</TD></TR><TR><TD align="RIGHT" border="1">distance</TD><TD align="LEFT" border="1">2.0</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=b] 58 [label=start fillcolor=red shape=egg] 55 -> 57 58 -> 56 56 -> 55 }

Get the sub node output:

>>> d.get_node('Sub-dispatcher', 'c')
(4, ('Sub-dispatcher', 'c'))
>>> d.get_node('Sub-dispatcher', 'c', node_attr='type')
('data', ('Sub-dispatcher', 'c'))
>>> sub_dsp, sub_dsp_id = d.get_node('Sub-dispatcher')

digraph workflow { graph [ratio=1] node [style=filled] label = <workflow> splines = ortho style = filled 72 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-e7fdaa767629f1ee50c33184e60ca16232ee7525/a-output.html">a</TD></TR><TR><TD align="RIGHT" border="1">distance</TD><TD align="LEFT" border="1">0.0</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=a] 73 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-e7fdaa767629f1ee50c33184e60ca16232ee7525/fun.html">a + b</TD></TR><TR><TD align="RIGHT" border="1">distance</TD><TD align="LEFT" border="1">1.0</TD></TR><TR><TD align="RIGHT" border="1">started</TD><TD align="LEFT" border="1">2018-10-08T22:02:35.598171</TD></TR><TR><TD align="RIGHT" border="1">duration</TD><TD align="LEFT" border="1">0:00:00.000006</TD></TR></TABLE>> fillcolor=springgreen shape=box tooltip="a + b"] 74 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-e7fdaa767629f1ee50c33184e60ca16232ee7525/b-output.html">b</TD></TR><TR><TD align="RIGHT" border="1">distance</TD><TD align="LEFT" border="1">0.0</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=b] 75 [label=<<TABLE border="0" cellspacing="0"><TR><TD border="0" colspan="2" href="./dispatcher-e7fdaa767629f1ee50c33184e60ca16232ee7525/c-output.html">c</TD></TR><TR><TD align="RIGHT" border="1">distance</TD><TD align="LEFT" border="1">2.0</TD></TR></TABLE>> fillcolor=cyan shape=box style="rounded,filled" tooltip=c] 76 [label=start fillcolor=red shape=egg] 72 -> 73 73 -> 75 76 -> 72 76 -> 74 74 -> 73 }