/usr/share/doc/python3-manuel/html/table-example.html is in python3-manuel 1.8.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FIT Table Example — Manuel Documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Manuel Documentation" href="index.html" />
<link rel="next" title="Fixed Bugs" href="bugs.html" />
<link rel="prev" title="Theory of Operation" href="README.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="fit-table-example">
<span id="id1"></span><h1>FIT Table Example<a class="headerlink" href="#fit-table-example" title="Permalink to this headline">¶</a></h1>
<p>Here is an example of writing a relatively complex Manuel plug-in.</p>
<p>Occasionally when writing a doctest, you want a better way to express a test
than doctest by itself provides.</p>
<p>For example, you may want to succinctly express the result of an expression for
several sets of inputs and outputs.</p>
<p>That’s something <a class="reference external" href="http://fit.c2.com/wiki.cgi?SimpleExample">FIT</a> tables do a
good job of.</p>
<p>We can use Manuel to write a parser that can read the tables, an evaluator that
can check to see if the assertions made in the tables match reality, and a
formatter to display the results if they don’t.</p>
<p>We’ll use <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reST</a> tables as the
table format. The table source will look like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
\ <span class="n">A</span> <span class="ow">or</span> <span class="n">B</span>
<span class="o">--------------------</span>
<span class="n">A</span> <span class="n">B</span> <span class="n">Result</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
<span class="kc">False</span> <span class="kc">False</span> <span class="kc">False</span>
<span class="kc">True</span> <span class="kc">False</span> <span class="kc">True</span>
<span class="kc">False</span> <span class="kc">True</span> <span class="kc">True</span>
<span class="kc">True</span> <span class="kc">True</span> <span class="kc">True</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
</pre></div>
</div>
<p>When rendered to HTML, it will look like this:</p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="31%" />
<col width="31%" />
<col width="38%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head" colspan="3"> A or B</th>
</tr>
<tr class="row-even"><th class="head">A</th>
<th class="head">B</th>
<th class="head">Result</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-odd"><td>False</td>
<td>False</td>
<td>False</td>
</tr>
<tr class="row-even"><td>True</td>
<td>False</td>
<td>True</td>
</tr>
<tr class="row-odd"><td>False</td>
<td>True</td>
<td>True</td>
</tr>
<tr class="row-even"><td>True</td>
<td>True</td>
<td>True</td>
</tr>
</tbody>
</table>
</div></blockquote>
<div class="section" id="documents">
<h2>Documents<a class="headerlink" href="#documents" title="Permalink to this headline">¶</a></h2>
<p>Here is an example of a source document we want our plug-in to be able to
understand:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">The</span> <span class="s2">"or"</span> <span class="n">operator</span>
<span class="o">=================</span>
<span class="n">Here</span> <span class="ow">is</span> <span class="n">an</span> <span class="n">example</span> <span class="n">of</span> <span class="n">the</span> <span class="s2">"or"</span> <span class="n">operator</span> <span class="ow">in</span> <span class="n">action</span><span class="p">:</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
\ <span class="n">A</span> <span class="ow">or</span> <span class="n">B</span>
<span class="o">--------------------</span>
<span class="n">A</span> <span class="n">B</span> <span class="n">Result</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
<span class="kc">False</span> <span class="kc">False</span> <span class="kc">False</span>
<span class="kc">True</span> <span class="kc">False</span> <span class="kc">True</span>
<span class="kc">False</span> <span class="kc">True</span> <span class="kc">True</span>
<span class="kc">True</span> <span class="kc">True</span> <span class="kc">True</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
</pre></div>
</div>
<p>Manuel plug-ins operate on instances of <code class="xref py py-class docutils literal"><span class="pre">manuel.Document</span></code>.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">manuel</span>
<span class="n">document</span> <span class="o">=</span> <span class="n">manuel</span><span class="o">.</span><span class="n">Document</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="s1">'fake.txt'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="parsing">
<h2>Parsing<a class="headerlink" href="#parsing" title="Permalink to this headline">¶</a></h2>
<p>We need an object to represent the tables.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Table</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">examples</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span>
<span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="n">variables</span>
<span class="bp">self</span><span class="o">.</span><span class="n">examples</span> <span class="o">=</span> <span class="n">examples</span>
</pre></div>
</div>
<p>We’ll also need a function to find the tables in the document, extract the
pertinent details, and instantiate Table objects.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">six</span>
<span class="n">table_start</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">r'(?<=\n\n)=[= ]+\n(?=[ \t]*?\S)'</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
<span class="n">table_end</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">r'\n=[= ]+\n(?=\Z|\n)'</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">parse_tables</span><span class="p">(</span><span class="n">document</span><span class="p">):</span>
<span class="k">for</span> <span class="n">region</span> <span class="ow">in</span> <span class="n">document</span><span class="o">.</span><span class="n">find_regions</span><span class="p">(</span><span class="n">table_start</span><span class="p">,</span> <span class="n">table_end</span><span class="p">):</span>
<span class="n">lines</span> <span class="o">=</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="n">region</span><span class="o">.</span><span class="n">source</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()))</span>
<span class="n">six</span><span class="o">.</span><span class="n">advance_iterator</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="c1"># skip the first line</span>
<span class="c1"># grab the expression to be evaluated</span>
<span class="n">expression</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">advance_iterator</span><span class="p">(</span><span class="n">lines</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
<span class="k">if</span> <span class="n">expression</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">'</span><span class="se">\\</span><span class="s1">'</span><span class="p">):</span>
<span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="n">six</span><span class="o">.</span><span class="n">advance_iterator</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="c1"># skip the divider line</span>
<span class="n">variables</span> <span class="o">=</span> <span class="p">[</span><span class="n">v</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">advance_iterator</span><span class="p">(</span><span class="n">lines</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()][:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">six</span><span class="o">.</span><span class="n">advance_iterator</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="c1"># skip the divider line</span>
<span class="n">examples</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">lineno_offset</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
<span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">'='</span><span class="p">):</span>
<span class="k">break</span> <span class="c1"># we ran into the final divider, so stop</span>
<span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="nb">eval</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">strip</span><span class="p">(),</span> <span class="p">{})</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>
<span class="n">inputs</span> <span class="o">=</span> <span class="n">values</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">values</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">examples</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">inputs</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">lineno_offset</span><span class="p">))</span>
<span class="n">table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">examples</span><span class="p">)</span>
<span class="n">document</span><span class="o">.</span><span class="n">claim_region</span><span class="p">(</span><span class="n">region</span><span class="p">)</span>
<span class="n">region</span><span class="o">.</span><span class="n">parsed</span> <span class="o">=</span> <span class="n">table</span>
</pre></div>
</div>
<p>If we parse the Document we can see that the table was recognized.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">parse_tables</span><span class="p">(</span><span class="n">document</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">region</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">document</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">six</span>
<span class="gp">>>> </span><span class="n">six</span><span class="o">.</span><span class="n">print_</span><span class="p">(</span><span class="n">region</span><span class="o">.</span><span class="n">source</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">''</span><span class="p">)</span>
<span class="go">===== ===== ======</span>
<span class="go">\ A or B</span>
<span class="go">--------------------</span>
<span class="go"> A B Result</span>
<span class="go">===== ===== ======</span>
<span class="go">False False False</span>
<span class="go">True False True</span>
<span class="go">False True True</span>
<span class="go">True True True</span>
<span class="go">===== ===== ======</span>
<span class="gp">>>> </span><span class="n">region</span><span class="o">.</span><span class="n">parsed</span>
<span class="go"><Table object at ...></span>
</pre></div>
</div>
</div>
<div class="section" id="evaluating">
<h2>Evaluating<a class="headerlink" href="#evaluating" title="Permalink to this headline">¶</a></h2>
<p>Now that we can find and extract the tables from the source, we need to be able
to check them for correctness.</p>
<p>The parse phase decomposed the <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> into several <code class="xref py py-class docutils literal"><span class="pre">Region</span></code>
instances. During the evaluation phase each evaluater is called once for each
region.</p>
<p>The evaluate_table function iterates over each set of inputs given in a single
table, evaluate the inputs with the expression and compare the result with what
was expected. Each discrepancy will be stored as a <code class="xref py py-class docutils literal"><span class="pre">TableError</span></code> in a
<code class="xref py py-class docutils literal"><span class="pre">TableErrors</span></code> object.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TableErrors</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">class</span> <span class="nc">TableError</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">location</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span> <span class="n">expected</span><span class="p">,</span> <span class="n">got</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">location</span> <span class="o">=</span> <span class="n">location</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lineno</span> <span class="o">=</span> <span class="n">lineno</span>
<span class="bp">self</span><span class="o">.</span><span class="n">expected</span> <span class="o">=</span> <span class="n">expected</span>
<span class="bp">self</span><span class="o">.</span><span class="n">got</span> <span class="o">=</span> <span class="n">got</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s1">'<</span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">>'</span> <span class="o">%</span> <span class="p">(</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">location</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lineno</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">evaluate_table</span><span class="p">(</span><span class="n">region</span><span class="p">,</span> <span class="n">document</span><span class="p">,</span> <span class="n">globs</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">region</span><span class="o">.</span><span class="n">parsed</span><span class="p">,</span> <span class="n">Table</span><span class="p">):</span>
<span class="k">return</span>
<span class="n">table</span> <span class="o">=</span> <span class="n">region</span><span class="o">.</span><span class="n">parsed</span>
<span class="n">errors</span> <span class="o">=</span> <span class="n">TableErrors</span><span class="p">()</span>
<span class="k">for</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">lineno_offset</span> <span class="ow">in</span> <span class="n">table</span><span class="o">.</span><span class="n">examples</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">table</span><span class="o">.</span><span class="n">expression</span><span class="p">,</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">table</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="n">inputs</span><span class="p">)))</span>
<span class="k">if</span> <span class="n">result</span> <span class="o">!=</span> <span class="n">output</span><span class="p">:</span>
<span class="n">lineno</span> <span class="o">=</span> <span class="n">region</span><span class="o">.</span><span class="n">lineno</span> <span class="o">+</span> <span class="n">lineno_offset</span>
<span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="n">TableError</span><span class="p">(</span><span class="n">document</span><span class="o">.</span><span class="n">location</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">result</span><span class="p">))</span>
<span class="n">region</span><span class="o">.</span><span class="n">evaluated</span> <span class="o">=</span> <span class="n">errors</span>
</pre></div>
</div>
<p>Now we can use the function to evaluate our table.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">evaluate_table</span><span class="p">(</span><span class="n">region</span><span class="p">,</span> <span class="n">document</span><span class="p">,</span> <span class="p">{})</span>
</pre></div>
</div>
<p>Yay! There were no errors:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">region</span><span class="o">.</span><span class="n">evaluated</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>What would happen if there were errors?</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">The</span> <span class="s2">"or"</span> <span class="n">operator</span>
<span class="o">=================</span>
<span class="n">Here</span> <span class="ow">is</span> <span class="n">an</span> <span class="p">(</span><span class="n">erroneous</span><span class="p">)</span> <span class="n">example</span> <span class="n">of</span> <span class="n">the</span> <span class="s2">"or"</span> <span class="n">operator</span> <span class="ow">in</span> <span class="n">action</span><span class="p">:</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
\ <span class="n">A</span> <span class="ow">or</span> <span class="n">B</span>
<span class="o">--------------------</span>
<span class="n">A</span> <span class="n">B</span> <span class="n">Result</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
<span class="kc">False</span> <span class="kc">False</span> <span class="kc">True</span>
<span class="kc">True</span> <span class="kc">False</span> <span class="kc">True</span>
<span class="kc">False</span> <span class="kc">True</span> <span class="kc">False</span>
<span class="kc">True</span> <span class="kc">True</span> <span class="kc">True</span>
<span class="o">=====</span> <span class="o">=====</span> <span class="o">======</span>
</pre></div>
</div>
<p>...the result of evaluaton would include them:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">region</span><span class="o">.</span><span class="n">evaluated</span>
<span class="go">[<TableError object at ...>]</span>
</pre></div>
</div>
</div>
<div class="section" id="formatting-errors">
<h2>Formatting Errors<a class="headerlink" href="#formatting-errors" title="Permalink to this headline">¶</a></h2>
<p>Now that we can parse the tables and evaluate them, we need to be able to
display the results in a readable fashion.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">format_table_errors</span><span class="p">(</span><span class="n">document</span><span class="p">):</span>
<span class="k">for</span> <span class="n">region</span> <span class="ow">in</span> <span class="n">document</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">region</span><span class="o">.</span><span class="n">evaluated</span><span class="p">,</span> <span class="n">TableErrors</span><span class="p">):</span>
<span class="k">continue</span>
<span class="c1"># if there were no errors, there is nothing to report</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">region</span><span class="o">.</span><span class="n">evaluated</span><span class="p">:</span>
<span class="k">continue</span>
<span class="n">messages</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">error</span> <span class="ow">in</span> <span class="n">region</span><span class="o">.</span><span class="n">evaluated</span><span class="p">:</span>
<span class="n">messages</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">'</span><span class="si">%s</span><span class="s1">, line </span><span class="si">%d</span><span class="s1">: expected </span><span class="si">%r</span><span class="s1">, got </span><span class="si">%r</span><span class="s1"> instead.'</span> <span class="o">%</span> <span class="p">(</span>
<span class="n">error</span><span class="o">.</span><span class="n">location</span><span class="p">,</span> <span class="n">error</span><span class="o">.</span><span class="n">lineno</span><span class="p">,</span> <span class="n">error</span><span class="o">.</span><span class="n">expected</span><span class="p">,</span> <span class="n">error</span><span class="o">.</span><span class="n">got</span><span class="p">))</span>
<span class="n">sep</span> <span class="o">=</span> <span class="s1">'</span><span class="se">\n</span><span class="s1"> '</span>
<span class="n">header</span> <span class="o">=</span> <span class="s1">'when evaluating table at </span><span class="si">%s</span><span class="s1">, line </span><span class="si">%d</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span>
<span class="n">document</span><span class="o">.</span><span class="n">location</span><span class="p">,</span> <span class="n">region</span><span class="o">.</span><span class="n">lineno</span><span class="p">)</span>
<span class="n">region</span><span class="o">.</span><span class="n">formatted</span> <span class="o">=</span> <span class="n">header</span> <span class="o">+</span> <span class="n">sep</span> <span class="o">+</span> <span class="n">sep</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">messages</span><span class="p">)</span>
</pre></div>
</div>
<p>We can see how the results are formatted.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">format_table_errors</span><span class="p">(</span><span class="n">document</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">six</span><span class="o">.</span><span class="n">print_</span><span class="p">(</span><span class="n">region</span><span class="o">.</span><span class="n">formatted</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">''</span><span class="p">)</span>
<span class="go">when evaluating table at fake.txt, line 6</span>
<span class="go"> fake.txt, line 11: expected True, got False instead.</span>
<span class="go"> fake.txt, line 13: expected False, got True instead.</span>
</pre></div>
</div>
</div>
<div class="section" id="all-together-now">
<h2>All Together Now<a class="headerlink" href="#all-together-now" title="Permalink to this headline">¶</a></h2>
<p>All the pieces (parsing, evaluating, and formatting) are available now, so we
just have to put them together into a single “Manuel” object.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Manuel</span><span class="p">(</span><span class="n">manuel</span><span class="o">.</span><span class="n">Manuel</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">manuel</span><span class="o">.</span><span class="n">Manuel</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="p">[</span><span class="n">parse_tables</span><span class="p">],</span> <span class="p">[</span><span class="n">evaluate_table</span><span class="p">],</span>
<span class="p">[</span><span class="n">format_table_errors</span><span class="p">])</span>
</pre></div>
</div>
<p>Now we can create a fresh document and tell it to do all the above steps
(parse, evaluate, format) using an instance of our plug-in.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">Manuel</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">document</span> <span class="o">=</span> <span class="n">manuel</span><span class="o">.</span><span class="n">Document</span><span class="p">(</span><span class="n">source_with_errors</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="s1">'fake.txt'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">document</span><span class="o">.</span><span class="n">process_with</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">globs</span><span class="o">=</span><span class="p">{})</span>
<span class="gp">>>> </span><span class="n">six</span><span class="o">.</span><span class="n">print_</span><span class="p">(</span><span class="n">document</span><span class="o">.</span><span class="n">formatted</span><span class="p">(),</span> <span class="n">end</span><span class="o">=</span><span class="s1">''</span><span class="p">)</span>
<span class="go">when evaluating table at fake.txt, line 6</span>
<span class="go"> fake.txt, line 11: expected True, got False instead.</span>
<span class="go"> fake.txt, line 13: expected False, got True instead.</span>
</pre></div>
</div>
<p>Of course, if there were no errors, nothing would be reported:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">document</span> <span class="o">=</span> <span class="n">manuel</span><span class="o">.</span><span class="n">Document</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="s1">'fake.txt'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">document</span><span class="o">.</span><span class="n">process_with</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">globs</span><span class="o">=</span><span class="p">{})</span>
<span class="gp">>>> </span><span class="n">six</span><span class="o">.</span><span class="n">print_</span><span class="p">(</span><span class="n">document</span><span class="o">.</span><span class="n">formatted</span><span class="p">())</span>
</pre></div>
</div>
<p>If we wanted to use instances of our Manuel object in a test, we would follow
the directions in <a class="reference internal" href="index.html#getting-started"><span class="std std-ref">Getting Started</span></a>, importing Manuel from the module
where we placed the code, just like any other Manuel plug-in.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">FIT Table Example</a><ul>
<li><a class="reference internal" href="#documents">Documents</a></li>
<li><a class="reference internal" href="#parsing">Parsing</a></li>
<li><a class="reference internal" href="#evaluating">Evaluating</a></li>
<li><a class="reference internal" href="#formatting-errors">Formatting Errors</a></li>
<li><a class="reference internal" href="#all-together-now">All Together Now</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="README.html" title="previous chapter">Theory of Operation</a></li>
<li>Next: <a href="bugs.html" title="next chapter">Fixed Bugs</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/table-example.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©Benji York.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.4</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/table-example.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|