Not quite the same as pasting into the shell, but the doctest
module can be useful. It scans a python module or regular text file looking for interactive script fragments and then runs them. Its primary use case is to blend documentation and unit test. Suppose you have a tutorial such as
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")
...
Save it to a file and then run 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
runs the script fragments and compares it to the expected output.
UPDATE
Here's a script that will take what's in the clipboard and paste back the python script fragments. Copy your example, run this script and then paste into the shell.
#!/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 ('>>> ', '... ')))