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)
14 The module docstring should give a concise description of the working,
15 details are in the literate source so the docstrings are not bloated::
18 This is the "testmod_literate" module.
20 It supplies one function, `factorial()`. For example,
27 __docformat__ = 'restructuredtext'
30 **Beware:** as the docstring is not parsed as separate unit but as part of
31 the file, there must be a blank line also after the last doctest block.
32 Otherwise `doctest` expects ``"""`` to be part of the output.
37 The functions docstring can be kept concise and additional discussion
38 referred to the text part of the literate source::
41 """Return the factorial of `n`, an exact integer >= 0.
43 >>> [factorial(n) for n in range(6)]
46 265252859812191058636308480000000L
48 Factorials of floats are OK, but the float must be an exact integer:
51 265252859812191058636308480000000L
57 raise ValueError("n must be >= 0")
58 if math.floor(n) != n:
59 raise ValueError("n must be exact integer")
60 if n+1 == n: # catch a value like 1e300
61 raise OverflowError("n too large")
73 `factorial()` accepts input as int, long or float:
76 265252859812191058636308480000000L
78 265252859812191058636308480000000L
80 265252859812191058636308480000000L
82 However, the float must be an exact integer and it must also not be
86 Traceback (most recent call last):
88 ValueError: n must be exact integer
91 Traceback (most recent call last):
93 OverflowError: n too large
95 The factorial of negative values is not defined:
98 Traceback (most recent call last):
100 ValueError: n must be >= 0
102 The type of the return value depends on the size of the result.
104 If the result is small enough to fit in an int, return an int.
107 >>> [factorial(n) for n in range(6)]
108 [1, 1, 2, 6, 24, 120]
109 >>> [factorial(long(n)) for n in range(6)]
110 [1, 1, 2, 6, 24, 120]
112 265252859812191058636308480000000L
114 265252859812191058636308480000000L
120 The traditional test function parses the docstrings of all objects in this
121 module. It misses doctests in comments::
127 Test all doctest blocks (both in docstrings and in text parts (well
128 formatted comments) if the module is called as `__main__` (i.e. from the
131 def _test_all_doctests():
133 pylit.run_doctest(infile=sys.argv[0], txt2code=False,
134 globs=sys.modules.get('__main__').__dict__)
136 (Future versions of `pylit` might contain a convenience function for a simpler
137 invocation of this test.)
139 Doctests can still be disabled or commented - make sure they are not
140 recognised as text block (no double colon here):
146 or (with non-canonical comments)::
152 Doctests in doc-strings can be skipped with the `strip` option::
154 def _test_text_doctests():
156 pylit.run_doctest(infile=sys.argv[0], txt2code=False, strip=True
157 globs=sys.modules.get('__main__').__dict__)
163 if __name__ == "__main__":