<?xml version="1.0" ?>
<article>
<title>Simple Literate Program</title>
<sect1>
<title>Introduction</title>
<para>
This program is made to illustrate the concepts of literate programming.
This program receives as input a number, and then adds up all of the
numbers between one and that number, and then prints the output.
The outline for the program is as follows:
<programlisting>
<?lp-file id="mainlisting" file="example.py"?>
<?lp-options preserve-newlines="no"?>
<?lp-section-id?>Main Listing<?lp-section-id-end?> =
<?lp-code?>
#!/usr/bin/python
<?lp-ref?>{Import needed modules}<?lp-ref-end?>
<?lp-ref?>{Initialize variables}<?lp-ref-end?>
<?lp-ref?>{Read in a line of input}<?lp-ref-end?>
<?lp-ref?>{Add up numbers from one to the number given}<?lp-ref-end?>
<?lp-ref?>{Print results}<?lp-ref-end?>
<?lp-code-end?>
<?lp-options preserve-newlines="yes"?>
</programlisting>
</para>
</sect1>
<sect1>
<title>Adding up the numbers</title>
<para>
Since adding up the numbers is the purpose of the program, we will
dive into that section first. One of the benefits of literate programming
is that you can go in any order you please. Therefore, you can explain
the most important concepts first, even if they appear later in the source
file.
</para>
<para>
The code in this section will generate a range from 1 to
number + 1, and then iterate through and add them all to the
variable total.
<programlisting>
<?lp-section-id?>Add up numbers from one to the number given<?lp-section-id-end?> =
<?lp-code?>
for num in range(1, number + 1):
total = total + num
<?lp-code-end?>
</programlisting>
We now have a variable total that has not been initialized. Although we
could do it here because Python allows that, we will put all variable
initializations in the "Initialize variables" section to show some additional
features of Literate Programming. The total variable needs to be initialized
to 0.
<programlisting>
<?lp-section-id?>Initialize variables<?lp-section-id-end?> =
<?lp-code?>
total = 0
<?lp-code-end?>
</programlisting>
</para>
</sect1>
<sect1>
<title>Input and Output</title>
<para>
The input routine for this program is very basic - it simply attempts to
read a number from standard input and store it in the number variable.
To start with, we will initialize number to an invalid value.
<programlisting>
<?lp-section-id?>Initialize variables<?lp-section-id-end?> +=
<?lp-code?>
number = 0
<?lp-code-end?>
</programlisting>
Notice that this has been appended to the "Initialize variables" code segment
that was started in the previous section. Now, we need to read in the
number.
<programlisting>
<?lp-section-id?>Read in a line of input<?lp-section-id-end?> =
<?lp-code?>
print """Please enter a positive integer, and I will add all numbers between one and
that number"""
textline = sys.stdin.readline()
try:
number = string.atoi(textline)
if number == 0:
raise InvalidNumberError
except:
print "You typed an invalid number. Exitting..."
sys.exit(1)
<?lp-code-end?>
</programlisting>
That code needs to have the string module and the sys module imported.
Therefore, we will add the imports to the beginning of the source file.
<programlisting>
<?lp-section-id?>Import needed modules<?lp-section-id-end?> =
<?lp-code?>
import sys, string
<?lp-code-end?>
</programlisting>
Finally, when the code is finished, we need to print the results.
<programlisting>
<?lp-section-id?>Print results<?lp-section-id-end?>
<?lp-code?>
print "When you add all the numbers from one to ", number, ", the result is ", total, "."
<?lp-code-end?>
</programlisting>
</para>
</sect1>
</article>
|