Example script

In the pydagoras github repository folder tests includes the script eg_use_pydagoras.py

The following code will create the simple DAG

image

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')
Print the initial DAG
    print(dag.G.to_string())  
Update the DAG input
    dag.set_input('In', 10)
Print the final DAG
    print(dag.G.to_string()) 

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.')
  1. 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];
    }
    image
  2. DAG: Eg DAG
    Inputs:
    NODE: in, id:In, value:10
          display_name:In tooltip:
          calc: None usedby:x3
  3. comment y I'm a code annotation! I can contain code, __formatted
  4. 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];
    }
    image