シェルに貼り付けるのと全く同じではありませんが、doctest
モジュールを使うと便利です。 Pythonモジュールまたは通常のテキストファイルをスキャンして、インタラクティブスクリプトの断片を探してから実行します。その主な使用例は、ドキュメントと単体テストを混在させることです。あなたは、ファイルへのそのようなこと保存
This is some code to demonstrate the power of the `if`
statement.
>>> if True:
... print("x")
...
x
Remember, each `if` increases entropy in the universe,
so use with care.
>>> if False:
... print("y")
...
としてチュートリアルを持っていると仮定して、doctest
$ python -m doctest -v k.txt
Trying:
if True:
print("x")
Expecting:
x
ok
Trying:
if False:
print("y")
Expecting nothing
ok
1 items passed all tests:
2 tests in k.txt
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
doctest
を実行すると、スクリプトの一部を実行し、期待される出力と比較します。
UPDATE
はここでクリップボードに何取るとPythonスクリプトの断片をバックペーストするスクリプトです。あなたの例をコピーし、このスクリプトを実行してシェルに貼り付けます。
#!/usr/bin/env python3
import os
import pyperclip
pyperclip.copy(os.linesep.join(line[4:]
for line in pyperclip.paste().split(os.linesep)
if line[:4] in ('>>> ', '... ')))
@PadraicCunningham、それは 'python'インタープリタのデフォルト動作ではありません。 [IPython](https://ipython.org/)はPythonには含まれていません。 – Chris
ええ、op _did_に質問の中のIPythonがあります:) – bakkal
@Chris、私はPythonインタプリタについて言及したことはありませんでした。私はOPyを参照するipythonインタプリタについて話しました。*誰かがPythonやiPythonを入手する方法を知っていますか? ... * –