为什么我不能在没有手动编辑的情况下粘贴 Pythons REPL 的输出? [英] Why can I not paste the output of Pythons REPL without manual-editing?
问题描述
大量的示例 Python 代码显示了 Python REPL 的输出,例如:
<预><代码>>>>类例如(对象):... def __init__(self, name):... self.name = 姓名...定义你好(自我):...打印嗨 %s" % (self.name)...>>>迎宾 = eg("鲍勃")>>>问候语.hi()嗨鲍勃>>>现在,你想要做的显而易见的事情是运行上面的代码..所以,我运行python"并将上面的文本粘贴到..
<预><代码>>>>>>>类例如(对象):文件<stdin>",第 1 行>>>类例如(对象):^语法错误:无效语法>>>... def __init__(self, name):文件密码坏了!?..
要让它运行,我必须要么......
- 一次复制并粘贴一行,确保我正确复制了所有缩进.如果你搞砸了(比如,错过了一个前导空间,你必须重新开始)
- 使用文本编辑器删除
>>>
和...
,然后再次粘贴
这不是一个大问题,但鉴于以这种格式提供了多少示例代码,您必须这样做似乎很奇怪..
如何运行/采用Pythons REPL的输出"
使用 IPython 外壳
在 [99]: %cpaste粘贴代码;单独输入'--'就行了.:>>>类例如(对象)::... def __init__(self, name)::... self.name = name:... def hi(self)::... 打印 "Hi %s" % (self.name):...:>>>迎宾 = eg("鲍勃"):>>>问候语.hi():--嗨鲍勃
使用功能强大的文本编辑器(例如,
Cx rk
在 Emacs)使用doctest模块
首先在没有 shell 提示的情况下进行复制(例如,尽管我不知道如何在 Google Chrome 上执行此操作).
为什么使用 doctest 格式
将以下内容保存到documentation.txt
:
运行:
$ python -c "import doctest; doctest.testfile('documentation.txt')" -v
输出:
尝试:类例如(对象):def __init__(self, name):self.name = 姓名def hi(自我):打印嗨 %s" % (self.name)什么都不期待好的试:迎宾 = eg("鲍勃")什么都不期待好的试:问候语.hi()期待:嗨鲍勃好的1 个项目通过了所有测试:doctest.txt 中的 3 个测试3 项测试 1 项.3 次通过,0 次失败.通过测试.
如果您在模块末尾添加以下代码段,它将测试其文档字符串中的所有代码:
如果 __name__=="__main__":导入文档测试;doctest.testmod()
QED
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..
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
这篇关于为什么我不能在没有手动编辑的情况下粘贴 Pythons REPL 的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!