API Reference for Enthought Tool Suite 2.7.1
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.
>>> from compiler import parse
>>> assert dependency_graph(to_ast=parse, asts=[
... 'a = 1', 'a = 2'
... ]) == {
... 'a = 1' : [],
... 'a = 2' : [],
... }
A topological sort of the above graph could produce either ['a = 1', 'a = 2'] or ['a = 2', 'a = 1'].
Some examples:
>>> from compiler import parse
>>> import enthought.util.graph as graph
>>>
>>> assert graph.eq(dependency_graph(to_ast=parse, asts=[
... 'a = b+c', 'c = 3', 'b = f(c)'
... ]), {
... 'a = b+c' : ['c = 3', 'b = f(c)'],
... 'c = 3' : [],
... 'b = f(c)' : ['c = 3'],
... })
>>> assert graph.eq(dependency_graph(to_ast=parse, asts=[
... 'a = 1', 'print a', 'a = 2'
... ]), {
... 'a = 1' : [],
... 'print a' : ['a = 1', 'a = 2'],
... 'a = 2' : [],
... })
>>> assert graph.eq(dependency_graph(to_ast=parse, asts=[
... 'a = 1', 'a = 0; print a', 'a = 2'
... ]), {
... 'a = 1' : [],
... 'a = 0; print a' : [],
... 'a = 2' : [],
... })
Sorting non-linear dependency graphs is useful (but non-deterministic):
>>> from enthought.util.graph import reverse, topological_sort >>> >>> assert topological_sort(reverse( ... dependency_graph(to_ast=parse, asts=[ ... 'c = h(a,b)', ... 'a = f(z)', ... 'b = g(z,y)', ... 'd = k(b,c)', ... ]) ... )) 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 import parse
>>> 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:
>>> from compiler import parse
>>> 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.graph.closure |
| compiler | compiler |
| compiler.ast | compiler.ast |
| Const | compiler.ast.Const |
| copy | copy.copy |
| deepcopy | copy.deepcopy |
| Dict | compiler.ast.Dict |
| disjoint | enthought.util.sequence.disjoint |
| enthought.numerical_modeling.workflow.block.compiler_.ast.get_children_tree | enthought.numerical_modeling.workflow.block.compiler_.ast.get_children_tree |
| eval_ast | enthought.numerical_modeling.workflow.block.compiler_.compiler_.eval_ast |
| Expression | compiler.ast.Expression |
| Getattr | compiler.ast.Getattr |
| graph | enthought.util.graph |
| intersect | enthought.util.sequence.intersect |
| 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 |
| Name | compiler.ast.Name |
| Node | compiler.ast.Node |
| partial | enthought.util.functional.partial |
| topological_sort | enthought.util.graph.topological_sort |
| tree | enthought.util.tree |
| Tuple | compiler.ast.Tuple |
| union | enthought.util.sequence.union |
Copyright © 2002-2008 Enthought, Inc.