diff --git a/docs/source/interactive/reference.txt b/docs/source/interactive/reference.txt index 11f68a8..49399cf 100644 --- a/docs/source/interactive/reference.txt +++ b/docs/source/interactive/reference.txt @@ -1070,55 +1070,44 @@ supplied, which we will briefly describe now. These can be used 'as is' (and both provide very useful functionality), or you can use them as a starting point for writing your own extensions. +.. _pasting_with_prompts: -Pasting of code starting with '>>> ' or '... ' ----------------------------------------------- - -In the python tutorial it is common to find code examples which have -been taken from real python sessions. The problem with those is that all -the lines begin with either '>>> ' or '... ', which makes it impossible -to paste them all at once. One must instead do a line by line manual -copying, carefully removing the leading extraneous characters. - -This extension identifies those starting characters and removes them -from the input automatically, so that one can paste multi-line examples -directly into IPython, saving a lot of time. Please look at the file -InterpreterPasteInput.py in the IPython/extensions directory for details -on how this is done. - -IPython comes with a special profile enabling this feature, called -tutorial. Simply start IPython via 'ipython -p tutorial' and the feature -will be available. In a normal IPython session you can activate the -feature by importing the corresponding module with: -In [1]: import IPython.extensions.InterpreterPasteInput - -The following is a 'screenshot' of how things work when this extension -is on, copying an example from the standard tutorial:: - - IPython profile: tutorial - - *** Pasting of code with ">>>" or "..." has been enabled. - - In [1]: >>> def fib2(n): # return Fibonacci series up to n - ...: ... """Return a list containing the Fibonacci series up to - n.""" - ...: ... result = [] - ...: ... a, b = 0, 1 - ...: ... while b < n: - ...: ... result.append(b) # see below - ...: ... a, b = b, a+b - ...: ... return result - ...: - - In [2]: fib2(10) - Out[2]: [1, 1, 2, 3, 5, 8] - -Note that as currently written, this extension does not recognize -IPython's prompts for pasting. Those are more complicated, since the -user can change them very easily, they involve numbers and can vary in -length. One could however extract all the relevant information from the -IPython instance and build an appropriate regular expression. This is -left as an exercise for the reader. +Pasting of code starting with Python or IPython prompts +------------------------------------------------------- + +IPython is smart enough to filter out input prompts, be they plain Python ones +(``>>>`` and ``...``) or IPython ones (``In [N]:`` and `` ...:``). You can +therefore copy and paste from existing interactive sessions without worry. + +The following is a 'screenshot' of how things work, copying an example from the +standard Python tutorial:: + + In [1]: >>> # Fibonacci series: + + In [2]: ... # the sum of two elements defines the next + + In [3]: ... a, b = 0, 1 + + In [4]: >>> while b < 10: + ...: ... print b + ...: ... a, b = b, a+b + ...: + 1 + 1 + 2 + 3 + 5 + 8 + +And pasting from IPython sessions works equally well:: + + In [1]: In [5]: def f(x): + ...: ...: "A simple function" + ...: ...: return x**2 + ...: ...: + + In [2]: f(3) + Out[2]: 9 .. _gui_support: diff --git a/docs/source/whatsnew/version0.11.txt b/docs/source/whatsnew/version0.11.txt index 07848a7..0eb6dbd 100644 --- a/docs/source/whatsnew/version0.11.txt +++ b/docs/source/whatsnew/version0.11.txt @@ -110,6 +110,10 @@ A quick summary of the major changes (see below for details): can have its attributes set either via files that now use real Python syntax or from the command-line. +* **Pasting of code with prompts**. IPython now intelligently strips out input + prompts , be they plain Python ones (``>>>`` and ``...``) or IPython ones + (``In [N]:`` and `` ...:``). More details :ref:`here `. + Authors and support -------------------