dketov
9/19/2014 - 5:41 PM

task1.py

#!/usr/bin/python
# -*- encoding: utf-8 -*-

class Node:
  def __init__(self, name, children=None):
    self.children = children or list()
    self.name = name

  def dependOn(self, node):
    self.children.append(node)

  def __str__(self):
    return self.name

  def __repr__(self):
    return str(self)

def down(root, callable):
    callable(root)
    for l in root.children:
      down(l, callable)

def up(root, callable):
    for l in root.children:
      up(l, callable)
    callable(root)

"""
G <--- H
A <--- B <--- C <--- D
       ^      ^----- E
       +----- F 

treeList = [ Node( 'A',
             [ Node('B',
                   [ Node('C',
                         [ Node('D'), Node('E') ]),
                     Node('F')
                   ])
             ]),
         Node('G', [ Node('H') ])
       ]
"""

tree1=a=Node('A')
b=Node('B')
c=Node('C')
d=Node('D')
e=Node('E')
f=Node('F')
tree2=g=Node('G')
h=Node('H')

a.dependOn(b)
b.dependOn(c)
c.dependOn(d)
c.dependOn(e)
b.dependOn(f)
g.dependOn(h)

def message(msg):
  def message(node):
    print "%s.%s()" % (node, msg) # node.activate() :)

  return message

treeList = [ tree2, tree1 ]

for n in treeList:
  up(n, message('activate'))