1 .. #!/usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
5 :Version: SVN-Revision $Revision$
7 :Copyright: 2006 Guenter Milde.
8 Released without any warranty under the terms of the
9 GNU General Public License (v. 2 or later)
17 As this module is not loaded when the file is tested with
18 ``pylit --doctest``, the first doctest must import it before any of its
20 An elegant solution is to give a usage example in the module's docstring::
23 This is the "testfile_literate" module.
25 It supplies one function, `factorial()` that returns the factorial of its
28 >>> import testfile_literate
29 >>> testfile_literate.factorial(5)
34 __docformat__ = 'restructuredtext'
37 .. attention:: As the docstring is not parsed as separate unit but as part of the
38 file, there must be a blank line also after the last doctest block.
39 Otherwise `doctest` expects ``"""`` to be part of the output.
41 Alternatives for easier access to the defined objects are:
43 >>> import testfile_literate as fac
47 >>> from testfile_literate import *
51 This imports the *code source* of the literal script:
53 * ``testfile_literate.py`` must be present in the PYTHONPATH or the current
56 * The doctest examples in the file argument to ``pylit --doctest`` (be it
57 text source or code source) are tested with the code definitions in the
58 last saved version of the code source.
64 The functions docstring can be kept concise and additional discussion
65 referred to the text part of the literate source::
68 """Return the factorial of `n`, an exact integer >= 0.
70 >>> [factorial(n) for n in range(6)]
73 265252859812191058636308480000000L
75 Factorials of floats are OK, but the float must be an exact integer:
78 265252859812191058636308480000000L
84 raise ValueError("n must be >= 0")
85 if math.floor(n) != n:
86 raise ValueError("n must be exact integer")
87 if n+1 == n: # catch a value like 1e300
88 raise OverflowError("n too large")
100 `factorial()` accepts input as int, long or float:
103 265252859812191058636308480000000L
105 265252859812191058636308480000000L
107 265252859812191058636308480000000L
109 However, the float must be an exact integer and it must also not be
113 Traceback (most recent call last):
115 ValueError: n must be exact integer
118 Traceback (most recent call last):
120 OverflowError: n too large
122 The factorial of negative values is not defined:
125 Traceback (most recent call last):
127 ValueError: n must be >= 0
129 The type of the return value depends on the size of the result.
131 If the result is small enough to fit in an int, return an int.
134 >>> [factorial(n) for n in range(6)]
135 [1, 1, 2, 6, 24, 120]
136 >>> [factorial(long(n)) for n in range(6)]
137 [1, 1, 2, 6, 24, 120]
139 265252859812191058636308480000000L
141 265252859812191058636308480000000L
147 The script is tested by calling ``pylit --doctest testfile_literate.py``
148 or ``pylit --doctest testfile_literate.py.txt``.
150 This is especially handy for scripts that should perform some default
151 action other than a self test, e.g.
153 Print the first 10 natural numbers and their factorials if
154 called as `__main__` (i.e. from the command line)::
156 if __name__ == "__main__":
159 print n, factorial(n)