Most viewed recipes tagged "ast"http://code.activestate.com/recipes/tags/ast/views/2015-02-05T20:18:08-08:00ActiveState Code RecipesNested contexts -- a chain of mapping objects (Python)
2010-10-25T02:13:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577434-nested-contexts-a-chain-of-mapping-objects/
<p style="color: grey">
Python
recipe 577434
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/chained/">chained</a>, <a href="/recipes/tags/compiler/">compiler</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/scopes/">scopes</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>Easy to use chain of dictionaries for crafting nested scopes or for a tree of scopes. Useful for analyzing AST nodes, XML nodes or other structures with multiple scopes. Can emulate various chaining styles including static/lexical scoping, dynamic scoping and Python's own globals(), locals(), nested scopes, and writeable nonlocals. Can also model Python's inheritance chains: instance dictionary, class dictionary, and base classes.</p>
Code to source and back (Python)
2012-12-01T08:59:49-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578353-code-to-source-and-back/
<p style="color: grey">
Python
recipe 578353
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/decompile/">decompile</a>, <a href="/recipes/tags/recompile/">recompile</a>, <a href="/recipes/tags/uncompile/">uncompile</a>).
</p>
<p>Converts a code object to a source code snippet and back: <code>c == recompile(*uncompile(c))</code></p>
<p>This is useful, for example, if you want to apply an AST transformation to the code.</p>
Parse call function for Py2.6 and Py2.7 (Python)
2009-02-28T20:13:15-08:00Jervis Whitleyhttp://code.activestate.com/recipes/users/4169341/http://code.activestate.com/recipes/576671-parse-call-function-for-py26-and-py27/
<p style="color: grey">
Python
recipe 576671
by <a href="/recipes/users/4169341/">Jervis Whitley</a>
(<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/call/">call</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/nodevisitor/">nodevisitor</a>, <a href="/recipes/tags/parsing/">parsing</a>).
Revision 14.
</p>
<p>In some cases it may be desirable to parse the string expression "f1(*args)"
and return some of the key features of the represented function-like call. </p>
<p>This recipe returns the key features in the form of a namedtuple. </p>
<p>e.g. (for the above)</p>
<pre class="prettyprint"><code>>>> explain("f1(*args)")
[ Call(func='f1', starargs='args') ]
</code></pre>
<p>The recipe will return a list of such namedtuples for <code>"f1(*args)\nf2(*args)"</code>
Note that while the passed string expression must evaluate to valid python syntax,
names needn't be declared in current scope.</p>
Python AST to XML (Python)
2015-02-05T20:18:08-08:00Robert Stewarthttp://code.activestate.com/recipes/users/4191612/http://code.activestate.com/recipes/579019-python-ast-to-xml/
<p style="color: grey">
Python
recipe 579019
by <a href="/recipes/users/4191612/">Robert Stewart</a>
(<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>Convert Python ASTs to XML files for reading in other languages.</p>
Python AST to XML (Python)
2013-08-07T00:42:35-07:00Ryan Gonzalezhttp://code.activestate.com/recipes/users/4187447/http://code.activestate.com/recipes/578625-python-ast-to-xml/
<p style="color: grey">
Python
recipe 578625
by <a href="/recipes/users/4187447/">Ryan Gonzalez</a>
(<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>Converts Python ASTs to XML files for reading in other languages.</p>