##// END OF EJS Templates
Fix manual section about pasting code with prompts.
Fernando Perez -
Show More
@@ -1070,55 +1070,44 b" supplied, which we will briefly describe now. These can be used 'as is'"
1070 (and both provide very useful functionality), or you can use them as a
1070 (and both provide very useful functionality), or you can use them as a
1071 starting point for writing your own extensions.
1071 starting point for writing your own extensions.
1072
1072
1073 .. _pasting_with_prompts:
1073
1074
1074 Pasting of code starting with '>>> ' or '... '
1075 Pasting of code starting with Python or IPython prompts
1075 ----------------------------------------------
1076 -------------------------------------------------------
1076
1077
1077 In the python tutorial it is common to find code examples which have
1078 IPython is smart enough to filter out input prompts, be they plain Python ones
1078 been taken from real python sessions. The problem with those is that all
1079 (``>>>`` and ``...``) or IPython ones (``In [N]:`` and `` ...:``). You can
1079 the lines begin with either '>>> ' or '... ', which makes it impossible
1080 therefore copy and paste from existing interactive sessions without worry.
1080 to paste them all at once. One must instead do a line by line manual
1081
1081 copying, carefully removing the leading extraneous characters.
1082 The following is a 'screenshot' of how things work, copying an example from the
1082
1083 standard Python tutorial::
1083 This extension identifies those starting characters and removes them
1084
1084 from the input automatically, so that one can paste multi-line examples
1085 In [1]: >>> # Fibonacci series:
1085 directly into IPython, saving a lot of time. Please look at the file
1086
1086 InterpreterPasteInput.py in the IPython/extensions directory for details
1087 In [2]: ... # the sum of two elements defines the next
1087 on how this is done.
1088
1088
1089 In [3]: ... a, b = 0, 1
1089 IPython comes with a special profile enabling this feature, called
1090
1090 tutorial. Simply start IPython via 'ipython -p tutorial' and the feature
1091 In [4]: >>> while b < 10:
1091 will be available. In a normal IPython session you can activate the
1092 ...: ... print b
1092 feature by importing the corresponding module with:
1093 ...: ... a, b = b, a+b
1093 In [1]: import IPython.extensions.InterpreterPasteInput
1094 ...:
1094
1095 1
1095 The following is a 'screenshot' of how things work when this extension
1096 1
1096 is on, copying an example from the standard tutorial::
1097 2
1097
1098 3
1098 IPython profile: tutorial
1099 5
1099
1100 8
1100 *** Pasting of code with ">>>" or "..." has been enabled.
1101
1101
1102 And pasting from IPython sessions works equally well::
1102 In [1]: >>> def fib2(n): # return Fibonacci series up to n
1103
1103 ...: ... """Return a list containing the Fibonacci series up to
1104 In [1]: In [5]: def f(x):
1104 n."""
1105 ...: ...: "A simple function"
1105 ...: ... result = []
1106 ...: ...: return x**2
1106 ...: ... a, b = 0, 1
1107 ...: ...:
1107 ...: ... while b < n:
1108
1108 ...: ... result.append(b) # see below
1109 In [2]: f(3)
1109 ...: ... a, b = b, a+b
1110 Out[2]: 9
1110 ...: ... return result
1111 ...:
1112
1113 In [2]: fib2(10)
1114 Out[2]: [1, 1, 2, 3, 5, 8]
1115
1116 Note that as currently written, this extension does not recognize
1117 IPython's prompts for pasting. Those are more complicated, since the
1118 user can change them very easily, they involve numbers and can vary in
1119 length. One could however extract all the relevant information from the
1120 IPython instance and build an appropriate regular expression. This is
1121 left as an exercise for the reader.
1122
1111
1123 .. _gui_support:
1112 .. _gui_support:
1124
1113
@@ -110,6 +110,10 b' A quick summary of the major changes (see below for details):'
110 can have its attributes set either via files that now use real Python syntax
110 can have its attributes set either via files that now use real Python syntax
111 or from the command-line.
111 or from the command-line.
112
112
113 * **Pasting of code with prompts**. IPython now intelligently strips out input
114 prompts , be they plain Python ones (``>>>`` and ``...``) or IPython ones
115 (``In [N]:`` and `` ...:``). More details :ref:`here <pasting_with_prompts>`.
116
113
117
114 Authors and support
118 Authors and support
115 -------------------
119 -------------------
General Comments 0
You need to be logged in to leave comments. Login now