python task2.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python task2.py相关的知识,希望对你有一定的参考价值。

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

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

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

  def mark(self, name):
    self.system.add(name)

  def unmark(self, name):
    self.system.remove(name)

  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)

""" Система S
G <--- H
A <--- B <--- C <--- D
       ^      ^----- E
       +----- F 
"""

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)


S = [ tree2, tree1 ]
for n in S:
  up(n, lambda x: x.mark('S'))

""" Система C (возможно вырожденная т.к. (зависящий от B) узел A в ней как-бы не может находиться ?)
G <--- H
A <--- ? <--- ? <--- D
       ^      ^----- E
       +----- F <--- NEW1 <--- NEW2
"""

import copy
C = copy.deepcopy(S)
f = C[1].children[0].children[1]

new1 = Node('NEW1')
f.dependOn(new1)

new2 = Node('NEW2')
new1.dependOn(new2)

for n in C:
  up(n, lambda x: x.mark('C'))

c = C[1].children[0].children[0]
c.unmark('C') # unmark B/A ???

b = C[1].children[0]
b.unmark('C')



def messageIf(msg, l):
  def message(node):
    if l(node.system):
      print "%s.%s()" % (node, msg) # node.deactivate() :)

  return message

for n in C:
  down(n, messageIf('deactivate', lambda s: 'C' not in s))
  up(n, messageIf('activate', lambda s: 'C' in s and 'S' not in s))

以上是关于python task2.py的主要内容,如果未能解决你的问题,请参考以下文章

代写python,代写python编程,python代写,python编程代写,留学生python代写

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