##// END OF EJS Templates
Moving clearcmd.py to quarantine....
Moving clearcmd.py to quarantine. When a module is moved to quarantine, it means that while we intend to keep it, it is currently broken or sufficiently untested that it can't be in the main IPython codebase. To be moved back into the main IPython codebase a module must: 1. Work fully. 2. Have a test suite as much as possible. 3. Have members of the IPython dev team who are willing to maintain it.

File last commit:

r2063:9650bd9e
r2266:eda4ef85
Show More
InterpreterPasteInput.py
124 lines | 4.4 KiB | text/x-python | PythonLexer
/ IPython / extensions / InterpreterPasteInput.py
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # -*- coding: utf-8 -*-
"""Modified input prompt for entering text with >>> or ... at the start.
We define a special input line filter to allow typing lines which begin with
'>>> ' or '... '. These two strings, if present at the start of the input
line, are stripped. This allows for direct pasting of code from examples such
as those available in the standard Python tutorial.
Normally pasting such code is one chunk is impossible because of the
extraneous >>> and ..., requiring one to do a line by line paste with careful
removal of those characters. This module allows pasting that kind of
multi-line examples in one pass.
Here is an 'screenshot' of a section of the tutorial pasted into IPython with
this feature 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]
The >>> and ... are stripped from the input so that the python interpreter
only sees the real part of the code.
All other input is processed normally.
fperez
- New profile with doctest support (two way: for generating doctests and for...
r750
Notes
=====
* You can even paste code that has extra initial spaces, such as is common in
doctests:
In [3]: >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
In [4]: >>> for i in range(len(a)):
...: ... print i, a[i]
...: ...
0 Mary
1 had
2 a
3 little
4 lamb
Fernando Perez
Update copyright/author statements....
r1875
Authors
-------
- Fernando Perez <Fernando.Perez@berkeley.edu>
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 """
fperez
- New profile with doctest support (two way: for generating doctests and for...
r750
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 #*****************************************************************************
Fernando Perez
Update copyright/author statements....
r1875 # Copyright (C) 2008-2009 The IPython Development Team
# Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#*****************************************************************************
# This file is an example of how to modify IPython's line-processing behavior
# without touching the internal code. We'll define an alternate pre-processing
# stage which allows a special form of input (which is invalid Python syntax)
# for certain quantities, rewrites a line of proper Python in those cases, and
# then passes it off to IPython's normal processor for further work.
# With this kind of customization, IPython can be adapted for many
# special-purpose scenarios providing alternate input syntaxes.
# This file can be imported like a regular module.
# IPython has a prefilter() function that analyzes each input line. We redefine
# it here to first pre-process certain forms of input
# The prototype of any alternate prefilter must be like this one (the name
# doesn't matter):
# - line is a string containing the user input line.
fperez
- New profile with doctest support (two way: for generating doctests and for...
r750 # - continuation is a parameter which tells us if we are processing a first
# line of user input or the second or higher of a multi-line statement.
import re
Brian Granger
iplib.py => core/iplib.py and updated tests and imports.
r2028 from IPython.core.iplib import InteractiveShell
fperez
- new doctest_mode magic to toggle doctest pasting/prompts....
r763
fperez
- New profile with doctest support (two way: for generating doctests and for...
r750 PROMPT_RE = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
def prefilter_paste(self,line,continuation):
"""Alternate prefilter for input of pasted code from an interpreter.
"""
fperez
- New profile with doctest support (two way: for generating doctests and for...
r750 if not line:
return ''
m = PROMPT_RE.match(line)
if m:
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # In the end, always call the default IPython _prefilter() function.
# Note that self must be passed explicitly, b/c we're calling the
# unbound class method (since this method will overwrite the instance
# prefilter())
fperez
- New profile with doctest support (two way: for generating doctests and for...
r750 return self._prefilter(line[len(m.group(0)):],continuation)
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 elif line.strip() == '...':
return self._prefilter('',continuation)
fperez
- New profile with doctest support (two way: for generating doctests and for...
r750 elif line.isspace():
# This allows us to recognize multiple input prompts separated by blank
# lines and pasted in a single chunk, very common when pasting doctests
# or long tutorial passages.
return ''
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 else:
return self._prefilter(line,continuation)
fperez
- new doctest_mode magic to toggle doctest pasting/prompts....
r763 def activate_prefilter():
"""Rebind the input-pasting filter to be the new IPython prefilter"""
InteractiveShell.prefilter = prefilter_paste
def deactivate_prefilter():
"""Reset the filter."""
InteractiveShell.prefilter = InteractiveShell._prefilter
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
# Just a heads up at the console
fperez
- new doctest_mode magic to toggle doctest pasting/prompts....
r763 activate_prefilter()
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 print '*** Pasting of code with ">>>" or "..." has been enabled.'