Code reference - pydagoras
Description: Documentation of the pydagoras package

Skip to content

# Code reference

## dag_dot.py

## `DAG`

Bases: `object`

Base DAG

Source code in `pydagoras/dag_dot.py`

     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    class DAG(object): # functionality
        '''Base DAG '''
        __shared_state = {}  # would be nice to use a collections.OrderedDict()

        def __init__(self, filename):
            '''__init__'''
            self.__dict__ = self.__shared_state

            self.filename = filename
            self.G=pgv.AGraph(directed=True,strict=True,rankdir='LR', label='Eg DAG')
            self.input_nodes=[]
            #print( self.G.to_string() )

        def makeNode(self,label,calc,usedby,nodetype):
            '''makeNode'''
            n = Node(label,calc,usedby,nodetype)
            if nodetype == 'in':
                self.input_nodes.append(n)
            self.defNode(n,usedby =usedby, nodetype=nodetype)
            return n

        def defNode(self,node,usedby,nodetype):
            '''defNode'''
            doc = node.doc
            if not doc:
                doc = 'qwerty'

            if nodetype == 'in':
                self.G.add_node(doc, shape="square")
                #node= self.AddInNode(doc)
                for n in usedby:
                    self.AddEdge(doc,n.doc)
            elif nodetype == 'internal':
                #node= self.AddNode(doc,self.calcRateA)
                for n in usedby:
                    self.AddEdge(doc,n.doc)
            elif nodetype == 'out':
                self.G.add_node(doc, color="white")

        def AddEdge(self,node1,node2):
            self.G.add_edge(node1,node2,label='Undefined')
            #print( self.G.to_string() )

        def update_node(self,node1,node2,value):
            #if node2.nodetype == 'out':
            #    mr_mess_bus.publish(node.doc,value)

            color = 'green'
            fontcolor='blue'
            if value == '-':
                fontcolor='black'
            elif value in ( 0, 'e'):
                fontcolor='red'
                color='red'

            #self.G.add_node(node1,color=color,fontcolor=fontcolor,tooltip='hello')
            #A [URL="[A|home]" tooltip="A link"]
            self.G.add_node(node1,color=color,fontcolor=fontcolor,URL=node1+'.html',tooltip=node1)
            self.G.add_edge(node1,node2, label=value,fontcolor=fontcolor,color=color)
            print('added node and edge')
            #print( self.G.to_string() )
            self.dot_pp()

            #t = threading.Timer(1, self.fade, args=(node1, node2,value,color) )
            #t.start()

        def fade(self,node1, node2,value,color):
            print ('FADE')
            1/0
            fontcolor=color
            color = color
            self.G.add_node(node1,color=color,fontcolor=fontcolor,URL=node1+'.html',tooltip=node1)
            self.G.add_edge(node1,node2, label=value,fontcolor=fontcolor,color=color)
            self.dot_pp()
            print('fade')

        def set_input(self,doc,value):
            for node in self.input_nodes:
                if node.doc == doc:
                    logger.info ('set %s %s' %(node.doc,value))
                    for usedby in node.usedby:
                         self.update_node(doc,usedby.doc, value=value)
                    self.setValue(node,value)

                    graph_def = self.G.to_string()
                    #print( graph_def )
                    return graph_def

        def dot_pp(self):
            pass
            #print 'print dot and convert to png'
            #self.G.layout(prog='dot') # layout with default (neato)
            #print 'Draw'
            #self.G.draw(self.filename)

        def setValue(self,n,v):
            if v == n.value:
                return

            # build the DAG
            n.value = v
            for u in n.usedby:
               if u.calc == None:
                   continue
               new_value = None
               try:
                  #u.pp()
                  new_value = u.calc(node=n)
               except Exception as e:
                  print (str(e))

               self.setValue(u,new_value)

               # if output print
            print ('SET VALUE used by', n.usedby[0].doc)
            if n.usedby[0].usedby == []:
                #print '!! SET VALUE OF OUTPUT'
                msg = 'update dag_dot.py %s %s' %(n.usedby[0].doc, n.value)
                logger.info (msg)
                #print msg
                #print('dont publish')
                #print( self.G.to_string() )
                #publish.run(n.usedby[0].doc,str(n.value))
                #mr_mess_bus.publish(n.usedby[0].doc,str(n.value))

        def pp(self): # must be over ridden by a borg
            # use doc string on class
            print (self.__doc__)
            for k, v in self.__dict__ .items():
                if type(v) == type(Node()):
                    print (k,)
                    v.pp()

        def ppInputs(self):
            print (self.__doc__, ' Inputs')
            for n in self.input_nodes:
                n.pp()

        def ppOutputs(self):
            print (self.__doc__, ' Outputs')
            for k, v in self.__dict__ .items():
                if type(v) == type(Node()):
                    if v.usedby == []:
                        print (k,)
                        print ('=', v.value, v.doc)

