From 098c62623c8bf93b53bccd2271ac5fcf9bcff52a Mon Sep 17 00:00:00 2001 From: wiemann Date: Fri, 29 Jun 2007 03:29:36 +0000 Subject: renamed .html to .htm to avoid collisions with the docutils-update website updater script; it was sending me email because the code-block directive caused error messages; this is kindof a hack, but it should work for now; Guenther, if the changed extensions are a problem for you, please feel free to revert (or drop me a line) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5305 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- .../code-block-directive/docs/for-else-test.py.htm | 185 +++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 sandbox/code-block-directive/docs/for-else-test.py.htm (limited to 'sandbox/code-block-directive/docs/for-else-test.py.htm') diff --git a/sandbox/code-block-directive/docs/for-else-test.py.htm b/sandbox/code-block-directive/docs/for-else-test.py.htm new file mode 100644 index 000000000..1e6057be1 --- /dev/null +++ b/sandbox/code-block-directive/docs/for-else-test.py.htm @@ -0,0 +1,185 @@ + + + + + + +Example for syntax highlight with Pygments + + + +
+

Example for syntax highlight with Pygments

+

Translate this document to HTML with a pygments enhanced frontend:

+
+rst2html-pygments --stylesheet=pygments-default.css
+
+

or to LaTeX with:

+
+rst2latex-pygments --stylesheet=pygments-default.sty
+
+

to gain syntax highlight in the output.

+

Convert between text <-> code source formats with:

+
+pylit --code-block-marker='.. code-block:: python'
+
+

Run the doctests with:

+
+pylit --doctest for-else-test.py
+
+
+

for-else-test

+

Test the flow in a for loop with else statement.

+

First define a simple for loop.

+
def loop1(iterable):
+    """simple for loop with `else` statement"""
+    for i in iterable:
+        print i
+    else:
+        print "iterable empty"
+    print "Ende"
+
+

Now test it:

+

The first test runs as I expect: iterator empty -> else clause applies:

+
>>> execfile('for-else-test.py')
+>>> loop1(range(0))
+iterable empty
+Ende
+
+

However, the else clause even runs if the iterator is not empty in the first +place but after it is "spent":

+
>>> loop1(range(3))
+0
+1
+2
+iterable empty
+Ende
+
+

It seems like the else clause can only be prevented, if we break out of +the loop. Let's try

+
def loop2(iterable):
+    """for loop with `break` and `else` statement"""
+    for i in iterable:
+        print i
+        break
+    else:
+        print "iterable empty"
+    print "Ende"
+
+

And indeed, the else clause is skipped after breaking out of the loop:

+
>>> loop2(range(3))
+0
+Ende
+
+

The empty iterator runs as expected:

+
>>> loop2(range(0))
+iterable empty
+Ende
+
+
+
+ + + -- cgit v1.2.1