提交 3c052bff authored 作者: James Bergstra's avatar James Bergstra

FIX: remove circular dependency in gof import

上级 28e9f9fe
......@@ -70,6 +70,7 @@ from optdb import \
EquilibriumDB, SequenceDB, ProxyDB
from toolbox import \
Feature, \
Bookkeeper, History, Validator, ReplaceValidate, NodeFinder,\
PrintListener, ReplacementDidntRemovedError
......
......@@ -27,59 +27,6 @@ class MissingInputError(Exception):
pass
class Feature(object):
"""
Base class for FunctionGraph extensions.
See toolbox and ext modules for common extensions.
"""
def on_attach(self, function_graph):
"""
Called by extend. The feature has great freedom in what
it can do with the function_graph: it may, for example, add methods
to it dynamically.
"""
def on_detach(self, function_graph):
"""
Called by remove_feature(feature). Should remove any dynamically-added
functionality that it installed into the function_graph.
"""
def on_import(self, function_graph, node):
"""
Called whenever a node is imported into function_graph, which is
just before the node is actually connected to the graph.
"""
def on_prune(self, function_graph, node):
"""
Called whenever a node is pruned (removed) from the function_graph,
after it is disconnected from the graph.
"""
def on_change_input(self, function_graph, node, i, r, new_r, reason=None):
"""
Called whenever node.inputs[i] is changed from r to new_r.
At the moment the callback is done, the change has already
taken place.
If you raise an exception in this function, the state of the graph
might be broken for all intents and purposes.
"""
def orderings(self, function_graph):
"""
Called by toposort. It should return a dictionary of
{node: predecessors} where predecessors is a list of
nodes that should be computed before the key node.
If you raise an exception in this function, the state of the graph
might be broken for all intents and purposes.
"""
class FunctionGraph(utils.object2):
""" WRITEME
A FunctionGraph represents a subgraph bound by a set of input variables and a
......@@ -100,7 +47,7 @@ class FunctionGraph(utils.object2):
.inputs field allows the graph to be traversed in both directions.
It can also be "extended" using function_graph.extend(some_object).
See Feature above for event types and documentation.
See toolbox.Feature for event types and documentation.
Historically, the FunctionGraph was called an Env. Keep this in mind
while reading out-of-date documentation, e-mail support threads, etc.
......
......@@ -19,7 +19,61 @@ class ReplacementDidntRemovedError(Exception):
pass
class Bookkeeper:
class Feature(object):
"""
Base class for FunctionGraph extensions.
See toolbox and ext modules for common extensions.
"""
def on_attach(self, function_graph):
"""
Called by extend. The feature has great freedom in what
it can do with the function_graph: it may, for example, add methods
to it dynamically.
"""
def on_detach(self, function_graph):
"""
Called by remove_feature(feature). Should remove any dynamically-added
functionality that it installed into the function_graph.
"""
def on_import(self, function_graph, node):
"""
Called whenever a node is imported into function_graph, which is
just before the node is actually connected to the graph.
"""
def on_prune(self, function_graph, node):
"""
Called whenever a node is pruned (removed) from the function_graph,
after it is disconnected from the graph.
"""
def on_change_input(self, function_graph, node, i, r, new_r, reason=None):
"""
Called whenever node.inputs[i] is changed from r to new_r.
At the moment the callback is done, the change has already
taken place.
If you raise an exception in this function, the state of the graph
might be broken for all intents and purposes.
"""
def orderings(self, function_graph):
"""
Called by toposort. It should return a dictionary of
{node: predecessors} where predecessors is a list of
nodes that should be computed before the key node.
If you raise an exception in this function, the state of the graph
might be broken for all intents and purposes.
"""
return {}
class Bookkeeper(Feature):
def on_attach(self, fgraph):
for node in graph.io_toposort(fgraph.inputs, fgraph.outputs):
......@@ -30,7 +84,7 @@ class Bookkeeper:
self.on_prune(fgraph, node)
class History:
class History(Feature):
def __init__(self):
self.history = {}
......@@ -69,7 +123,7 @@ class History:
self.history[fgraph] = h
class Validator:
class Validator(Feature):
def on_attach(self, fgraph):
for attr in ('validate', 'validate_time'):
......@@ -224,7 +278,7 @@ class NodeFinder(dict, Bookkeeper):
return all
class PrintListener(object):
class PrintListener(Feature):
def __init__(self, active=True):
self.active = active
......@@ -251,7 +305,9 @@ class PrintListener(object):
node, i, r, new_r)
class PreserveNames:
class PreserveNames(Feature):
def on_change_input(self, fgraph, mode, i, r, new_r, reason=None):
if r.name is not None and new_r.name is None:
new_r.name = r.name
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论