MCPcopy Index your code
hub / github.com/python/cpython / script_from_examples

Function script_from_examples

Lib/doctest.py:2719–2799  ·  view source on GitHub ↗

r"""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.

(s)

Source from the content-addressed store, hash-verified

2717######################################################################
2718
2719def script_from_examples(s):
2720 r"""Extract script from text with examples.
2721
2722 Converts text with examples to a Python script. Example input is
2723 converted to regular code. Example output and all other words
2724 are converted to comments:
2725
2726 >>> text = '''
2727 ... Here are examples of simple math.
2728 ...
2729 ... Python has super accurate integer addition
2730 ...
2731 ... >>> 2 + 2
2732 ... 5
2733 ...
2734 ... And very friendly error messages:
2735 ...
2736 ... >>> 1/0
2737 ... To Infinity
2738 ... And
2739 ... Beyond
2740 ...
2741 ... You can use logic if you want:
2742 ...
2743 ... >>> if 0:
2744 ... ... blah
2745 ... ... blah
2746 ... ...
2747 ...
2748 ... Ho hum
2749 ... '''
2750
2751 >>> print(script_from_examples(text))
2752 # Here are examples of simple math.
2753 #
2754 # Python has super accurate integer addition
2755 #
2756 2 + 2
2757 # Expected:
2758 ## 5
2759 #
2760 # And very friendly error messages:
2761 #
2762 1/0
2763 # Expected:
2764 ## To Infinity
2765 ## And
2766 ## Beyond
2767 #
2768 # You can use logic if you want:
2769 #
2770 if 0:
2771 blah
2772 blah
2773 #
2774 # Ho hum
2775 <BLANKLINE>
2776 """

Callers 2

testsourceFunction · 0.85
debug_srcFunction · 0.85

Calls 7

DocTestParserClass · 0.85
_comment_lineFunction · 0.85
parseMethod · 0.45
appendMethod · 0.45
splitMethod · 0.45
popMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…