Why can I not paste the output of Pythons REPL without manual-editing?

北慕城南 提交于 2020-01-01 05:46:29

问题


A huge amount of example Python code shows the output of the Python REPL, for example:

>>> class eg(object):
...     def __init__(self, name):
...             self.name = name
...     def hi(self):
...             print "Hi %s" % (self.name)
... 
>>> greeter = eg("Bob")
>>> greeter.hi()
Hi Bob
>>> 

Now, the obvious thing you want to do is run the above code.. so, I run "python" and paste the above text in..

>>> >>> class eg(object):
  File "<stdin>", line 1
    >>> class eg(object):
     ^
SyntaxError: invalid syntax
>>> ...     def __init__(self, name):
  File "<stdin>", line 1
    ...     def __init__(self, name):
    ^

The code is broken!?..

To get it to run, I would have to either..

  • copy-and-paste the lines one at a time, making sure I copy all the indentation correctly. If you screw it up (say, miss a leading space, you have to start all over again)
  • use a text editor to remove >>> and ..., then paste again

It's not a huge issue, but given how much example code is presented in this format, it seems strange you have to do this..


回答1:


How to run/adopt "the output of Pythons REPL"

  • Use IPython shell

    In [99]: %cpaste
    Pasting code; enter '--' alone on the line to stop.
    :>>> class eg(object):
    :...     def __init__(self, name):
    :...             self.name = name
    :...     def hi(self):
    :...             print "Hi %s" % (self.name)
    :...
    :>>> greeter = eg("Bob")
    :>>> greeter.hi()
    :--
    Hi Bob
    
  • Use a capable text editor (e.g., C-x r k kills rectangular region in Emacs)

  • Use doctest module

Copy without the shell prompt in the first place (though I don't know how to do it on Google Chrome, for example).

Why the doctest format is used

Save the following to documentation.txt:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. 

>>> class eg(object):
...     def __init__(self, name):
...             self.name = name
...     def hi(self):
...             print "Hi %s" % (self.name)
... 
>>> greeter = eg("Bob")
>>> greeter.hi()
Hi Bob
>>>

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.

Run:

$ python -c "import doctest; doctest.testfile('documentation.txt')" -v

Output:

Trying:
    class eg(object):
        def __init__(self, name):
                self.name = name
        def hi(self):
                print "Hi %s" % (self.name)
Expecting nothing
ok
Trying:
    greeter = eg("Bob")
Expecting nothing
ok
Trying:
    greeter.hi()
Expecting:
    Hi Bob
ok
1 items passed all tests:
   3 tests in doctest.txt
3 tests in 1 items.
3 passed and 0 failed.
Test passed.

If you add the following snippet at the end of your module it will test all code in its docstrings:

if __name__=="__main__":
   import doctest; doctest.testmod()

QED




回答2:


I don't know if there's a good solution out there for this. Ideally, there'd be some way to modify the behavior of the interpretter to accept copy/paste input of this sort. Here are some alternate suggestions:

Use triple quoting to save the example to a string. Then, use exec:

>>> def chomp_prompt(s): return '\n'.join(ln[4:] for ln in s.splitlines())
...
>>> dirty = """>>> class eg(object):
... ...     def __init__(self, name):
... ...             self.name = name
... ...     def hi(self):
... ...             print "Hi %s" % (self.name)
... ...
... >>> greeter = eg("Bob")
... >>> greeter.hi()
... """
>>> clean = chomp_prompt(dirty)
>>> exec clean
Hi Bob
>>>

Not only does my solution all fit on one line (so it'll be easy for you to copy/paste it in the interpreter), it works on the above example :D :

>>> s = r'''>>> def chomp_prompt(s): return '\n'.join(ln[4:] for ln in s.splitlines())
... ...
... >>> dirty = """>>> class eg(object):
... ... ...     def __init__(self, name):
... ... ...             self.name = name
... ... ...     def hi(self):
... ... ...             print "Hi %s" % (self.name)
... ... ...
... ... >>> greeter = eg("Bob")
... ... >>> greeter.hi()
... ... """
... >>> clean = chomp_prompt(dirty)
... >>> exec clean'''
>>> s2 = chomp_prompt(s)
>>> exec s2
Hi Bob

My second suggestion is to look at ipython's ability to open an editor for you and execute what you entered there after you're done editing:

http://ipython.scipy.org/doc/rel-0.9.1/html/interactive/tutorial.html#source-code-handling-tips

If you set emacs as your editor, I know it has the ability to delete a rectangle of text (you can probably guess the command: M-x delete-rectangle), which would work perfectly for getting rid of those pesky prompts. I'm sure many other editors have this as well.




回答3:


"Why" questions rarely have useful answers.

For example, if I said that the reason why was to avoid a complex intellectual property infringement lawsuit, what does that do? Nothing. You still have to stop copying and pasting and start thinking and typing.

Or, for example, if I said that the reason why was given here, there's nothing actionable. The problem is that examples have to be typed instead of cut-and-pasted. And that problem is not solved by this information.

Indeed, the problem is really "I want to copy-and-paste without so much thinking and typing, how can I do that?" and the answer is the same.

You can't copy and paste the interactive session (except in doctest comments). You have to type it. Sorry.




回答4:


The code is presented this way, because it is meant to be a step by step process. The three characters you see ">>>" are the ones of the Python IDE, but it seems you know that already. When you have access to a console or a shell, and type python, you will get something like this.

% python
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

So really take it as an educational tool. :)



来源:https://stackoverflow.com/questions/647142/why-can-i-not-paste-the-output-of-pythons-repl-without-manual-editing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!