提交 55470b76 authored 作者: Frederic's avatar Frederic

pep8 fix.

上级 60d6b2e5
...@@ -10,7 +10,7 @@ class AlreadyThere(Exception): ...@@ -10,7 +10,7 @@ class AlreadyThere(Exception):
class Bookkeeper: class Bookkeeper:
def on_attach(self, env): def on_attach(self, env):
for node in graph.io_toposort(env.inputs, env.outputs): for node in graph.io_toposort(env.inputs, env.outputs):
self.on_import(env, node) self.on_import(env, node)
...@@ -18,7 +18,7 @@ class Bookkeeper: ...@@ -18,7 +18,7 @@ class Bookkeeper:
def on_detach(self, env): def on_detach(self, env):
for node in graph.io_toposort(env.inputs, env.outputs): for node in graph.io_toposort(env.inputs, env.outputs):
self.on_prune(env, node) self.on_prune(env, node)
class History: class History:
...@@ -27,7 +27,8 @@ class History: ...@@ -27,7 +27,8 @@ class History:
def on_attach(self, env): def on_attach(self, env):
if hasattr(env, 'checkpoint') or hasattr(env, 'revert'): if hasattr(env, 'checkpoint') or hasattr(env, 'revert'):
raise AlreadyThere("History feature is already present or in conflict with another plugin.") raise AlreadyThere("History feature is already present or in"
" conflict with another plugin.")
self.history[env] = [] self.history[env] = []
env.checkpoint = lambda: len(self.history[env]) env.checkpoint = lambda: len(self.history[env])
env.revert = partial(self.revert, env) env.revert = partial(self.revert, env)
...@@ -41,8 +42,9 @@ class History: ...@@ -41,8 +42,9 @@ class History:
if self.history[env] is None: if self.history[env] is None:
return return
h = self.history[env] h = self.history[env]
h.append(lambda: env.change_input(node, i, r, reason=("Revert", reason))) h.append(lambda: env.change_input(node, i, r,
reason=("Revert", reason)))
def revert(self, env, checkpoint): def revert(self, env, checkpoint):
""" """
Reverts the graph to whatever it was at the provided Reverts the graph to whatever it was at the provided
...@@ -61,8 +63,10 @@ class Validator: ...@@ -61,8 +63,10 @@ class Validator:
def on_attach(self, env): def on_attach(self, env):
if hasattr(env, 'validate'): if hasattr(env, 'validate'):
raise AlreadyThere("Validator feature is already present or in conflict with another plugin.") raise AlreadyThere("Validator feature is already present or in"
" conflict with another plugin.")
env.validate = lambda: env.execute_callbacks('validate') env.validate = lambda: env.execute_callbacks('validate')
def consistent(): def consistent():
try: try:
env.validate() env.validate()
...@@ -81,9 +85,10 @@ class ReplaceValidate(History, Validator): ...@@ -81,9 +85,10 @@ class ReplaceValidate(History, Validator):
def on_attach(self, env): def on_attach(self, env):
History.on_attach(self, env) History.on_attach(self, env)
Validator.on_attach(self, env) Validator.on_attach(self, env)
for attr in ('replace_validate', 'replace_all_validate'): for attr in ('replace_validate', 'replace_all_validate'):
if hasattr(env, attr): if hasattr(env, attr):
raise AlreadyThere("ReplaceValidate feature is already present or in conflict with another plugin.") raise AlreadyThere("ReplaceValidate feature is already present"
" or in conflict with another plugin.")
env.replace_validate = partial(self.replace_validate, env) env.replace_validate = partial(self.replace_validate, env)
env.replace_all_validate = partial(self.replace_all_validate, env) env.replace_all_validate = partial(self.replace_all_validate, env)
...@@ -101,10 +106,16 @@ class ReplaceValidate(History, Validator): ...@@ -101,10 +106,16 @@ class ReplaceValidate(History, Validator):
for r, new_r in replacements: for r, new_r in replacements:
try: try:
env.replace(r, new_r, reason=reason) env.replace(r, new_r, reason=reason)
print reason
except Exception, e: except Exception, e:
if 'The type of the replacement must be the same' not in str(e) and 'does not belong to this Env' not in str(e): if ('The type of the replacement must be the same' not in
print >> sys.stderr, "<<!! BUG IN ENV.REPLACE OR A LISTENER !!>>", type(e), e, reason str(e) and 'does not belong to this Env' not in str(e)):
env.revert(chk) # this might fail if the error is in a listener: (env.replace kinda needs better internal error handling) out = sys.stderr
print >> out, "<<!! BUG IN ENV.REPLACE OR A LISTENER !!>>",
print >> out, type(e), e, reason
# this might fail if the error is in a listener:
# (env.replace kinda needs better internal error handling)
env.revert(chk)
raise raise
try: try:
env.validate() env.validate()
...@@ -117,19 +128,21 @@ class NodeFinder(dict, Bookkeeper): ...@@ -117,19 +128,21 @@ class NodeFinder(dict, Bookkeeper):
def __init__(self): def __init__(self):
self.env = None self.env = None
def on_attach(self, env): def on_attach(self, env):
if self.env is not None: if self.env is not None:
raise Exception("A NodeFinder instance can only serve one Env.") raise Exception("A NodeFinder instance can only serve one Env.")
if hasattr(env, 'get_nodes'): if hasattr(env, 'get_nodes'):
raise AlreadyThere("NodeFinder is already present or in conflict with another plugin.") raise AlreadyThere("NodeFinder is already present or in conflict"
" with another plugin.")
self.env = env self.env = env
env.get_nodes = partial(self.query, env) env.get_nodes = partial(self.query, env)
Bookkeeper.on_attach(self, env) Bookkeeper.on_attach(self, env)
def on_detach(self, env): def on_detach(self, env):
if self.env is not env: if self.env is not env:
raise Exception("This NodeFinder instance was not attached to the provided env.") raise Exception("This NodeFinder instance was not attached to the"
" provided env.")
self.env = None self.env = None
del env.get_nodes del env.get_nodes
Bookkeeper.on_detach(self, env) Bookkeeper.on_detach(self, env)
...@@ -137,7 +150,7 @@ class NodeFinder(dict, Bookkeeper): ...@@ -137,7 +150,7 @@ class NodeFinder(dict, Bookkeeper):
def on_import(self, env, node): def on_import(self, env, node):
try: try:
self.setdefault(node.op, []).append(node) self.setdefault(node.op, []).append(node)
except TypeError: #node.op is unhashable except TypeError: # node.op is unhashable
return return
except Exception, e: except Exception, e:
print >> sys.stderr, 'OFFENDING node', type(node), type(node.op) print >> sys.stderr, 'OFFENDING node', type(node), type(node.op)
...@@ -150,7 +163,7 @@ class NodeFinder(dict, Bookkeeper): ...@@ -150,7 +163,7 @@ class NodeFinder(dict, Bookkeeper):
def on_prune(self, env, node): def on_prune(self, env, node):
try: try:
nodes = self[node.op] nodes = self[node.op]
except TypeError: #node.op is unhashable except TypeError: # node.op is unhashable
return return
nodes.remove(node) nodes.remove(node)
if not nodes: if not nodes:
...@@ -160,16 +173,17 @@ class NodeFinder(dict, Bookkeeper): ...@@ -160,16 +173,17 @@ class NodeFinder(dict, Bookkeeper):
try: try:
all = self.get(op, []) all = self.get(op, [])
except TypeError: except TypeError:
raise TypeError("%s in unhashable and cannot be queried by the optimizer" % op) raise TypeError("%s in unhashable and cannot be queried by the"
" optimizer" % op)
all = list(all) all = list(all)
return all return all
class PrintListener(object): class PrintListener(object):
def __init__(self, active = True): def __init__(self, active=True):
self.active = active self.active = active
def on_attach(self, env): def on_attach(self, env):
if self.active: if self.active:
print "-- attaching to: ", env print "-- attaching to: ", env
...@@ -188,7 +202,8 @@ class PrintListener(object): ...@@ -188,7 +202,8 @@ class PrintListener(object):
def on_change_input(self, env, node, i, r, new_r, reason=None): def on_change_input(self, env, node, i, r, new_r, reason=None):
if self.active: if self.active:
print "-- changing (%s.inputs[%s]) from %s to %s" % (node, i, r, new_r) print "-- changing (%s.inputs[%s]) from %s to %s" % (
node, i, r, new_r)
class PreserveNames: class PreserveNames:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论