[IPython-user] e-mail pasting with cpaste

Stefan van der Walt stefan at sun.ac.za
Thu Jul 6 03:56:24 CDT 2006


Hi all,

I find %cpaste very useful.  However, I often want to paste pieces of
code received in an email, which are prepended with '>' characters.
The attached patch strips all '>' characters from the beginning of
cpaste lines, allowing you to paste code like

On 6 July 2006, Stefan wrote:

> print "123"

I couldn't think of any situations where this would cause problems
with other python code -- are there any?

Regards
Stéfan
-------------- next part --------------
Index: IPython/Magic.py
===================================================================
--- IPython/Magic.py	(revision 1385)
+++ IPython/Magic.py	(working copy)
@@ -2915,8 +2915,10 @@
         is the new sentinel for this operation)
         
         The block is dedented prior to execution to enable execution of 
-        method definitions. The executed block is also assigned to variable 
-        named 'pasted_block' for later editing with '%edit pasted_block'.
+	method definitions. '>' characters at the beginning of a line is
+	ignored, to allow pasting directly from e-mails. The executed block
+	is also assigned to variable named 'pasted_block' for later editing
+	with '%edit pasted_block'.
         
         You can also pass a variable name as an argument, e.g. '%cpaste foo'.
         This assigns the pasted block to variable 'foo' as string, without 
@@ -2939,7 +2941,7 @@
             l = iplib.raw_input_original(':')
             if l ==sentinel:
                 break
-            lines.append(l)
+            lines.append(l.lstrip('>'))
         block = "\n".join(lines) + '\n'
         #print "block:\n",block
         if not par:


More information about the IPython-user mailing list