API Reference for Enthought Tool Suite 3.2.0
logger = logging.getLogger(__name__)
Compute the dependency graph for a set of ASTs.
'asts' is a sequence of either ASTs or objects 'x' such that 'to_ast(x)' is an AST. The returned value is the dependency graph: a directed (acyclic) graph relating the elements of 'asts' such that a->b iff a's AST uses names created by b's AST. We assume 'to_ast' is injective and elements of 'asts' are hashable. Raises a CyclicGraph exception when the output graph would have been cyclic.
If a name is created by multiple elements of 'asts', then the dependency graph won't determine a well-defined program. e.g.
>>> g = dependency_graph(to_ast=parse, asts=[ ... 'a = 1', 'a = 2' ... ]) >>> sorted( g.nodes() ) ['a = 1', 'a = 2'] >>> sorted( g.edges() ) []
A topological sort of the above graph could produce either ['a = 1', 'a = 2'] or ['a = 2', 'a = 1'].
Some examples:
>>> assert sorted( dependency_graph(to_ast=parse, asts=[ ... 'a = b+c', 'c = 3', 'b = f(c)' ... ]).edges() ) == [ ... ('a = b+c', 'b = f(c)'), ... ('a = b+c', 'c = 3'), ... ('b = f(c)', 'c = 3'), ... ]>>> assert sorted( dependency_graph(to_ast=parse, asts=[ ... 'a = 1', 'print a', 'a = 2' ... ]).edges() ) == [ ... ('print a', 'a = 1'), ... ('print a', 'a = 2'), ... ]>>> assert sorted( dependency_graph(to_ast=parse, asts=[ ... 'a = 1', 'a = 0; print a', 'a = 2' ... ]).edges() ) == [ ... ]>>> try: ... dependency_graph(to_ast=parse, asts=[ ... 'a = b', 'b = a' ... ]) ... except CyclicGraph: ... print 'Cyclic graph!' Cyclic graph!>>> try: ... dependency_graph(to_ast=parse, asts=[ ... 'a = a', ... ]) ... except CyclicGraph: ... print 'Cyclic graph!' Cyclic graph!
Sorting non-linear dependency graphs is useful (but non-deterministic):
>>> assert topological_sort( ... dependency_graph(to_ast=parse, asts=[ ... 'c = h(a,b)', ... 'a = f(z)', ... 'b = g(z,y)', ... 'd = k(b,c)', ... ]).reverse() ... ) in [ ... [ 'a = f(z)', 'b = g(z,y)', 'c = h(a,b)', 'd = k(b,c)' ], ... [ 'b = g(z,y)', 'a = f(z)', 'c = h(a,b)', 'd = k(b,c)' ], ... ]
Transform an AST by extracting its constant assignment statements.
>>> from compiler_.ast.api import similar
>>> ast, const_for = extract_const_assigns(parse('a = 0'))
>>> similar(ast, parse('a = __a'))
True
>>> const_for
{'__a': 0}
Whether an AST represents a constant expression.
I'm not sure what "constant" means yet, but here are some examples:
>>> all(is_const(parse(s, mode='eval')) for s in (
... '0',
... 'True',
... 'None',
... '"foo"',
... '[1,2]',
... '(False, [])',
... '{"a": 1}',
... '{"a": 0, (True, False): [None, 3, "fish", ()]}',
... ))
True
And some non-examples, some of which maybe should be reclassified:
>>> any(is_const(parse(s, mode='eval')) for s in (
... '0+1',
... '~8',
... '0 < 1',
... 'not True',
... '"fo%s" % "o"',
... 'len([1,2])',
... '[1,2][0]',
... '[1,a]',
... '[a for a in [1,2]]',
... '{"a": 0}.keys()',
... 'set()',
... 'list()',
... 'dict()',
... 'dict',
... 'lambda: 3',
... 'lambda a: a',
... ))
False
Wrap compiler.walk to handle 'None' and sequences.
| Local name | Refers to |
|---|---|
| all | enthought.util.sequence.all |
| any | enthought.util.sequence.any |
| AssAttr | compiler.ast.AssAttr |
| Assign | compiler.ast.Assign |
| AssName | compiler.ast.AssName |
| closure | enthought.util.networkx_.closure |
| compiler | compiler |
| compiler.ast | compiler.ast |
| Const | compiler.ast.Const |
| copy | copy.copy |
| CyclicGraph | enthought.util.networkx_.CyclicGraph |
| deepcopy | copy.deepcopy |
| Dict | compiler.ast.Dict |
| DiGraph | networkx.DiGraph |
| disjoint | enthought.util.sequence.disjoint |
| enthought.blocks.compiler_.ast.get_children_tree | enthought.blocks.compiler_.ast.get_children_tree |
| eval_ast | enthought.numerical_modeling.workflow.block_experiment_networkx.compiler_.compiler_.eval_ast |
| Expression | compiler.ast.Expression |
| Getattr | compiler.ast.Getattr |
| graph_map | enthought.util.networkx_.graph_map |
| intersect | enthought.util.sequence.intersect |
| is_directed_acyclic_graph | networkx.is_directed_acyclic_graph |
| is_sequence | enthought.util.sequence.is_sequence |
| List | compiler.ast.List |
| logging | logging |
| magically_bound_names | enthought.numerical_modeling.name_magic.magically_bound_names |
| map_items | enthought.util.dict.map_items |
| map_keys | enthought.util.dict.map_keys |
| map_values | enthought.util.dict.map_values |
| Name | compiler.ast.Name |
| Node | compiler.ast.Node |
| parse | compiler.parse |
| partial | enthought.util.functional.partial |
| topological_sort | networkx.topological_sort |
| tree | enthought.util.tree |
| Tuple | compiler.ast.Tuple |
| union | enthought.util.sequence.union |
© Copyright 2002-2009 Enthought, Inc.