| <?xml version="1.0" encoding="ascii"?> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| "DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| <head> |
| <title>TestON.core.ast</title> |
| <link rel="stylesheet" href="epydoc.css" type="text/css" /> |
| <script type="text/javascript" src="epydoc.js"></script> |
| </head> |
| |
| <body bgcolor="white" text="black" link="blue" vlink="#204080" |
| alink="#204080"> |
| <!-- ==================== NAVIGATION BAR ==================== --> |
| <table class="navbar" border="0" width="100%" cellpadding="0" |
| bgcolor="#a0c0ff" cellspacing="0"> |
| <tr valign="middle"> |
| <!-- Home link --> |
| <th> <a |
| href="TestON-module.html">Home</a> </th> |
| |
| <!-- Tree link --> |
| <th> <a |
| href="module-tree.html">Trees</a> </th> |
| |
| <!-- Index link --> |
| <th> <a |
| href="identifier-index.html">Indices</a> </th> |
| |
| <!-- Help link --> |
| <th> <a |
| href="help.html">Help</a> </th> |
| |
| <th class="navbar" width="100%"></th> |
| </tr> |
| </table> |
| <table width="100%" cellpadding="0" cellspacing="0"> |
| <tr valign="top"> |
| <td width="100%"> |
| <span class="breadcrumbs"> |
| <a href="TestON-module.html">Package TestON</a> :: |
| <a href="TestON.core-module.html">Package core</a> :: |
| Module ast |
| </span> |
| </td> |
| <td> |
| <table cellpadding="0" cellspacing="0"> |
| <!-- hide/show private --> |
| <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink" |
| onclick="toggle_private();">hide private</a>]</span></td></tr> |
| <tr><td align="right"><span class="options" |
| >[<a href="frames.html" target="_top">frames</a |
| >] | <a href="TestON.core.ast-pysrc.html" |
| target="_top">no frames</a>]</span></td></tr> |
| </table> |
| </td> |
| </tr> |
| </table> |
| <h1 class="epydoc">Source Code for <a href="TestON.core.ast-module.html">Module TestON.core.ast</a></h1> |
| <pre class="py-src"> |
| <a name="L1"></a><tt class="py-lineno"> 1</tt> <tt class="py-line"><tt class="py-comment"># -*- coding: utf-8 -*-</tt> </tt> |
| <a name="L2"></a><tt class="py-lineno"> 2</tt> <tt class="py-line"><tt class="py-docstring">"""</tt> </tt> |
| <a name="L3"></a><tt class="py-lineno"> 3</tt> <tt class="py-line"><tt class="py-docstring"> ast</tt> </tt> |
| <a name="L4"></a><tt class="py-lineno"> 4</tt> <tt class="py-line"><tt class="py-docstring"> ~~~</tt> </tt> |
| <a name="L5"></a><tt class="py-lineno"> 5</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L6"></a><tt class="py-lineno"> 6</tt> <tt class="py-line"><tt class="py-docstring"> The `ast` module helps Python applications to process trees of the Python</tt> </tt> |
| <a name="L7"></a><tt class="py-lineno"> 7</tt> <tt class="py-line"><tt class="py-docstring"> abstract syntax grammar. The abstract syntax itself might change with</tt> </tt> |
| <a name="L8"></a><tt class="py-lineno"> 8</tt> <tt class="py-line"><tt class="py-docstring"> each Python release; this module helps to find out programmatically what</tt> </tt> |
| <a name="L9"></a><tt class="py-lineno"> 9</tt> <tt class="py-line"><tt class="py-docstring"> the current grammar looks like and allows modifications of it.</tt> </tt> |
| <a name="L10"></a><tt class="py-lineno"> 10</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L11"></a><tt class="py-lineno"> 11</tt> <tt class="py-line"><tt class="py-docstring"> An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as</tt> </tt> |
| <a name="L12"></a><tt class="py-lineno"> 12</tt> <tt class="py-line"><tt class="py-docstring"> a flag to the `compile()` builtin function or by using the `parse()`</tt> </tt> |
| <a name="L13"></a><tt class="py-lineno"> 13</tt> <tt class="py-line"><tt class="py-docstring"> function from this module. The result will be a tree of objects whose</tt> </tt> |
| <a name="L14"></a><tt class="py-lineno"> 14</tt> <tt class="py-line"><tt class="py-docstring"> classes all inherit from `ast.AST`.</tt> </tt> |
| <a name="L15"></a><tt class="py-lineno"> 15</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L16"></a><tt class="py-lineno"> 16</tt> <tt class="py-line"><tt class="py-docstring"> A modified abstract syntax tree can be compiled into a Python code object</tt> </tt> |
| <a name="L17"></a><tt class="py-lineno"> 17</tt> <tt class="py-line"><tt class="py-docstring"> using the built-in `compile()` function.</tt> </tt> |
| <a name="L18"></a><tt class="py-lineno"> 18</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L19"></a><tt class="py-lineno"> 19</tt> <tt class="py-line"><tt class="py-docstring"> Additionally various helper functions are provided that make working with</tt> </tt> |
| <a name="L20"></a><tt class="py-lineno"> 20</tt> <tt class="py-line"><tt class="py-docstring"> the trees simpler. The main intention of the helper functions and this</tt> </tt> |
| <a name="L21"></a><tt class="py-lineno"> 21</tt> <tt class="py-line"><tt class="py-docstring"> module in general is to provide an easy to use interface for libraries</tt> </tt> |
| <a name="L22"></a><tt class="py-lineno"> 22</tt> <tt class="py-line"><tt class="py-docstring"> that work tightly with the python syntax (template engines for example).</tt> </tt> |
| <a name="L23"></a><tt class="py-lineno"> 23</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L24"></a><tt class="py-lineno"> 24</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L25"></a><tt class="py-lineno"> 25</tt> <tt class="py-line"><tt class="py-docstring"> :copyright: Copyright 2008 by Armin Ronacher.</tt> </tt> |
| <a name="L26"></a><tt class="py-lineno"> 26</tt> <tt class="py-line"><tt class="py-docstring"> :license: Python License.</tt> </tt> |
| <a name="L27"></a><tt class="py-lineno"> 27</tt> <tt class="py-line"><tt class="py-docstring">"""</tt> </tt> |
| <a name="L28"></a><tt class="py-lineno"> 28</tt> <tt class="py-line"><tt class="py-keyword">from</tt> <tt class="py-name">_ast</tt> <tt class="py-keyword">import</tt> <tt class="py-op">*</tt> </tt> |
| <a name="L29"></a><tt class="py-lineno"> 29</tt> <tt class="py-line"><tt class="py-keyword">from</tt> <tt class="py-name">_ast</tt> <tt class="py-keyword">import</tt> <tt class="py-name">__version__</tt> </tt> |
| <a name="L30"></a><tt class="py-lineno"> 30</tt> <tt class="py-line"> </tt> |
| <a name="L31"></a><tt class="py-lineno"> 31</tt> <tt class="py-line"> </tt> |
| <a name="parse"></a><div id="parse-def"><a name="L32"></a><tt class="py-lineno"> 32</tt> <a class="py-toggle" href="#" id="parse-toggle" onclick="return toggle('parse');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#parse">parse</a><tt class="py-op">(</tt><tt class="py-param">source</tt><tt class="py-op">,</tt> <tt class="py-param">filename</tt><tt class="py-op">=</tt><tt class="py-string">'<unknown>'</tt><tt class="py-op">,</tt> <tt class="py-param">mode</tt><tt class="py-op">=</tt><tt class="py-string">'exec'</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="parse-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="parse-expanded"><a name="L33"></a><tt class="py-lineno"> 33</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L34"></a><tt class="py-lineno"> 34</tt> <tt class="py-line"><tt class="py-docstring"> Parse the source into an AST node.</tt> </tt> |
| <a name="L35"></a><tt class="py-lineno"> 35</tt> <tt class="py-line"><tt class="py-docstring"> Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).</tt> </tt> |
| <a name="L36"></a><tt class="py-lineno"> 36</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L37"></a><tt class="py-lineno"> 37</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">compile</tt><tt class="py-op">(</tt><tt class="py-name">source</tt><tt class="py-op">,</tt> <tt class="py-name">filename</tt><tt class="py-op">,</tt> <tt class="py-name">mode</tt><tt class="py-op">,</tt> <tt class="py-name">PyCF_ONLY_AST</tt><tt class="py-op">)</tt> </tt> |
| </div><a name="L38"></a><tt class="py-lineno"> 38</tt> <tt class="py-line"> </tt> |
| <a name="L39"></a><tt class="py-lineno"> 39</tt> <tt class="py-line"> </tt> |
| <a name="literal_eval"></a><div id="literal_eval-def"><a name="L40"></a><tt class="py-lineno"> 40</tt> <a class="py-toggle" href="#" id="literal_eval-toggle" onclick="return toggle('literal_eval');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#literal_eval">literal_eval</a><tt class="py-op">(</tt><tt class="py-param">node_or_string</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="literal_eval-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="literal_eval-expanded"><a name="L41"></a><tt class="py-lineno"> 41</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L42"></a><tt class="py-lineno"> 42</tt> <tt class="py-line"><tt class="py-docstring"> Safely evaluate an expression node or a string containing a Python</tt> </tt> |
| <a name="L43"></a><tt class="py-lineno"> 43</tt> <tt class="py-line"><tt class="py-docstring"> expression. The string or node provided may only consist of the following</tt> </tt> |
| <a name="L44"></a><tt class="py-lineno"> 44</tt> <tt class="py-line"><tt class="py-docstring"> Python literal structures: strings, numbers, tuples, lists, dicts, booleans,</tt> </tt> |
| <a name="L45"></a><tt class="py-lineno"> 45</tt> <tt class="py-line"><tt class="py-docstring"> and None.</tt> </tt> |
| <a name="L46"></a><tt class="py-lineno"> 46</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L47"></a><tt class="py-lineno"> 47</tt> <tt class="py-line"> <tt class="py-name">_safe_names</tt> <tt class="py-op">=</tt> <tt class="py-op">{</tt><tt class="py-string">'None'</tt><tt class="py-op">:</tt> <tt class="py-name">None</tt><tt class="py-op">,</tt> <tt class="py-string">'True'</tt><tt class="py-op">:</tt> <tt class="py-name">True</tt><tt class="py-op">,</tt> <tt class="py-string">'False'</tt><tt class="py-op">:</tt> <tt class="py-name">False</tt><tt class="py-op">}</tt> </tt> |
| <a name="L48"></a><tt class="py-lineno"> 48</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node_or_string</tt><tt class="py-op">,</tt> <tt class="py-name">basestring</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L49"></a><tt class="py-lineno"> 49</tt> <tt class="py-line"> <tt class="py-name">node_or_string</tt> <tt class="py-op">=</tt> <tt id="link-0" class="py-name" targets="Function TestON.core.ast.parse()=TestON.core.ast-module.html#parse,Method TestON.core.iniparser.iniparser.parse()=TestON.core.iniparser.iniparser-class.html#parse,Method TestON.core.xmlparser.xmlparser.parse()=TestON.core.xmlparser.xmlparser-class.html#parse"><a title="TestON.core.ast.parse |
| TestON.core.iniparser.iniparser.parse |
| TestON.core.xmlparser.xmlparser.parse" class="py-name" href="#" onclick="return doclink('link-0', 'parse', 'link-0');">parse</a></tt><tt class="py-op">(</tt><tt class="py-name">node_or_string</tt><tt class="py-op">,</tt> <tt class="py-name">mode</tt><tt class="py-op">=</tt><tt class="py-string">'eval'</tt><tt class="py-op">)</tt> </tt> |
| <a name="L50"></a><tt class="py-lineno"> 50</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node_or_string</tt><tt class="py-op">,</tt> <tt class="py-name">Expression</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L51"></a><tt class="py-lineno"> 51</tt> <tt class="py-line"> <tt class="py-name">node_or_string</tt> <tt class="py-op">=</tt> <tt class="py-name">node_or_string</tt><tt class="py-op">.</tt><tt class="py-name">body</tt> </tt> |
| <a name="L52"></a><tt class="py-lineno"> 52</tt> <tt class="py-line"> <tt class="py-keyword">def</tt> <tt class="py-def-name">_convert</tt><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L53"></a><tt class="py-lineno"> 53</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">Str</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L54"></a><tt class="py-lineno"> 54</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">s</tt> </tt> |
| <a name="L55"></a><tt class="py-lineno"> 55</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">Num</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L56"></a><tt class="py-lineno"> 56</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">n</tt> </tt> |
| <a name="L57"></a><tt class="py-lineno"> 57</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">Tuple</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L58"></a><tt class="py-lineno"> 58</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">tuple</tt><tt class="py-op">(</tt><tt class="py-name">map</tt><tt class="py-op">(</tt><tt class="py-name">_convert</tt><tt class="py-op">,</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">elts</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt> |
| <a name="L59"></a><tt class="py-lineno"> 59</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">List</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L60"></a><tt class="py-lineno"> 60</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">list</tt><tt class="py-op">(</tt><tt class="py-name">map</tt><tt class="py-op">(</tt><tt class="py-name">_convert</tt><tt class="py-op">,</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">elts</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt> |
| <a name="L61"></a><tt class="py-lineno"> 61</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">Dict</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L62"></a><tt class="py-lineno"> 62</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">dict</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">_convert</tt><tt class="py-op">(</tt><tt class="py-name">k</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">_convert</tt><tt class="py-op">(</tt><tt class="py-name">v</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> <tt class="py-keyword">for</tt> <tt class="py-name">k</tt><tt class="py-op">,</tt> <tt class="py-name">v</tt> </tt> |
| <a name="L63"></a><tt class="py-lineno"> 63</tt> <tt class="py-line"> <tt class="py-keyword">in</tt> <tt class="py-name">zip</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">keys</tt><tt class="py-op">,</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">values</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt> |
| <a name="L64"></a><tt class="py-lineno"> 64</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">Name</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L65"></a><tt class="py-lineno"> 65</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">id</tt> <tt class="py-keyword">in</tt> <tt class="py-name">_safe_names</tt><tt class="py-op">:</tt> </tt> |
| <a name="L66"></a><tt class="py-lineno"> 66</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">_safe_names</tt><tt class="py-op">[</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">id</tt><tt class="py-op">]</tt> </tt> |
| <a name="L67"></a><tt class="py-lineno"> 67</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">BinOp</tt><tt class="py-op">)</tt> <tt class="py-keyword">and</tt> \ </tt> |
| <a name="L68"></a><tt class="py-lineno"> 68</tt> <tt class="py-line"> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">op</tt><tt class="py-op">,</tt> <tt class="py-op">(</tt><tt class="py-name">Add</tt><tt class="py-op">,</tt> <tt class="py-name">Sub</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> <tt class="py-keyword">and</tt> \ </tt> |
| <a name="L69"></a><tt class="py-lineno"> 69</tt> <tt class="py-line"> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">right</tt><tt class="py-op">,</tt> <tt class="py-name">Num</tt><tt class="py-op">)</tt> <tt class="py-keyword">and</tt> \ </tt> |
| <a name="L70"></a><tt class="py-lineno"> 70</tt> <tt class="py-line"> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">right</tt><tt class="py-op">.</tt><tt class="py-name">n</tt><tt class="py-op">,</tt> <tt class="py-name">complex</tt><tt class="py-op">)</tt> <tt class="py-keyword">and</tt> \ </tt> |
| <a name="L71"></a><tt class="py-lineno"> 71</tt> <tt class="py-line"> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">left</tt><tt class="py-op">,</tt> <tt class="py-name">Num</tt><tt class="py-op">)</tt> <tt class="py-keyword">and</tt> \ </tt> |
| <a name="L72"></a><tt class="py-lineno"> 72</tt> <tt class="py-line"> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">left</tt><tt class="py-op">.</tt><tt class="py-name">n</tt><tt class="py-op">,</tt> <tt class="py-op">(</tt><tt class="py-name">int</tt><tt class="py-op">,</tt> <tt class="py-name">long</tt><tt class="py-op">,</tt> <tt class="py-name">float</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L73"></a><tt class="py-lineno"> 73</tt> <tt class="py-line"> <tt class="py-name">left</tt> <tt class="py-op">=</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">left</tt><tt class="py-op">.</tt><tt class="py-name">n</tt> </tt> |
| <a name="L74"></a><tt class="py-lineno"> 74</tt> <tt class="py-line"> <tt class="py-name">right</tt> <tt class="py-op">=</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">right</tt><tt class="py-op">.</tt><tt class="py-name">n</tt> </tt> |
| <a name="L75"></a><tt class="py-lineno"> 75</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">op</tt><tt class="py-op">,</tt> <tt class="py-name">Add</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L76"></a><tt class="py-lineno"> 76</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">left</tt> <tt class="py-op">+</tt> <tt class="py-name">right</tt> </tt> |
| <a name="L77"></a><tt class="py-lineno"> 77</tt> <tt class="py-line"> <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt> |
| <a name="L78"></a><tt class="py-lineno"> 78</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">left</tt> <tt class="py-op">-</tt> <tt class="py-name">right</tt> </tt> |
| <a name="L79"></a><tt class="py-lineno"> 79</tt> <tt class="py-line"> <tt class="py-keyword">raise</tt> <tt class="py-name">ValueError</tt><tt class="py-op">(</tt><tt class="py-string">'malformed string'</tt><tt class="py-op">)</tt> </tt> |
| </div><a name="L80"></a><tt class="py-lineno"> 80</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">_convert</tt><tt class="py-op">(</tt><tt class="py-name">node_or_string</tt><tt class="py-op">)</tt> </tt> |
| </div><a name="L81"></a><tt class="py-lineno"> 81</tt> <tt class="py-line"> </tt> |
| <a name="L82"></a><tt class="py-lineno"> 82</tt> <tt class="py-line"> </tt> |
| <a name="dump"></a><div id="dump-def"><a name="L83"></a><tt class="py-lineno"> 83</tt> <a class="py-toggle" href="#" id="dump-toggle" onclick="return toggle('dump');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#dump">dump</a><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">,</tt> <tt class="py-param">annotate_fields</tt><tt class="py-op">=</tt><tt class="py-name">True</tt><tt class="py-op">,</tt> <tt class="py-param">include_attributes</tt><tt class="py-op">=</tt><tt class="py-name">False</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="dump-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="dump-expanded"><a name="L84"></a><tt class="py-lineno"> 84</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L85"></a><tt class="py-lineno"> 85</tt> <tt class="py-line"><tt class="py-docstring"> Return a formatted dump of the tree in *node*. This is mainly useful for</tt> </tt> |
| <a name="L86"></a><tt class="py-lineno"> 86</tt> <tt class="py-line"><tt class="py-docstring"> debugging purposes. The returned string will show the names and the values</tt> </tt> |
| <a name="L87"></a><tt class="py-lineno"> 87</tt> <tt class="py-line"><tt class="py-docstring"> for fields. This makes the code impossible to evaluate, so if evaluation is</tt> </tt> |
| <a name="L88"></a><tt class="py-lineno"> 88</tt> <tt class="py-line"><tt class="py-docstring"> wanted *annotate_fields* must be set to False. Attributes such as line</tt> </tt> |
| <a name="L89"></a><tt class="py-lineno"> 89</tt> <tt class="py-line"><tt class="py-docstring"> numbers and column offsets are not dumped by default. If this is wanted,</tt> </tt> |
| <a name="L90"></a><tt class="py-lineno"> 90</tt> <tt class="py-line"><tt class="py-docstring"> *include_attributes* can be set to True.</tt> </tt> |
| <a name="L91"></a><tt class="py-lineno"> 91</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L92"></a><tt class="py-lineno"> 92</tt> <tt class="py-line"> <tt class="py-keyword">def</tt> <tt class="py-def-name">_format</tt><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L93"></a><tt class="py-lineno"> 93</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L94"></a><tt class="py-lineno"> 94</tt> <tt class="py-line"> <tt class="py-name">fields</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">(</tt><tt class="py-name">a</tt><tt class="py-op">,</tt> <tt class="py-name">_format</tt><tt class="py-op">(</tt><tt class="py-name">b</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> <tt class="py-keyword">for</tt> <tt class="py-name">a</tt><tt class="py-op">,</tt> <tt class="py-name">b</tt> <tt class="py-keyword">in</tt> <tt id="link-1" class="py-name" targets="Function TestON.core.ast.iter_fields()=TestON.core.ast-module.html#iter_fields"><a title="TestON.core.ast.iter_fields" class="py-name" href="#" onclick="return doclink('link-1', 'iter_fields', 'link-1');">iter_fields</a></tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt><tt class="py-op">]</tt> </tt> |
| <a name="L95"></a><tt class="py-lineno"> 95</tt> <tt class="py-line"> <tt class="py-name">rv</tt> <tt class="py-op">=</tt> <tt class="py-string">'%s(%s'</tt> <tt class="py-op">%</tt> <tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">__class__</tt><tt class="py-op">.</tt><tt class="py-name">__name__</tt><tt class="py-op">,</tt> <tt class="py-string">', '</tt><tt class="py-op">.</tt><tt class="py-name">join</tt><tt class="py-op">(</tt> </tt> |
| <a name="L96"></a><tt class="py-lineno"> 96</tt> <tt class="py-line"> <tt class="py-op">(</tt><tt class="py-string">'%s=%s'</tt> <tt class="py-op">%</tt> <tt class="py-name">field</tt> <tt class="py-keyword">for</tt> <tt class="py-name">field</tt> <tt class="py-keyword">in</tt> <tt class="py-name">fields</tt><tt class="py-op">)</tt> </tt> |
| <a name="L97"></a><tt class="py-lineno"> 97</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">annotate_fields</tt> <tt class="py-keyword">else</tt> </tt> |
| <a name="L98"></a><tt class="py-lineno"> 98</tt> <tt class="py-line"> <tt class="py-op">(</tt><tt class="py-name">b</tt> <tt class="py-keyword">for</tt> <tt class="py-name">a</tt><tt class="py-op">,</tt> <tt class="py-name">b</tt> <tt class="py-keyword">in</tt> <tt class="py-name">fields</tt><tt class="py-op">)</tt> </tt> |
| <a name="L99"></a><tt class="py-lineno"> 99</tt> <tt class="py-line"> <tt class="py-op">)</tt><tt class="py-op">)</tt> </tt> |
| <a name="L100"></a><tt class="py-lineno">100</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">include_attributes</tt> <tt class="py-keyword">and</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">_attributes</tt><tt class="py-op">:</tt> </tt> |
| <a name="L101"></a><tt class="py-lineno">101</tt> <tt class="py-line"> <tt class="py-name">rv</tt> <tt class="py-op">+=</tt> <tt class="py-name">fields</tt> <tt class="py-keyword">and</tt> <tt class="py-string">', '</tt> <tt class="py-keyword">or</tt> <tt class="py-string">' '</tt> </tt> |
| <a name="L102"></a><tt class="py-lineno">102</tt> <tt class="py-line"> <tt class="py-name">rv</tt> <tt class="py-op">+=</tt> <tt class="py-string">', '</tt><tt class="py-op">.</tt><tt class="py-name">join</tt><tt class="py-op">(</tt><tt class="py-string">'%s=%s'</tt> <tt class="py-op">%</tt> <tt class="py-op">(</tt><tt class="py-name">a</tt><tt class="py-op">,</tt> <tt class="py-name">_format</tt><tt class="py-op">(</tt><tt class="py-name">getattr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">a</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt> |
| <a name="L103"></a><tt class="py-lineno">103</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">a</tt> <tt class="py-keyword">in</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">_attributes</tt><tt class="py-op">)</tt> </tt> |
| <a name="L104"></a><tt class="py-lineno">104</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">rv</tt> <tt class="py-op">+</tt> <tt class="py-string">')'</tt> </tt> |
| <a name="L105"></a><tt class="py-lineno">105</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">list</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L106"></a><tt class="py-lineno">106</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-string">'[%s]'</tt> <tt class="py-op">%</tt> <tt class="py-string">', '</tt><tt class="py-op">.</tt><tt class="py-name">join</tt><tt class="py-op">(</tt><tt class="py-name">_format</tt><tt class="py-op">(</tt><tt class="py-name">x</tt><tt class="py-op">)</tt> <tt class="py-keyword">for</tt> <tt class="py-name">x</tt> <tt class="py-keyword">in</tt> <tt class="py-name">node</tt><tt class="py-op">)</tt> </tt> |
| <a name="L107"></a><tt class="py-lineno">107</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">repr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt> </tt> |
| </div><a name="L108"></a><tt class="py-lineno">108</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-keyword">not</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L109"></a><tt class="py-lineno">109</tt> <tt class="py-line"> <tt class="py-keyword">raise</tt> <tt class="py-name">TypeError</tt><tt class="py-op">(</tt><tt class="py-string">'expected AST, got %r'</tt> <tt class="py-op">%</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">__class__</tt><tt class="py-op">.</tt><tt class="py-name">__name__</tt><tt class="py-op">)</tt> </tt> |
| <a name="L110"></a><tt class="py-lineno">110</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">_format</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt> </tt> |
| </div><a name="L111"></a><tt class="py-lineno">111</tt> <tt class="py-line"> </tt> |
| <a name="L112"></a><tt class="py-lineno">112</tt> <tt class="py-line"> </tt> |
| <a name="copy_location"></a><div id="copy_location-def"><a name="L113"></a><tt class="py-lineno">113</tt> <a class="py-toggle" href="#" id="copy_location-toggle" onclick="return toggle('copy_location');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#copy_location">copy_location</a><tt class="py-op">(</tt><tt class="py-param">new_node</tt><tt class="py-op">,</tt> <tt class="py-param">old_node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="copy_location-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="copy_location-expanded"><a name="L114"></a><tt class="py-lineno">114</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L115"></a><tt class="py-lineno">115</tt> <tt class="py-line"><tt class="py-docstring"> Copy source location (`lineno` and `col_offset` attributes) from</tt> </tt> |
| <a name="L116"></a><tt class="py-lineno">116</tt> <tt class="py-line"><tt class="py-docstring"> *old_node* to *new_node* if possible, and return *new_node*.</tt> </tt> |
| <a name="L117"></a><tt class="py-lineno">117</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L118"></a><tt class="py-lineno">118</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">attr</tt> <tt class="py-keyword">in</tt> <tt class="py-string">'lineno'</tt><tt class="py-op">,</tt> <tt class="py-string">'col_offset'</tt><tt class="py-op">:</tt> </tt> |
| <a name="L119"></a><tt class="py-lineno">119</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">attr</tt> <tt class="py-keyword">in</tt> <tt class="py-name">old_node</tt><tt class="py-op">.</tt><tt class="py-name">_attributes</tt> <tt class="py-keyword">and</tt> <tt class="py-name">attr</tt> <tt class="py-keyword">in</tt> <tt class="py-name">new_node</tt><tt class="py-op">.</tt><tt class="py-name">_attributes</tt> \ </tt> |
| <a name="L120"></a><tt class="py-lineno">120</tt> <tt class="py-line"> <tt class="py-keyword">and</tt> <tt class="py-name">hasattr</tt><tt class="py-op">(</tt><tt class="py-name">old_node</tt><tt class="py-op">,</tt> <tt class="py-name">attr</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L121"></a><tt class="py-lineno">121</tt> <tt class="py-line"> <tt class="py-name">setattr</tt><tt class="py-op">(</tt><tt class="py-name">new_node</tt><tt class="py-op">,</tt> <tt class="py-name">attr</tt><tt class="py-op">,</tt> <tt class="py-name">getattr</tt><tt class="py-op">(</tt><tt class="py-name">old_node</tt><tt class="py-op">,</tt> <tt class="py-name">attr</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt> |
| <a name="L122"></a><tt class="py-lineno">122</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">new_node</tt> </tt> |
| </div><a name="L123"></a><tt class="py-lineno">123</tt> <tt class="py-line"> </tt> |
| <a name="L124"></a><tt class="py-lineno">124</tt> <tt class="py-line"> </tt> |
| <a name="fix_missing_locations"></a><div id="fix_missing_locations-def"><a name="L125"></a><tt class="py-lineno">125</tt> <a class="py-toggle" href="#" id="fix_missing_locations-toggle" onclick="return toggle('fix_missing_locations');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#fix_missing_locations">fix_missing_locations</a><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="fix_missing_locations-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="fix_missing_locations-expanded"><a name="L126"></a><tt class="py-lineno">126</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L127"></a><tt class="py-lineno">127</tt> <tt class="py-line"><tt class="py-docstring"> When you compile a node tree with compile(), the compiler expects lineno and</tt> </tt> |
| <a name="L128"></a><tt class="py-lineno">128</tt> <tt class="py-line"><tt class="py-docstring"> col_offset attributes for every node that supports them. This is rather</tt> </tt> |
| <a name="L129"></a><tt class="py-lineno">129</tt> <tt class="py-line"><tt class="py-docstring"> tedious to fill in for generated nodes, so this helper adds these attributes</tt> </tt> |
| <a name="L130"></a><tt class="py-lineno">130</tt> <tt class="py-line"><tt class="py-docstring"> recursively where not already set, by setting them to the values of the</tt> </tt> |
| <a name="L131"></a><tt class="py-lineno">131</tt> <tt class="py-line"><tt class="py-docstring"> parent node. It works recursively starting at *node*.</tt> </tt> |
| <a name="L132"></a><tt class="py-lineno">132</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L133"></a><tt class="py-lineno">133</tt> <tt class="py-line"> <tt class="py-keyword">def</tt> <tt class="py-def-name">_fix</tt><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">,</tt> <tt class="py-param">lineno</tt><tt class="py-op">,</tt> <tt class="py-param">col_offset</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L134"></a><tt class="py-lineno">134</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-string">'lineno'</tt> <tt class="py-keyword">in</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">_attributes</tt><tt class="py-op">:</tt> </tt> |
| <a name="L135"></a><tt class="py-lineno">135</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-keyword">not</tt> <tt class="py-name">hasattr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-string">'lineno'</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L136"></a><tt class="py-lineno">136</tt> <tt class="py-line"> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">lineno</tt> <tt class="py-op">=</tt> <tt class="py-name">lineno</tt> </tt> |
| <a name="L137"></a><tt class="py-lineno">137</tt> <tt class="py-line"> <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt> |
| <a name="L138"></a><tt class="py-lineno">138</tt> <tt class="py-line"> <tt class="py-name">lineno</tt> <tt class="py-op">=</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">lineno</tt> </tt> |
| <a name="L139"></a><tt class="py-lineno">139</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-string">'col_offset'</tt> <tt class="py-keyword">in</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">_attributes</tt><tt class="py-op">:</tt> </tt> |
| <a name="L140"></a><tt class="py-lineno">140</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-keyword">not</tt> <tt class="py-name">hasattr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-string">'col_offset'</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L141"></a><tt class="py-lineno">141</tt> <tt class="py-line"> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">col_offset</tt> <tt class="py-op">=</tt> <tt class="py-name">col_offset</tt> </tt> |
| <a name="L142"></a><tt class="py-lineno">142</tt> <tt class="py-line"> <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt> |
| <a name="L143"></a><tt class="py-lineno">143</tt> <tt class="py-line"> <tt class="py-name">col_offset</tt> <tt class="py-op">=</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">col_offset</tt> </tt> |
| <a name="L144"></a><tt class="py-lineno">144</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">child</tt> <tt class="py-keyword">in</tt> <tt id="link-2" class="py-name" targets="Function TestON.core.ast.iter_child_nodes()=TestON.core.ast-module.html#iter_child_nodes"><a title="TestON.core.ast.iter_child_nodes" class="py-name" href="#" onclick="return doclink('link-2', 'iter_child_nodes', 'link-2');">iter_child_nodes</a></tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L145"></a><tt class="py-lineno">145</tt> <tt class="py-line"> <tt class="py-name">_fix</tt><tt class="py-op">(</tt><tt class="py-name">child</tt><tt class="py-op">,</tt> <tt class="py-name">lineno</tt><tt class="py-op">,</tt> <tt class="py-name">col_offset</tt><tt class="py-op">)</tt> </tt> |
| </div><a name="L146"></a><tt class="py-lineno">146</tt> <tt class="py-line"> <tt class="py-name">_fix</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-number">0</tt><tt class="py-op">)</tt> </tt> |
| <a name="L147"></a><tt class="py-lineno">147</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">node</tt> </tt> |
| </div><a name="L148"></a><tt class="py-lineno">148</tt> <tt class="py-line"> </tt> |
| <a name="L149"></a><tt class="py-lineno">149</tt> <tt class="py-line"> </tt> |
| <a name="increment_lineno"></a><div id="increment_lineno-def"><a name="L150"></a><tt class="py-lineno">150</tt> <a class="py-toggle" href="#" id="increment_lineno-toggle" onclick="return toggle('increment_lineno');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#increment_lineno">increment_lineno</a><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">,</tt> <tt class="py-param">n</tt><tt class="py-op">=</tt><tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="increment_lineno-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="increment_lineno-expanded"><a name="L151"></a><tt class="py-lineno">151</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L152"></a><tt class="py-lineno">152</tt> <tt class="py-line"><tt class="py-docstring"> Increment the line number of each node in the tree starting at *node* by *n*.</tt> </tt> |
| <a name="L153"></a><tt class="py-lineno">153</tt> <tt class="py-line"><tt class="py-docstring"> This is useful to "move code" to a different location in a file.</tt> </tt> |
| <a name="L154"></a><tt class="py-lineno">154</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L155"></a><tt class="py-lineno">155</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">child</tt> <tt class="py-keyword">in</tt> <tt id="link-3" class="py-name" targets="Function TestON.core.ast.walk()=TestON.core.ast-module.html#walk"><a title="TestON.core.ast.walk" class="py-name" href="#" onclick="return doclink('link-3', 'walk', 'link-3');">walk</a></tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L156"></a><tt class="py-lineno">156</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-string">'lineno'</tt> <tt class="py-keyword">in</tt> <tt class="py-name">child</tt><tt class="py-op">.</tt><tt class="py-name">_attributes</tt><tt class="py-op">:</tt> </tt> |
| <a name="L157"></a><tt class="py-lineno">157</tt> <tt class="py-line"> <tt class="py-name">child</tt><tt class="py-op">.</tt><tt class="py-name">lineno</tt> <tt class="py-op">=</tt> <tt class="py-name">getattr</tt><tt class="py-op">(</tt><tt class="py-name">child</tt><tt class="py-op">,</tt> <tt class="py-string">'lineno'</tt><tt class="py-op">,</tt> <tt class="py-number">0</tt><tt class="py-op">)</tt> <tt class="py-op">+</tt> <tt class="py-name">n</tt> </tt> |
| <a name="L158"></a><tt class="py-lineno">158</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">node</tt> </tt> |
| </div><a name="L159"></a><tt class="py-lineno">159</tt> <tt class="py-line"> </tt> |
| <a name="L160"></a><tt class="py-lineno">160</tt> <tt class="py-line"> </tt> |
| <a name="iter_fields"></a><div id="iter_fields-def"><a name="L161"></a><tt class="py-lineno">161</tt> <a class="py-toggle" href="#" id="iter_fields-toggle" onclick="return toggle('iter_fields');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#iter_fields">iter_fields</a><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="iter_fields-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="iter_fields-expanded"><a name="L162"></a><tt class="py-lineno">162</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L163"></a><tt class="py-lineno">163</tt> <tt class="py-line"><tt class="py-docstring"> Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``</tt> </tt> |
| <a name="L164"></a><tt class="py-lineno">164</tt> <tt class="py-line"><tt class="py-docstring"> that is present on *node*.</tt> </tt> |
| <a name="L165"></a><tt class="py-lineno">165</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L166"></a><tt class="py-lineno">166</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">field</tt> <tt class="py-keyword">in</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">_fields</tt><tt class="py-op">:</tt> </tt> |
| <a name="L167"></a><tt class="py-lineno">167</tt> <tt class="py-line"> <tt class="py-keyword">try</tt><tt class="py-op">:</tt> </tt> |
| <a name="L168"></a><tt class="py-lineno">168</tt> <tt class="py-line"> <tt class="py-keyword">yield</tt> <tt class="py-name">field</tt><tt class="py-op">,</tt> <tt class="py-name">getattr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">field</tt><tt class="py-op">)</tt> </tt> |
| <a name="L169"></a><tt class="py-lineno">169</tt> <tt class="py-line"> <tt class="py-keyword">except</tt> <tt class="py-name">AttributeError</tt><tt class="py-op">:</tt> </tt> |
| <a name="L170"></a><tt class="py-lineno">170</tt> <tt class="py-line"> <tt class="py-keyword">pass</tt> </tt> |
| </div><a name="L171"></a><tt class="py-lineno">171</tt> <tt class="py-line"> </tt> |
| <a name="L172"></a><tt class="py-lineno">172</tt> <tt class="py-line"> </tt> |
| <a name="iter_child_nodes"></a><div id="iter_child_nodes-def"><a name="L173"></a><tt class="py-lineno">173</tt> <a class="py-toggle" href="#" id="iter_child_nodes-toggle" onclick="return toggle('iter_child_nodes');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#iter_child_nodes">iter_child_nodes</a><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="iter_child_nodes-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="iter_child_nodes-expanded"><a name="L174"></a><tt class="py-lineno">174</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L175"></a><tt class="py-lineno">175</tt> <tt class="py-line"><tt class="py-docstring"> Yield all direct child nodes of *node*, that is, all fields that are nodes</tt> </tt> |
| <a name="L176"></a><tt class="py-lineno">176</tt> <tt class="py-line"><tt class="py-docstring"> and all items of fields that are lists of nodes.</tt> </tt> |
| <a name="L177"></a><tt class="py-lineno">177</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L178"></a><tt class="py-lineno">178</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">name</tt><tt class="py-op">,</tt> <tt class="py-name">field</tt> <tt class="py-keyword">in</tt> <tt id="link-4" class="py-name"><a title="TestON.core.ast.iter_fields" class="py-name" href="#" onclick="return doclink('link-4', 'iter_fields', 'link-1');">iter_fields</a></tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L179"></a><tt class="py-lineno">179</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">field</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L180"></a><tt class="py-lineno">180</tt> <tt class="py-line"> <tt class="py-keyword">yield</tt> <tt class="py-name">field</tt> </tt> |
| <a name="L181"></a><tt class="py-lineno">181</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">field</tt><tt class="py-op">,</tt> <tt class="py-name">list</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L182"></a><tt class="py-lineno">182</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">item</tt> <tt class="py-keyword">in</tt> <tt class="py-name">field</tt><tt class="py-op">:</tt> </tt> |
| <a name="L183"></a><tt class="py-lineno">183</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">item</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L184"></a><tt class="py-lineno">184</tt> <tt class="py-line"> <tt class="py-keyword">yield</tt> <tt class="py-name">item</tt> </tt> |
| </div><a name="L185"></a><tt class="py-lineno">185</tt> <tt class="py-line"> </tt> |
| <a name="L186"></a><tt class="py-lineno">186</tt> <tt class="py-line"> </tt> |
| <a name="get_docstring"></a><div id="get_docstring-def"><a name="L187"></a><tt class="py-lineno">187</tt> <a class="py-toggle" href="#" id="get_docstring-toggle" onclick="return toggle('get_docstring');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#get_docstring">get_docstring</a><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">,</tt> <tt class="py-param">clean</tt><tt class="py-op">=</tt><tt class="py-name">True</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="get_docstring-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="get_docstring-expanded"><a name="L188"></a><tt class="py-lineno">188</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L189"></a><tt class="py-lineno">189</tt> <tt class="py-line"><tt class="py-docstring"> Return the docstring for the given node or None if no docstring can</tt> </tt> |
| <a name="L190"></a><tt class="py-lineno">190</tt> <tt class="py-line"><tt class="py-docstring"> be found. If the node provided does not have docstrings a TypeError</tt> </tt> |
| <a name="L191"></a><tt class="py-lineno">191</tt> <tt class="py-line"><tt class="py-docstring"> will be raised.</tt> </tt> |
| <a name="L192"></a><tt class="py-lineno">192</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L193"></a><tt class="py-lineno">193</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-keyword">not</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-op">(</tt><tt class="py-name">FunctionDef</tt><tt class="py-op">,</tt> <tt class="py-name">ClassDef</tt><tt class="py-op">,</tt> <tt class="py-name">Module</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L194"></a><tt class="py-lineno">194</tt> <tt class="py-line"> <tt class="py-keyword">raise</tt> <tt class="py-name">TypeError</tt><tt class="py-op">(</tt><tt class="py-string">"%r can't have docstrings"</tt> <tt class="py-op">%</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">__class__</tt><tt class="py-op">.</tt><tt class="py-name">__name__</tt><tt class="py-op">)</tt> </tt> |
| <a name="L195"></a><tt class="py-lineno">195</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">body</tt> <tt class="py-keyword">and</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">body</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">Expr</tt><tt class="py-op">)</tt> <tt class="py-keyword">and</tt> \ </tt> |
| <a name="L196"></a><tt class="py-lineno">196</tt> <tt class="py-line"> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">body</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">.</tt><tt class="py-name">value</tt><tt class="py-op">,</tt> <tt class="py-name">Str</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L197"></a><tt class="py-lineno">197</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">clean</tt><tt class="py-op">:</tt> </tt> |
| <a name="L198"></a><tt class="py-lineno">198</tt> <tt class="py-line"> <tt class="py-keyword">import</tt> <tt class="py-name">inspect</tt> </tt> |
| <a name="L199"></a><tt class="py-lineno">199</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">inspect</tt><tt class="py-op">.</tt><tt class="py-name">cleandoc</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">body</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">.</tt><tt class="py-name">value</tt><tt class="py-op">.</tt><tt class="py-name">s</tt><tt class="py-op">)</tt> </tt> |
| <a name="L200"></a><tt class="py-lineno">200</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">body</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">.</tt><tt class="py-name">value</tt><tt class="py-op">.</tt><tt class="py-name">s</tt> </tt> |
| </div><a name="L201"></a><tt class="py-lineno">201</tt> <tt class="py-line"> </tt> |
| <a name="L202"></a><tt class="py-lineno">202</tt> <tt class="py-line"> </tt> |
| <a name="walk"></a><div id="walk-def"><a name="L203"></a><tt class="py-lineno">203</tt> <a class="py-toggle" href="#" id="walk-toggle" onclick="return toggle('walk');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast-module.html#walk">walk</a><tt class="py-op">(</tt><tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="walk-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="walk-expanded"><a name="L204"></a><tt class="py-lineno">204</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L205"></a><tt class="py-lineno">205</tt> <tt class="py-line"><tt class="py-docstring"> Recursively yield all descendant nodes in the tree starting at *node*</tt> </tt> |
| <a name="L206"></a><tt class="py-lineno">206</tt> <tt class="py-line"><tt class="py-docstring"> (including *node* itself), in no specified order. This is useful if you</tt> </tt> |
| <a name="L207"></a><tt class="py-lineno">207</tt> <tt class="py-line"><tt class="py-docstring"> only want to modify nodes in place and don't care about the context.</tt> </tt> |
| <a name="L208"></a><tt class="py-lineno">208</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L209"></a><tt class="py-lineno">209</tt> <tt class="py-line"> <tt class="py-keyword">from</tt> <tt class="py-name">collections</tt> <tt class="py-keyword">import</tt> <tt class="py-name">deque</tt> </tt> |
| <a name="L210"></a><tt class="py-lineno">210</tt> <tt class="py-line"> <tt class="py-name">todo</tt> <tt class="py-op">=</tt> <tt class="py-name">deque</tt><tt class="py-op">(</tt><tt class="py-op">[</tt><tt class="py-name">node</tt><tt class="py-op">]</tt><tt class="py-op">)</tt> </tt> |
| <a name="L211"></a><tt class="py-lineno">211</tt> <tt class="py-line"> <tt class="py-keyword">while</tt> <tt class="py-name">todo</tt><tt class="py-op">:</tt> </tt> |
| <a name="L212"></a><tt class="py-lineno">212</tt> <tt class="py-line"> <tt class="py-name">node</tt> <tt class="py-op">=</tt> <tt class="py-name">todo</tt><tt class="py-op">.</tt><tt class="py-name">popleft</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt> |
| <a name="L213"></a><tt class="py-lineno">213</tt> <tt class="py-line"> <tt class="py-name">todo</tt><tt class="py-op">.</tt><tt class="py-name">extend</tt><tt class="py-op">(</tt><tt id="link-5" class="py-name"><a title="TestON.core.ast.iter_child_nodes" class="py-name" href="#" onclick="return doclink('link-5', 'iter_child_nodes', 'link-2');">iter_child_nodes</a></tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt> |
| <a name="L214"></a><tt class="py-lineno">214</tt> <tt class="py-line"> <tt class="py-keyword">yield</tt> <tt class="py-name">node</tt> </tt> |
| </div><a name="L215"></a><tt class="py-lineno">215</tt> <tt class="py-line"> </tt> |
| <a name="L216"></a><tt class="py-lineno">216</tt> <tt class="py-line"> </tt> |
| <a name="NodeVisitor"></a><div id="NodeVisitor-def"><a name="L217"></a><tt class="py-lineno">217</tt> <a class="py-toggle" href="#" id="NodeVisitor-toggle" onclick="return toggle('NodeVisitor');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="TestON.core.ast.NodeVisitor-class.html">NodeVisitor</a><tt class="py-op">(</tt><tt class="py-base-class">object</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="NodeVisitor-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="NodeVisitor-expanded"><a name="L218"></a><tt class="py-lineno">218</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L219"></a><tt class="py-lineno">219</tt> <tt class="py-line"><tt class="py-docstring"> A node visitor base class that walks the abstract syntax tree and calls a</tt> </tt> |
| <a name="L220"></a><tt class="py-lineno">220</tt> <tt class="py-line"><tt class="py-docstring"> visitor function for every node found. This function may return a value</tt> </tt> |
| <a name="L221"></a><tt class="py-lineno">221</tt> <tt class="py-line"><tt class="py-docstring"> which is forwarded by the `visit` method.</tt> </tt> |
| <a name="L222"></a><tt class="py-lineno">222</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L223"></a><tt class="py-lineno">223</tt> <tt class="py-line"><tt class="py-docstring"> This class is meant to be subclassed, with the subclass adding visitor</tt> </tt> |
| <a name="L224"></a><tt class="py-lineno">224</tt> <tt class="py-line"><tt class="py-docstring"> methods.</tt> </tt> |
| <a name="L225"></a><tt class="py-lineno">225</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L226"></a><tt class="py-lineno">226</tt> <tt class="py-line"><tt class="py-docstring"> Per default the visitor functions for the nodes are ``'visit_'`` +</tt> </tt> |
| <a name="L227"></a><tt class="py-lineno">227</tt> <tt class="py-line"><tt class="py-docstring"> class name of the node. So a `TryFinally` node visit function would</tt> </tt> |
| <a name="L228"></a><tt class="py-lineno">228</tt> <tt class="py-line"><tt class="py-docstring"> be `visit_TryFinally`. This behavior can be changed by overriding</tt> </tt> |
| <a name="L229"></a><tt class="py-lineno">229</tt> <tt class="py-line"><tt class="py-docstring"> the `visit` method. If no visitor function exists for a node</tt> </tt> |
| <a name="L230"></a><tt class="py-lineno">230</tt> <tt class="py-line"><tt class="py-docstring"> (return value `None`) the `generic_visit` visitor is used instead.</tt> </tt> |
| <a name="L231"></a><tt class="py-lineno">231</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L232"></a><tt class="py-lineno">232</tt> <tt class="py-line"><tt class="py-docstring"> Don't use the `NodeVisitor` if you want to apply changes to nodes during</tt> </tt> |
| <a name="L233"></a><tt class="py-lineno">233</tt> <tt class="py-line"><tt class="py-docstring"> traversing. For this a special visitor exists (`NodeTransformer`) that</tt> </tt> |
| <a name="L234"></a><tt class="py-lineno">234</tt> <tt class="py-line"><tt class="py-docstring"> allows modifications.</tt> </tt> |
| <a name="L235"></a><tt class="py-lineno">235</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L236"></a><tt class="py-lineno">236</tt> <tt class="py-line"> </tt> |
| <a name="NodeVisitor.visit"></a><div id="NodeVisitor.visit-def"><a name="L237"></a><tt class="py-lineno">237</tt> <a class="py-toggle" href="#" id="NodeVisitor.visit-toggle" onclick="return toggle('NodeVisitor.visit');">-</a><tt class="py-line"> <tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast.NodeVisitor-class.html#visit">visit</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="NodeVisitor.visit-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="NodeVisitor.visit-expanded"><a name="L238"></a><tt class="py-lineno">238</tt> <tt class="py-line"> <tt class="py-docstring">"""Visit a node."""</tt> </tt> |
| <a name="L239"></a><tt class="py-lineno">239</tt> <tt class="py-line"> <tt class="py-name">method</tt> <tt class="py-op">=</tt> <tt class="py-string">'visit_'</tt> <tt class="py-op">+</tt> <tt class="py-name">node</tt><tt class="py-op">.</tt><tt class="py-name">__class__</tt><tt class="py-op">.</tt><tt class="py-name">__name__</tt> </tt> |
| <a name="L240"></a><tt class="py-lineno">240</tt> <tt class="py-line"> <tt class="py-name">visitor</tt> <tt class="py-op">=</tt> <tt class="py-name">getattr</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">,</tt> <tt class="py-name">method</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-6" class="py-name" targets="Method TestON.core.ast.NodeTransformer.generic_visit()=TestON.core.ast.NodeTransformer-class.html#generic_visit,Method TestON.core.ast.NodeVisitor.generic_visit()=TestON.core.ast.NodeVisitor-class.html#generic_visit"><a title="TestON.core.ast.NodeTransformer.generic_visit |
| TestON.core.ast.NodeVisitor.generic_visit" class="py-name" href="#" onclick="return doclink('link-6', 'generic_visit', 'link-6');">generic_visit</a></tt><tt class="py-op">)</tt> </tt> |
| <a name="L241"></a><tt class="py-lineno">241</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">visitor</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt> </tt> |
| </div><a name="L242"></a><tt class="py-lineno">242</tt> <tt class="py-line"> </tt> |
| <a name="NodeVisitor.generic_visit"></a><div id="NodeVisitor.generic_visit-def"><a name="L243"></a><tt class="py-lineno">243</tt> <a class="py-toggle" href="#" id="NodeVisitor.generic_visit-toggle" onclick="return toggle('NodeVisitor.generic_visit');">-</a><tt class="py-line"> <tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast.NodeVisitor-class.html#generic_visit">generic_visit</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="NodeVisitor.generic_visit-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="NodeVisitor.generic_visit-expanded"><a name="L244"></a><tt class="py-lineno">244</tt> <tt class="py-line"> <tt class="py-docstring">"""Called if no explicit visitor function exists for a node."""</tt> </tt> |
| <a name="L245"></a><tt class="py-lineno">245</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">field</tt><tt class="py-op">,</tt> <tt class="py-name">value</tt> <tt class="py-keyword">in</tt> <tt id="link-7" class="py-name"><a title="TestON.core.ast.iter_fields" class="py-name" href="#" onclick="return doclink('link-7', 'iter_fields', 'link-1');">iter_fields</a></tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L246"></a><tt class="py-lineno">246</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">,</tt> <tt class="py-name">list</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L247"></a><tt class="py-lineno">247</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">item</tt> <tt class="py-keyword">in</tt> <tt class="py-name">value</tt><tt class="py-op">:</tt> </tt> |
| <a name="L248"></a><tt class="py-lineno">248</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">item</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L249"></a><tt class="py-lineno">249</tt> <tt class="py-line"> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-8" class="py-name" targets="Method TestON.core.ast.NodeVisitor.visit()=TestON.core.ast.NodeVisitor-class.html#visit"><a title="TestON.core.ast.NodeVisitor.visit" class="py-name" href="#" onclick="return doclink('link-8', 'visit', 'link-8');">visit</a></tt><tt class="py-op">(</tt><tt class="py-name">item</tt><tt class="py-op">)</tt> </tt> |
| <a name="L250"></a><tt class="py-lineno">250</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L251"></a><tt class="py-lineno">251</tt> <tt class="py-line"> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-9" class="py-name"><a title="TestON.core.ast.NodeVisitor.visit" class="py-name" href="#" onclick="return doclink('link-9', 'visit', 'link-8');">visit</a></tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">)</tt> </tt> |
| </div></div><a name="L252"></a><tt class="py-lineno">252</tt> <tt class="py-line"> </tt> |
| <a name="L253"></a><tt class="py-lineno">253</tt> <tt class="py-line"> </tt> |
| <a name="NodeTransformer"></a><div id="NodeTransformer-def"><a name="L254"></a><tt class="py-lineno">254</tt> <a class="py-toggle" href="#" id="NodeTransformer-toggle" onclick="return toggle('NodeTransformer');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="TestON.core.ast.NodeTransformer-class.html">NodeTransformer</a><tt class="py-op">(</tt><tt class="py-base-class">NodeVisitor</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="NodeTransformer-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="NodeTransformer-expanded"><a name="L255"></a><tt class="py-lineno">255</tt> <tt class="py-line"> <tt class="py-docstring">"""</tt> </tt> |
| <a name="L256"></a><tt class="py-lineno">256</tt> <tt class="py-line"><tt class="py-docstring"> A :class:`NodeVisitor` subclass that walks the abstract syntax tree and</tt> </tt> |
| <a name="L257"></a><tt class="py-lineno">257</tt> <tt class="py-line"><tt class="py-docstring"> allows modification of nodes.</tt> </tt> |
| <a name="L258"></a><tt class="py-lineno">258</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L259"></a><tt class="py-lineno">259</tt> <tt class="py-line"><tt class="py-docstring"> The `NodeTransformer` will walk the AST and use the return value of the</tt> </tt> |
| <a name="L260"></a><tt class="py-lineno">260</tt> <tt class="py-line"><tt class="py-docstring"> visitor methods to replace or remove the old node. If the return value of</tt> </tt> |
| <a name="L261"></a><tt class="py-lineno">261</tt> <tt class="py-line"><tt class="py-docstring"> the visitor method is ``None``, the node will be removed from its location,</tt> </tt> |
| <a name="L262"></a><tt class="py-lineno">262</tt> <tt class="py-line"><tt class="py-docstring"> otherwise it is replaced with the return value. The return value may be the</tt> </tt> |
| <a name="L263"></a><tt class="py-lineno">263</tt> <tt class="py-line"><tt class="py-docstring"> original node in which case no replacement takes place.</tt> </tt> |
| <a name="L264"></a><tt class="py-lineno">264</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L265"></a><tt class="py-lineno">265</tt> <tt class="py-line"><tt class="py-docstring"> Here is an example transformer that rewrites all occurrences of name lookups</tt> </tt> |
| <a name="L266"></a><tt class="py-lineno">266</tt> <tt class="py-line"><tt class="py-docstring"> (``foo``) to ``data['foo']``::</tt> </tt> |
| <a name="L267"></a><tt class="py-lineno">267</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L268"></a><tt class="py-lineno">268</tt> <tt class="py-line"><tt class="py-docstring"> class RewriteName(NodeTransformer):</tt> </tt> |
| <a name="L269"></a><tt class="py-lineno">269</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L270"></a><tt class="py-lineno">270</tt> <tt class="py-line"><tt class="py-docstring"> def visit_Name(self, node):</tt> </tt> |
| <a name="L271"></a><tt class="py-lineno">271</tt> <tt class="py-line"><tt class="py-docstring"> return copy_location(Subscript(</tt> </tt> |
| <a name="L272"></a><tt class="py-lineno">272</tt> <tt class="py-line"><tt class="py-docstring"> value=Name(id='data', ctx=Load()),</tt> </tt> |
| <a name="L273"></a><tt class="py-lineno">273</tt> <tt class="py-line"><tt class="py-docstring"> slice=Index(value=Str(s=node.id)),</tt> </tt> |
| <a name="L274"></a><tt class="py-lineno">274</tt> <tt class="py-line"><tt class="py-docstring"> ctx=node.ctx</tt> </tt> |
| <a name="L275"></a><tt class="py-lineno">275</tt> <tt class="py-line"><tt class="py-docstring"> ), node)</tt> </tt> |
| <a name="L276"></a><tt class="py-lineno">276</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L277"></a><tt class="py-lineno">277</tt> <tt class="py-line"><tt class="py-docstring"> Keep in mind that if the node you're operating on has child nodes you must</tt> </tt> |
| <a name="L278"></a><tt class="py-lineno">278</tt> <tt class="py-line"><tt class="py-docstring"> either transform the child nodes yourself or call the :meth:`generic_visit`</tt> </tt> |
| <a name="L279"></a><tt class="py-lineno">279</tt> <tt class="py-line"><tt class="py-docstring"> method for the node first.</tt> </tt> |
| <a name="L280"></a><tt class="py-lineno">280</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L281"></a><tt class="py-lineno">281</tt> <tt class="py-line"><tt class="py-docstring"> For nodes that were part of a collection of statements (that applies to all</tt> </tt> |
| <a name="L282"></a><tt class="py-lineno">282</tt> <tt class="py-line"><tt class="py-docstring"> statement nodes), the visitor may also return a list of nodes rather than</tt> </tt> |
| <a name="L283"></a><tt class="py-lineno">283</tt> <tt class="py-line"><tt class="py-docstring"> just a single node.</tt> </tt> |
| <a name="L284"></a><tt class="py-lineno">284</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L285"></a><tt class="py-lineno">285</tt> <tt class="py-line"><tt class="py-docstring"> Usually you use the transformer like this::</tt> </tt> |
| <a name="L286"></a><tt class="py-lineno">286</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt> |
| <a name="L287"></a><tt class="py-lineno">287</tt> <tt class="py-line"><tt class="py-docstring"> node = YourTransformer().visit(node)</tt> </tt> |
| <a name="L288"></a><tt class="py-lineno">288</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt> |
| <a name="L289"></a><tt class="py-lineno">289</tt> <tt class="py-line"> </tt> |
| <a name="NodeTransformer.generic_visit"></a><div id="NodeTransformer.generic_visit-def"><a name="L290"></a><tt class="py-lineno">290</tt> <a class="py-toggle" href="#" id="NodeTransformer.generic_visit-toggle" onclick="return toggle('NodeTransformer.generic_visit');">-</a><tt class="py-line"> <tt class="py-keyword">def</tt> <a class="py-def-name" href="TestON.core.ast.NodeTransformer-class.html#generic_visit">generic_visit</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| </div><div id="NodeTransformer.generic_visit-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="NodeTransformer.generic_visit-expanded"><a name="L291"></a><tt class="py-lineno">291</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">field</tt><tt class="py-op">,</tt> <tt class="py-name">old_value</tt> <tt class="py-keyword">in</tt> <tt id="link-10" class="py-name"><a title="TestON.core.ast.iter_fields" class="py-name" href="#" onclick="return doclink('link-10', 'iter_fields', 'link-1');">iter_fields</a></tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L292"></a><tt class="py-lineno">292</tt> <tt class="py-line"> <tt class="py-name">old_value</tt> <tt class="py-op">=</tt> <tt class="py-name">getattr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">field</tt><tt class="py-op">,</tt> <tt class="py-name">None</tt><tt class="py-op">)</tt> </tt> |
| <a name="L293"></a><tt class="py-lineno">293</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">old_value</tt><tt class="py-op">,</tt> <tt class="py-name">list</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L294"></a><tt class="py-lineno">294</tt> <tt class="py-line"> <tt class="py-name">new_values</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt> |
| <a name="L295"></a><tt class="py-lineno">295</tt> <tt class="py-line"> <tt class="py-keyword">for</tt> <tt class="py-name">value</tt> <tt class="py-keyword">in</tt> <tt class="py-name">old_value</tt><tt class="py-op">:</tt> </tt> |
| <a name="L296"></a><tt class="py-lineno">296</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L297"></a><tt class="py-lineno">297</tt> <tt class="py-line"> <tt class="py-name">value</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-11" class="py-name"><a title="TestON.core.ast.NodeVisitor.visit" class="py-name" href="#" onclick="return doclink('link-11', 'visit', 'link-8');">visit</a></tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">)</tt> </tt> |
| <a name="L298"></a><tt class="py-lineno">298</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">value</tt> <tt class="py-keyword">is</tt> <tt class="py-name">None</tt><tt class="py-op">:</tt> </tt> |
| <a name="L299"></a><tt class="py-lineno">299</tt> <tt class="py-line"> <tt class="py-keyword">continue</tt> </tt> |
| <a name="L300"></a><tt class="py-lineno">300</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-keyword">not</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L301"></a><tt class="py-lineno">301</tt> <tt class="py-line"> <tt class="py-name">new_values</tt><tt class="py-op">.</tt><tt class="py-name">extend</tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">)</tt> </tt> |
| <a name="L302"></a><tt class="py-lineno">302</tt> <tt class="py-line"> <tt class="py-keyword">continue</tt> </tt> |
| <a name="L303"></a><tt class="py-lineno">303</tt> <tt class="py-line"> <tt class="py-name">new_values</tt><tt class="py-op">.</tt><tt class="py-name">append</tt><tt class="py-op">(</tt><tt class="py-name">value</tt><tt class="py-op">)</tt> </tt> |
| <a name="L304"></a><tt class="py-lineno">304</tt> <tt class="py-line"> <tt class="py-name">old_value</tt><tt class="py-op">[</tt><tt class="py-op">:</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">new_values</tt> </tt> |
| <a name="L305"></a><tt class="py-lineno">305</tt> <tt class="py-line"> <tt class="py-keyword">elif</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">old_value</tt><tt class="py-op">,</tt> <tt class="py-name">AST</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt> |
| <a name="L306"></a><tt class="py-lineno">306</tt> <tt class="py-line"> <tt class="py-name">new_node</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-12" class="py-name"><a title="TestON.core.ast.NodeVisitor.visit" class="py-name" href="#" onclick="return doclink('link-12', 'visit', 'link-8');">visit</a></tt><tt class="py-op">(</tt><tt class="py-name">old_value</tt><tt class="py-op">)</tt> </tt> |
| <a name="L307"></a><tt class="py-lineno">307</tt> <tt class="py-line"> <tt class="py-keyword">if</tt> <tt class="py-name">new_node</tt> <tt class="py-keyword">is</tt> <tt class="py-name">None</tt><tt class="py-op">:</tt> </tt> |
| <a name="L308"></a><tt class="py-lineno">308</tt> <tt class="py-line"> <tt class="py-name">delattr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">field</tt><tt class="py-op">)</tt> </tt> |
| <a name="L309"></a><tt class="py-lineno">309</tt> <tt class="py-line"> <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt> |
| <a name="L310"></a><tt class="py-lineno">310</tt> <tt class="py-line"> <tt class="py-name">setattr</tt><tt class="py-op">(</tt><tt class="py-name">node</tt><tt class="py-op">,</tt> <tt class="py-name">field</tt><tt class="py-op">,</tt> <tt class="py-name">new_node</tt><tt class="py-op">)</tt> </tt> |
| <a name="L311"></a><tt class="py-lineno">311</tt> <tt class="py-line"> <tt class="py-keyword">return</tt> <tt class="py-name">node</tt> </tt> |
| </div></div><a name="L312"></a><tt class="py-lineno">312</tt> <tt class="py-line"> </tt><script type="text/javascript"> |
| <!-- |
| expandto(location.href); |
| // --> |
| </script> |
| </pre> |
| <br /> |
| <!-- ==================== NAVIGATION BAR ==================== --> |
| <table class="navbar" border="0" width="100%" cellpadding="0" |
| bgcolor="#a0c0ff" cellspacing="0"> |
| <tr valign="middle"> |
| <!-- Home link --> |
| <th> <a |
| href="TestON-module.html">Home</a> </th> |
| |
| <!-- Tree link --> |
| <th> <a |
| href="module-tree.html">Trees</a> </th> |
| |
| <!-- Index link --> |
| <th> <a |
| href="identifier-index.html">Indices</a> </th> |
| |
| <!-- Help link --> |
| <th> <a |
| href="help.html">Help</a> </th> |
| |
| <th class="navbar" width="100%"></th> |
| </tr> |
| </table> |
| <table border="0" cellpadding="0" cellspacing="0" width="100%%"> |
| <tr> |
| <td align="left" class="footer"> |
| Generated by Epydoc 3.0.1 on Mon Mar 11 12:56:27 2013 |
| </td> |
| <td align="right" class="footer"> |
| <a target="mainFrame" href="http://epydoc.sourceforge.net" |
| >http://epydoc.sourceforge.net</a> |
| </td> |
| </tr> |
| </table> |
| |
| <script type="text/javascript"> |
| <!-- |
| // Private objects are initially displayed (because if |
| // javascript is turned off then we want them to be |
| // visible); but by default, we want to hide them. So hide |
| // them unless we have a cookie that says to show them. |
| checkCookie(); |
| // --> |
| </script> |
| </body> |
| </html> |