What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 134 Stack Exchange communities.

If the poster has copied from the user interface, especially in interactive languages such as python lines entered by the user will start with either >>> or ... while lines of responses and error messages will not.

Wouldn't it be possible for a copy mechanism to only copy lines that started with the prompt and skip any lines that didn't plus strip the known prompt characters out of the copied code.

This would greatly simplify copying code from questions & answers to try the code out and work on the solution.

Obviously such changes should only be applied when copying from examples/answers rather than when copying into them.

e.g if the user posts:

Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 2,
2
>>> print fred
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fred' is not defined
>>> print fred()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fred' is not defined
>>> def fred():
...    return (27)
... 
>>> print fred()
27
>>> 

And I select and paste it I would like it to paste as:

print 2,
print fred
print fred()
def fred():
   return (27)

print fred()
share|improve this question
5  
But if I copy code I copied it that way because I want it to look that way. If you show someone what code will look like on the interpreter then they know exactly what you're doing.... which is helpful for newer people. You've also missed the return value and exceptions, which is not something helpful to exclude. –  ben is uǝq backwards Jul 27 '13 at 16:01
    
I am talking about copying from the site not to it. Question clarified. –  Steve Barnes Jul 27 '13 at 16:06
    
If I copy code I want it to look exactly the same when pasted. If the real problem is users posting poorly formatted code, then we should educate them about it. Sites that modify what you're copying (through use of invisible text or Javascript) are among the worst on the Internet, to be honest. –  slhck Jul 27 '13 at 16:09
    
So you enjoy going through a pasted example and removing all the >>> and ... at the start of every line! –  Steve Barnes Jul 27 '13 at 16:13
2  
I wouldn't particularly enjoy it, but I definitely wouldn't enjoy a site messing with the text I copy from it. The question is: Does this happen so often that it warrants the implementation of the feature you request? I could imagine that it'd take quite a bit of time to figure out all the specifics, and even then we'd just be talking about one implementation of a Python interpreter. –  slhck Jul 27 '13 at 16:16
3  
Hint: s/>>> // removes the start of the line just fine. –  slhck Jul 27 '13 at 16:18
1  
As why not could be a interesting thread (I think this is one of the goal of meta-SO), please stop downvote this and explain your point of vue. –  F. Hauri Jul 28 '13 at 9:01

2 Answers 2

up vote 1 down vote accepted

Don't change a working solution

Adding such kind of feature may have some unpredictable border effects. As my opinion is that all work fine now, and the formating operation for publishing on SO stay quite simple and efficient.

Please don't touch!

Build your own workaround solutions

sed on the fly

You could even filter both ouptut of your interpreter by sed on the fly,

Running directly you interpreter by:

python 2>&1| sed -ue 's/^\(\(>\|\.\)\2\2 \)*//'

or

python 2>&1| sed -ue 's/^\(>>> \|\.\.\. \)*//'

than hit your sample

Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
print 3,
3
print fred
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fred' is not defined
print fred()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fred' is not defined
def fred():
    return(31)

print fred()
31
exit()

this could be improved.

script output re-formated

Another solution may use tools like script for storing whole operation, than a little script could be used to re-format stored scriptfiles.

script -c python /tmp/python;sed 's/^\(\.\|>\)\1\1 //;s/^/    /' /tmp/python 

Offer a nice way for this:

Script started on Sun Jul 28 11:13:04 2013
Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
print 4
4
print fred
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fred' is not defined
print fred()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fred' is not defined
def fred():
    return (32)

print fred()
32
exit()

Script done on Sun Jul 28 11:13:48 2013
share|improve this answer

Basically you want lines that start with ">>> " and "... " but with those characters removed.

One of many ways to do this: you can use sed in bash / dash (or any shell that supports heredocs) to format the Python console code you copied

sed -n 's/^>>> //p; s/^\.\.\. //p' <<'EOI'
... copied code here ...
EOI

then you can copy the output of this command to Python interpreter.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .