PyDocs

Nav

Development Tools

script_from_examples

doctest

function (source)
  • script_from_examples(s)
Extract script from text with examples.

Converts text with examples to a Python script.  Example input is
converted to regular code.  Example output and all other words
are converted to comments:

>>> text = '''
...       Here are examples of simple math.
...
...           Python has super accurate integer addition
...
...           >>> 2 + 2
...           5
...
...           And very friendly error messages:
...
...           >>> 1/0
...           To Infinity
...           And
...           Beyond
...
...           You can use logic if you want:
...
...           >>> if 0:
...           ...    blah
...           ...    blah
...           ...
...
...           Ho hum
...           '''

>>> print(script_from_examples(text))
# Here are examples of simple math.
#
#     Python has super accurate integer addition
#
2 + 2
# Expected:
## 5
#
#     And very friendly error messages:
#
1/0
# Expected:
## To Infinity
## And
## Beyond
#
#     You can use logic if you want:
#
if 0:
   blah
   blah
#
#     Ho hum
<BLANKLINE>
0 Examples
No examples for doctest/script_from_examples.
See Also
No see-alsos for doctest/script_from_examples
0 Notes
No notes for script_from_examples