Example script
In the pydagoras github repository folder tests includes the script eg_use_pydagoras.py
The following simple DAG
will be created, updated and displayed by the following code
| Import the necessary code from pydagoras |
|---|
| from pydagoras.dag_dot import DAG_dot, calc
|
| Define the node calculations |
|---|
| @calc
def tripple(node=None):
return node.get_value() * 3
|
| Create the DAG |
|---|
| dag = DAG_dot(label='Eg DAG')
|
| Define the input node and single calculation node |
|---|
| n2 = dag.makeNode('x3', calc=tripple, tooltip='multiply')
n1 = dag.makeNode('In', calc=None, usedby=[n2], nodetype='in')
|
Putting it all together...
| eg_use_pydagoras.py |
|---|
| # eg_use_pydagoras.py
# a script to provide an example of creating and using a DAG using pydagoras
from pydagoras.dag_dot import DAG_dot, calc
def run():
print('#######################################')
@calc
def tripple(node=None):
return node.get_value() * 3
dag = DAG_dot(label='Eg DAG')
n2 = dag.makeNode('x3', calc=tripple, tooltip='multiply')
n1 = dag.makeNode('In', calc=None, usedby=[n2], nodetype='in')
print('Initial DAG')
print(dag.G.to_string()) # (1)
print('Updates --------------')
dag.set_input('In', 10)
print('Outputs --------------')
dag.ppInputs() # (2)
dag.ppOutput() # (3)
dag.pp()
print('Final DAG')
print(dag.G.to_string()) # (4)
if __name__ == '__main__':
run()
print('Done.')
|
strict digraph {
graph [label="Eg DAG",
labelloc=t,
rankdir=LR
];
out [color=white];
x3 [tooltip=multiply];
x3 -> out [fontname=Courier,
label=Undefined];
In [shape=square];
In -> x3 [fontname=Courier,
label=Undefined];
}
DAG: Eg DAG
Inputs:
NODE: in, id:In, value:10
display_name:In tooltip:
calc: None usedby:x3
NODE: out, id:out, value:30
display_name:out tooltip:
calc: .calc_out at 0x1007b9b20> usedby:None
strict digraph {
graph [label="Eg DAG",
labelloc=t,
rankdir=LR
];
out [color=white];
x3 [color=green,
fontcolor=blue,
tooltip=multiply];
x3 -> out [color=green,
fontcolor=blue,
fontname=Courier,
label=30];
In [color=green,
fontcolor=blue,
shape=square];
In -> x3 [color=green,
fontcolor=blue,
fontname=Courier,
label=10];
}