summaryrefslogtreecommitdiff
path: root/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.htm
blob: 1e6057be1d9d9f671413617ac930be459bcd8151 (plain)
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
<?xml version="1.0" encoding="iso-8859-1" ?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
<title>Example for syntax highlight with Pygments</title>
<style type="text/css">

/* Stylesheet for pygments enhanced reStructured Text */
/* ================================================== */

/* :Author:    Guenter Milde */
/* :Copyright: 2007 G. Milde */
/*             This stylesheet is released under the GPL v. 2 or later */

/* This stylesheet provides syntax highlight for documents generated with a */
/* pygments_ enhanced reStructured Text -> html converter. */

/* Import the default docutils style sheet */
/* --------------------------------------- */
/* :: */

@import url("/stylesheets/html4css1.css");         

/* Indent the code block */
/* --------------------- */

/* Content copied from the `html4css1.css` rule for literal blocks. */
/* Selector adapted to the output of Pygments_. :: */

div.highlight {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee
  }


/* Colour code blocks */
/* ------------------ */

/* Pygments_ has an option to generate stylesheets for html and latex. */
/* The following code is generated with the command */
/* `pygmentize -S default -f html > pygments-default.css`:: */

.c { color: #008800; font-style: italic } /* Comment */
.err { border: 1px solid #FF0000 } /* Error */
.k { color: #AA22FF; font-weight: bold } /* Keyword */
.o { color: #666666 } /* Operator */
.cm { color: #008800; font-style: italic } /* Comment.Multiline */
.cp { color: #008800 } /* Comment.Preproc */
.c1 { color: #008800; font-style: italic } /* Comment.Single */
.gd { color: #A00000 } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #FF0000 } /* Generic.Error */
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
.gi { color: #00A000 } /* Generic.Inserted */
.go { color: #808080 } /* Generic.Output */
.gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.gt { color: #0040D0 } /* Generic.Traceback */
.kc { color: #AA22FF; font-weight: bold } /* Keyword.Constant */
.kd { color: #AA22FF; font-weight: bold } /* Keyword.Declaration */
.kp { color: #AA22FF } /* Keyword.Pseudo */
.kr { color: #AA22FF; font-weight: bold } /* Keyword.Reserved */
.kt { color: #AA22FF; font-weight: bold } /* Keyword.Type */
.m { color: #666666 } /* Literal.Number */
.s { color: #BB4444 } /* Literal.String */
.na { color: #BB4444 } /* Name.Attribute */
.nb { color: #AA22FF } /* Name.Builtin */
.nc { color: #0000FF } /* Name.Class */
.no { color: #880000 } /* Name.Constant */
.nd { color: #AA22FF } /* Name.Decorator */
.ni { color: #999999; font-weight: bold } /* Name.Entity */
.ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.nf { color: #00A000 } /* Name.Function */
.nl { color: #A0A000 } /* Name.Label */
.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.nt { color: #008000; font-weight: bold } /* Name.Tag */
.nv { color: #B8860B } /* Name.Variable */
.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.mf { color: #666666 } /* Literal.Number.Float */
.mh { color: #666666 } /* Literal.Number.Hex */
.mi { color: #666666 } /* Literal.Number.Integer */
.mo { color: #666666 } /* Literal.Number.Oct */
.sb { color: #BB4444 } /* Literal.String.Backtick */
.sc { color: #BB4444 } /* Literal.String.Char */
.sd { color: #BB4444; font-style: italic } /* Literal.String.Doc */
.s2 { color: #BB4444 } /* Literal.String.Double */
.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.sh { color: #BB4444 } /* Literal.String.Heredoc */
.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.sx { color: #008000 } /* Literal.String.Other */
.sr { color: #BB6688 } /* Literal.String.Regex */
.s1 { color: #BB4444 } /* Literal.String.Single */
.ss { color: #B8860B } /* Literal.String.Symbol */
.bp { color: #AA22FF } /* Name.Builtin.Pseudo */
.vc { color: #B8860B } /* Name.Variable.Class */
.vg { color: #B8860B } /* Name.Variable.Global */
.vi { color: #B8860B } /* Name.Variable.Instance */
.il { color: #666666 } /* Literal.Number.Integer.Long */

/* .. _pygments:  http://pygments.org/ */

</style>
</head>
<body>
<div class="document" id="example-for-syntax-highlight-with-pygments">
<h1 class="title">Example for syntax highlight with Pygments</h1>
<p>Translate this document to HTML with a pygments enhanced frontend:</p>
<pre class="literal-block">
rst2html-pygments --stylesheet=pygments-default.css
</pre>
<p>or to LaTeX with:</p>
<pre class="literal-block">
rst2latex-pygments --stylesheet=pygments-default.sty
</pre>
<p>to gain syntax highlight in the output.</p>
<p>Convert between text &lt;-&gt; code source formats with:</p>
<pre class="literal-block">
pylit --code-block-marker='.. code-block:: python'
</pre>
<p>Run the doctests with:</p>
<pre class="literal-block">
pylit --doctest for-else-test.py
</pre>
<div class="section">
<h1><a id="for-else-test" name="for-else-test">for-else-test</a></h1>
<p>Test the flow in a <cite>for</cite> loop with <cite>else</cite> statement.</p>
<p>First define a simple <cite>for</cite> loop.</p>
<div class="highlight"><pre><span class="k">def</span> <span class="nf">loop1</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;simple for loop with `else` statement&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
        <span class="k">print</span> <span class="n">i</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;iterable empty&quot;</span>
    <span class="k">print</span> <span class="s">&quot;Ende&quot;</span>
</pre></div>
<p>Now test it:</p>
<p>The first test runs as I expect: iterator empty -&gt; else clause applies:</p>
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">execfile</span><span class="p">(</span><span class="s">&#39;for-else-test.py&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">loop1</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
<span class="go">iterable empty</span>
<span class="go">Ende</span>
</pre></div>
<p>However, the else clause even runs if the iterator is not empty in the first
place but after it is &quot;spent&quot;:</p>
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">loop1</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">0</span>
<span class="go">1</span>
<span class="go">2</span>
<span class="go">iterable empty</span>
<span class="go">Ende</span>
</pre></div>
<p>It seems like the else clause can only be prevented, if we break out of
the loop. Let's try</p>
<div class="highlight"><pre><span class="k">def</span> <span class="nf">loop2</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;for loop with `break` and `else` statement&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
        <span class="k">print</span> <span class="n">i</span>
        <span class="k">break</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;iterable empty&quot;</span>
    <span class="k">print</span> <span class="s">&quot;Ende&quot;</span>
</pre></div>
<p>And indeed, the else clause is skipped after breaking out of the loop:</p>
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">loop2</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">0</span>
<span class="go">Ende</span>
</pre></div>
<p>The empty iterator runs as expected:</p>
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">loop2</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
<span class="go">iterable empty</span>
<span class="go">Ende</span>
</pre></div>
</div>
</div>
<div class="footer">
<hr class="footer" />
Generated on: 2007-06-21.

</div>
</body>
</html>