blob: 159075f01b5c18fa4f32ee5f8a044d3516d773e7 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001<?xml version="1.0" encoding="ascii"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "DTD/xhtml1-transitional.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5<head>
6 <title>TestON.core.ast</title>
7 <link rel="stylesheet" href="epydoc.css" type="text/css" />
8 <script type="text/javascript" src="epydoc.js"></script>
9</head>
10
11<body bgcolor="white" text="black" link="blue" vlink="#204080"
12 alink="#204080">
13<!-- ==================== NAVIGATION BAR ==================== -->
14<table class="navbar" border="0" width="100%" cellpadding="0"
15 bgcolor="#a0c0ff" cellspacing="0">
16 <tr valign="middle">
17 <!-- Home link -->
18 <th>&nbsp;&nbsp;&nbsp;<a
19 href="TestON-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>
20
21 <!-- Tree link -->
22 <th>&nbsp;&nbsp;&nbsp;<a
23 href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>
24
25 <!-- Index link -->
26 <th>&nbsp;&nbsp;&nbsp;<a
27 href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>
28
29 <!-- Help link -->
30 <th>&nbsp;&nbsp;&nbsp;<a
31 href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>
32
33 <th class="navbar" width="100%"></th>
34 </tr>
35</table>
36<table width="100%" cellpadding="0" cellspacing="0">
37 <tr valign="top">
38 <td width="100%">
39 <span class="breadcrumbs">
40 <a href="TestON-module.html">Package&nbsp;TestON</a> ::
41 <a href="TestON.core-module.html">Package&nbsp;core</a> ::
42 Module&nbsp;ast
43 </span>
44 </td>
45 <td>
46 <table cellpadding="0" cellspacing="0">
47 <!-- hide/show private -->
48 <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink"
49 onclick="toggle_private();">hide&nbsp;private</a>]</span></td></tr>
50 <tr><td align="right"><span class="options"
51 >[<a href="frames.html" target="_top">frames</a
52 >]&nbsp;|&nbsp;<a href="TestON.core.ast-pysrc.html"
53 target="_top">no&nbsp;frames</a>]</span></td></tr>
54 </table>
55 </td>
56 </tr>
57</table>
58<h1 class="epydoc">Source Code for <a href="TestON.core.ast-module.html">Module TestON.core.ast</a></h1>
59<pre class="py-src">
60<a name="L1"></a><tt class="py-lineno"> 1</tt> <tt class="py-line"><tt class="py-comment"># -*- coding: utf-8 -*-</tt> </tt>
61<a name="L2"></a><tt class="py-lineno"> 2</tt> <tt class="py-line"><tt class="py-docstring">"""</tt> </tt>
62<a name="L3"></a><tt class="py-lineno"> 3</tt> <tt class="py-line"><tt class="py-docstring"> ast</tt> </tt>
63<a name="L4"></a><tt class="py-lineno"> 4</tt> <tt class="py-line"><tt class="py-docstring"> ~~~</tt> </tt>
64<a name="L5"></a><tt class="py-lineno"> 5</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
65<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>
66<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>
67<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>
68<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>
69<a name="L10"></a><tt class="py-lineno"> 10</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
70<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>
71<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>
72<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>
73<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>
74<a name="L15"></a><tt class="py-lineno"> 15</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
75<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>
76<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>
77<a name="L18"></a><tt class="py-lineno"> 18</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
78<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>
79<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>
80<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>
81<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>
82<a name="L23"></a><tt class="py-lineno"> 23</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
83<a name="L24"></a><tt class="py-lineno"> 24</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
84<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>
85<a name="L26"></a><tt class="py-lineno"> 26</tt> <tt class="py-line"><tt class="py-docstring"> :license: Python License.</tt> </tt>
86<a name="L27"></a><tt class="py-lineno"> 27</tt> <tt class="py-line"><tt class="py-docstring">"""</tt> </tt>
87<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>
88<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>
89<a name="L30"></a><tt class="py-lineno"> 30</tt> <tt class="py-line"> </tt>
90<a name="L31"></a><tt class="py-lineno"> 31</tt> <tt class="py-line"> </tt>
91<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">'&lt;unknown&gt;'</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>
92</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>
93<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>
94<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>
95<a name="L36"></a><tt class="py-lineno"> 36</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
96<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>
97</div><a name="L38"></a><tt class="py-lineno"> 38</tt> <tt class="py-line"> </tt>
98<a name="L39"></a><tt class="py-lineno"> 39</tt> <tt class="py-line"> </tt>
99<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>
100</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>
101<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>
102<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>
103<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>
104<a name="L45"></a><tt class="py-lineno"> 45</tt> <tt class="py-line"><tt class="py-docstring"> and None.</tt> </tt>
105<a name="L46"></a><tt class="py-lineno"> 46</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
106<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>
107<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>
108<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
109TestON.core.iniparser.iniparser.parse
110TestON.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>
111<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>
112<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>
113<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>
114<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>
115<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>
116<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>
117<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>
118<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>
119<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>
120<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>
121<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>
122<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>
123<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>
124<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>
125<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>
126<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>
127<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>
128<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>
129<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>
130<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>
131<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>
132<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>
133<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>
134<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>
135<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>
136<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>
137<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>
138<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>
139<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>
140<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>
141</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>
142</div><a name="L81"></a><tt class="py-lineno"> 81</tt> <tt class="py-line"> </tt>
143<a name="L82"></a><tt class="py-lineno"> 82</tt> <tt class="py-line"> </tt>
144<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>
145</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>
146<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>
147<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>
148<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>
149<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>
150<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>
151<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>
152<a name="L91"></a><tt class="py-lineno"> 91</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
153<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>
154<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>
155<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>
156<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>
157<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>
158<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>
159<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>
160<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>
161<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>
162<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>
163<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>
164<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>
165<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>
166<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>
167<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>
168<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>
169</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>
170<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>
171<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>
172</div><a name="L111"></a><tt class="py-lineno">111</tt> <tt class="py-line"> </tt>
173<a name="L112"></a><tt class="py-lineno">112</tt> <tt class="py-line"> </tt>
174<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>
175</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>
176<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>
177<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>
178<a name="L117"></a><tt class="py-lineno">117</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
179<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>
180<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>
181<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>
182<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>
183<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>
184</div><a name="L123"></a><tt class="py-lineno">123</tt> <tt class="py-line"> </tt>
185<a name="L124"></a><tt class="py-lineno">124</tt> <tt class="py-line"> </tt>
186<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>
187</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>
188<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>
189<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>
190<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>
191<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>
192<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>
193<a name="L132"></a><tt class="py-lineno">132</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
194<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>
195<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>
196<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>
197<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>
198<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>
199<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>
200<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>
201<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>
202<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>
203<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>
204<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>
205<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>
206<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>
207</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>
208<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>
209</div><a name="L148"></a><tt class="py-lineno">148</tt> <tt class="py-line"> </tt>
210<a name="L149"></a><tt class="py-lineno">149</tt> <tt class="py-line"> </tt>
211<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>
212</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>
213<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>
214<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>
215<a name="L154"></a><tt class="py-lineno">154</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
216<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>
217<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>
218<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>
219<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>
220</div><a name="L159"></a><tt class="py-lineno">159</tt> <tt class="py-line"> </tt>
221<a name="L160"></a><tt class="py-lineno">160</tt> <tt class="py-line"> </tt>
222<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>
223</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>
224<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>
225<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>
226<a name="L165"></a><tt class="py-lineno">165</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
227<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>
228<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>
229<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>
230<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>
231<a name="L170"></a><tt class="py-lineno">170</tt> <tt class="py-line"> <tt class="py-keyword">pass</tt> </tt>
232</div><a name="L171"></a><tt class="py-lineno">171</tt> <tt class="py-line"> </tt>
233<a name="L172"></a><tt class="py-lineno">172</tt> <tt class="py-line"> </tt>
234<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>
235</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>
236<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>
237<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>
238<a name="L177"></a><tt class="py-lineno">177</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
239<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>
240<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>
241<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>
242<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>
243<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>
244<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>
245<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>
246</div><a name="L185"></a><tt class="py-lineno">185</tt> <tt class="py-line"> </tt>
247<a name="L186"></a><tt class="py-lineno">186</tt> <tt class="py-line"> </tt>
248<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>
249</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>
250<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>
251<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>
252<a name="L191"></a><tt class="py-lineno">191</tt> <tt class="py-line"><tt class="py-docstring"> will be raised.</tt> </tt>
253<a name="L192"></a><tt class="py-lineno">192</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
254<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>
255<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>
256<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>
257<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>
258<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>
259<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>
260<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>
261<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>
262</div><a name="L201"></a><tt class="py-lineno">201</tt> <tt class="py-line"> </tt>
263<a name="L202"></a><tt class="py-lineno">202</tt> <tt class="py-line"> </tt>
264<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>
265</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>
266<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>
267<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>
268<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>
269<a name="L208"></a><tt class="py-lineno">208</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
270<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>
271<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>
272<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>
273<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>
274<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>
275<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>
276</div><a name="L215"></a><tt class="py-lineno">215</tt> <tt class="py-line"> </tt>
277<a name="L216"></a><tt class="py-lineno">216</tt> <tt class="py-line"> </tt>
278<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>
279</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>
280<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>
281<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>
282<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>
283<a name="L222"></a><tt class="py-lineno">222</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
284<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>
285<a name="L224"></a><tt class="py-lineno">224</tt> <tt class="py-line"><tt class="py-docstring"> methods.</tt> </tt>
286<a name="L225"></a><tt class="py-lineno">225</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
287<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>
288<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>
289<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>
290<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>
291<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>
292<a name="L231"></a><tt class="py-lineno">231</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
293<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>
294<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>
295<a name="L234"></a><tt class="py-lineno">234</tt> <tt class="py-line"><tt class="py-docstring"> allows modifications.</tt> </tt>
296<a name="L235"></a><tt class="py-lineno">235</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
297<a name="L236"></a><tt class="py-lineno">236</tt> <tt class="py-line"> </tt>
298<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>
299</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>
300<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>
301<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
302TestON.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>
303<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>
304</div><a name="L242"></a><tt class="py-lineno">242</tt> <tt class="py-line"> </tt>
305<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>
306</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>
307<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>
308<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>
309<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>
310<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>
311<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>
312<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>
313<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>
314</div></div><a name="L252"></a><tt class="py-lineno">252</tt> <tt class="py-line"> </tt>
315<a name="L253"></a><tt class="py-lineno">253</tt> <tt class="py-line"> </tt>
316<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>
317</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>
318<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>
319<a name="L257"></a><tt class="py-lineno">257</tt> <tt class="py-line"><tt class="py-docstring"> allows modification of nodes.</tt> </tt>
320<a name="L258"></a><tt class="py-lineno">258</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
321<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>
322<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>
323<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>
324<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>
325<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>
326<a name="L264"></a><tt class="py-lineno">264</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
327<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>
328<a name="L266"></a><tt class="py-lineno">266</tt> <tt class="py-line"><tt class="py-docstring"> (``foo``) to ``data['foo']``::</tt> </tt>
329<a name="L267"></a><tt class="py-lineno">267</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
330<a name="L268"></a><tt class="py-lineno">268</tt> <tt class="py-line"><tt class="py-docstring"> class RewriteName(NodeTransformer):</tt> </tt>
331<a name="L269"></a><tt class="py-lineno">269</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
332<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>
333<a name="L271"></a><tt class="py-lineno">271</tt> <tt class="py-line"><tt class="py-docstring"> return copy_location(Subscript(</tt> </tt>
334<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>
335<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>
336<a name="L274"></a><tt class="py-lineno">274</tt> <tt class="py-line"><tt class="py-docstring"> ctx=node.ctx</tt> </tt>
337<a name="L275"></a><tt class="py-lineno">275</tt> <tt class="py-line"><tt class="py-docstring"> ), node)</tt> </tt>
338<a name="L276"></a><tt class="py-lineno">276</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
339<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>
340<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>
341<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>
342<a name="L280"></a><tt class="py-lineno">280</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
343<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>
344<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>
345<a name="L283"></a><tt class="py-lineno">283</tt> <tt class="py-line"><tt class="py-docstring"> just a single node.</tt> </tt>
346<a name="L284"></a><tt class="py-lineno">284</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
347<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>
348<a name="L286"></a><tt class="py-lineno">286</tt> <tt class="py-line"><tt class="py-docstring"></tt> </tt>
349<a name="L287"></a><tt class="py-lineno">287</tt> <tt class="py-line"><tt class="py-docstring"> node = YourTransformer().visit(node)</tt> </tt>
350<a name="L288"></a><tt class="py-lineno">288</tt> <tt class="py-line"><tt class="py-docstring"> """</tt> </tt>
351<a name="L289"></a><tt class="py-lineno">289</tt> <tt class="py-line"> </tt>
352<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>
353</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>
354<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>
355<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>
356<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>
357<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>
358<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>
359<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>
360<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>
361<a name="L299"></a><tt class="py-lineno">299</tt> <tt class="py-line"> <tt class="py-keyword">continue</tt> </tt>
362<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>
363<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>
364<a name="L302"></a><tt class="py-lineno">302</tt> <tt class="py-line"> <tt class="py-keyword">continue</tt> </tt>
365<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>
366<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>
367<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>
368<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>
369<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>
370<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>
371<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>
372<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>
373<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>
374</div></div><a name="L312"></a><tt class="py-lineno">312</tt> <tt class="py-line"> </tt><script type="text/javascript">
375<!--
376expandto(location.href);
377// -->
378</script>
379</pre>
380<br />
381<!-- ==================== NAVIGATION BAR ==================== -->
382<table class="navbar" border="0" width="100%" cellpadding="0"
383 bgcolor="#a0c0ff" cellspacing="0">
384 <tr valign="middle">
385 <!-- Home link -->
386 <th>&nbsp;&nbsp;&nbsp;<a
387 href="TestON-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>
388
389 <!-- Tree link -->
390 <th>&nbsp;&nbsp;&nbsp;<a
391 href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>
392
393 <!-- Index link -->
394 <th>&nbsp;&nbsp;&nbsp;<a
395 href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>
396
397 <!-- Help link -->
398 <th>&nbsp;&nbsp;&nbsp;<a
399 href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>
400
401 <th class="navbar" width="100%"></th>
402 </tr>
403</table>
404<table border="0" cellpadding="0" cellspacing="0" width="100%%">
405 <tr>
406 <td align="left" class="footer">
407 Generated by Epydoc 3.0.1 on Mon Mar 11 12:56:27 2013
408 </td>
409 <td align="right" class="footer">
410 <a target="mainFrame" href="http://epydoc.sourceforge.net"
411 >http://epydoc.sourceforge.net</a>
412 </td>
413 </tr>
414</table>
415
416<script type="text/javascript">
417 <!--
418 // Private objects are initially displayed (because if
419 // javascript is turned off then we want them to be
420 // visible); but by default, we want to hide them. So hide
421 // them unless we have a cookie that says to show them.
422 checkCookie();
423 // -->
424</script>
425</body>
426</html>