### `__init__(filename)`

init

Source code in `pydagoras/dag_dot.py`

    15
    16
    17
    18
    19
    20
    21
    def __init__(self, filename):
        '''__init__'''
        self.__dict__ = self.__shared_state

        self.filename = filename
        self.G=pgv.AGraph(directed=True,strict=True,rankdir='LR', label='Eg DAG')
        self.input_nodes=[]

### `defNode(node, usedby, nodetype)`

defNode

Source code in `pydagoras/dag_dot.py`

    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    def defNode(self,node,usedby,nodetype):
        '''defNode'''
        doc = node.doc
        if not doc:
            doc = 'qwerty'

        if nodetype == 'in':
            self.G.add_node(doc, shape="square")
            #node= self.AddInNode(doc)
            for n in usedby:
                self.AddEdge(doc,n.doc)
        elif nodetype == 'internal':
            #node= self.AddNode(doc,self.calcRateA)
            for n in usedby:
                self.AddEdge(doc,n.doc)
        elif nodetype == 'out':
            self.G.add_node(doc, color="white")

### `makeNode(label, calc, usedby, nodetype)`

makeNode

Source code in `pydagoras/dag_dot.py`

    24
    25
    26
    27
    28
    29
    30
    def makeNode(self,label,calc,usedby,nodetype):
        '''makeNode'''
        n = Node(label,calc,usedby,nodetype)
        if nodetype == 'in':
            self.input_nodes.append(n)
        self.defNode(n,usedby =usedby, nodetype=nodetype)
        return n

## `calc(f1)`

calc

Source code in `pydagoras/dag_dot.py`

    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    def calc(f1):
            ''' calc '''
            def f3(self,*args, **kwargs):
                node=kwargs['node']

                for u_node in node.usedby:
                    for o_node in u_node.usedby:
                        self.update_node(u_node.doc,o_node.doc, value='-')

                try:
                    rtn = f1(self,*args, **kwargs)
                except Exception as e:
                    print ('Error in %s: %s' %(u_node.doc,str(e)))
                    #rtn = str(e)
                    rtn = 'e'

                for u_node in node.usedby:
                    for o_node in u_node.usedby:
                        self.update_node(u_node.doc,o_node.doc, value=rtn)

                self.dot_pp()
                return rtn
            return f3

## node.py

## `Node`

Bases: `object`

Node

Source code in `pydagoras/node.py`

    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class Node(object):
        ''' Node '''
        def __init__(self,doc=None, calc=None,usedby=None, nodetype=None):
            ''' __init__ '''
            self.calc = calc
            self.doc = doc
            self.usedby = usedby
            self.value = None
            self.nodetype = nodetype

        def pp(self):
            ''' __pp__ '''
            if self.usedby:
                print ("= %s, %s, used by, %s" %( self.value , self.doc, [n.doc for n in self.usedby]))

### `__init__(doc=None, calc=None, usedby=None, nodetype=None)`

init

Source code in `pydagoras/node.py`

    12
    13
    14
    15
    16
    17
    18
    def __init__(self,doc=None, calc=None,usedby=None, nodetype=None):
        ''' __init__ '''
        self.calc = calc
        self.doc = doc
        self.usedby = usedby
        self.value = None
        self.nodetype = nodetype

### `pp()`

pp

Source code in `pydagoras/node.py`

    20
    21
    22
    23
    def pp(self):
        ''' __pp__ '''
        if self.usedby:
            print ("= %s, %s, used by, %s" %( self.value , self.doc, [n.doc for n in self.usedby]))

Was this page helpful?
Thanks for your feedback!
Thanks for your feedback! Help us improve this page by using our feedback form.