从 python 脚本与 python REPL 交互
Interact with python REPL from python script
我想找到(或制作)一个 python 脚本,它逐行读取不同的 python 脚本,并打印执行的命令和之后的输出。
假设您有一个 python 脚本,testfile.py
这样:
print("Hello world")
for i in range(3):
print(f"i is: {i}")
现在,我想要一个不同的 python 脚本来解析 testfile.py
并输出以下内容:
print("Hello world")
## Hello world
for i in range(3):
print(f"i is: {i}")
## i is: 0
## i is: 1
## i is: 2
非常感谢对现有软件或新代码如何实现这一目标的任何建议!
尝试/概念代码:
运行 ipython
来自 python:
第一个想法是 运行 ipython 从 python 使用 subprocess
:
import subprocess
import re
try:
proc = subprocess.Popen(args=["ipython", "-i"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
# Delimiter to know when to stop reading
OUTPUT_DELIMITER = ":::EOL:::"
# Variable to contain the entire interaction:
output_string = ""
# Open testfile.py
with open("testfile.py") as file_:
for line in file_:
# Read command
cmd = line.rstrip()
# Add the command to the output string
output_string += cmd + "\n"
proc.stdin.write(f"{cmd}\n")
# Print the delimiter so we know when to end:
proc.stdin.write('print("{}")\n'.format(OUTPUT_DELIMITER))
proc.stdin.flush()
# Start reading output from ipython
while True:
thisoutput = proc.stdout.readline()
thisoutput = thisoutput.rstrip()
# Now check if it's the delimiter
if thisoutput.find(OUTPUT_DELIMITER) >= 0:
break
output_string += thisoutput + "\n"
except Exception as e:
proc.stdout.close()
proc.stdin.close()
raise
proc.stdout.close()
proc.stdin.close()
print("-" * 4 + "START OUTPUT" + "-" * 4)
print(output_string)
print("-" * 4 + "END OUTPUT" + "-" * 4)
在这种方法中,问题变成了缩进块,就像 for
循环一样。
理想情况下,像这样的东西只使用普通的 python
(而不是 ipython
)就可以工作。
这不完全您想要的,但很接近。 trace
模块做的事情非常相似。
so.py:
print("Hello world")
for i in range(3):
print(f"i is: {i}")
python -m trace --trace so.py
--- modulename: so, funcname: <module>
so.py(1): print("Hello world")
Hello world
so.py(3): for i in range(3):
so.py(4): print(f"i is: {i}")
i is: 0
so.py(3): for i in range(3):
so.py(4): print(f"i is: {i}")
i is: 1
so.py(3): for i in range(3):
so.py(4): print(f"i is: {i}")
i is: 2
so.py(3): for i in range(3):
code.InteractiveConsole.interact
完全按照要求去做。
我想找到(或制作)一个 python 脚本,它逐行读取不同的 python 脚本,并打印执行的命令和之后的输出。
假设您有一个 python 脚本,testfile.py
这样:
print("Hello world")
for i in range(3):
print(f"i is: {i}")
现在,我想要一个不同的 python 脚本来解析 testfile.py
并输出以下内容:
print("Hello world")
## Hello world
for i in range(3):
print(f"i is: {i}")
## i is: 0
## i is: 1
## i is: 2
非常感谢对现有软件或新代码如何实现这一目标的任何建议!
尝试/概念代码:
运行 ipython
来自 python:
第一个想法是 运行 ipython 从 python 使用 subprocess
:
import subprocess
import re
try:
proc = subprocess.Popen(args=["ipython", "-i"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
# Delimiter to know when to stop reading
OUTPUT_DELIMITER = ":::EOL:::"
# Variable to contain the entire interaction:
output_string = ""
# Open testfile.py
with open("testfile.py") as file_:
for line in file_:
# Read command
cmd = line.rstrip()
# Add the command to the output string
output_string += cmd + "\n"
proc.stdin.write(f"{cmd}\n")
# Print the delimiter so we know when to end:
proc.stdin.write('print("{}")\n'.format(OUTPUT_DELIMITER))
proc.stdin.flush()
# Start reading output from ipython
while True:
thisoutput = proc.stdout.readline()
thisoutput = thisoutput.rstrip()
# Now check if it's the delimiter
if thisoutput.find(OUTPUT_DELIMITER) >= 0:
break
output_string += thisoutput + "\n"
except Exception as e:
proc.stdout.close()
proc.stdin.close()
raise
proc.stdout.close()
proc.stdin.close()
print("-" * 4 + "START OUTPUT" + "-" * 4)
print(output_string)
print("-" * 4 + "END OUTPUT" + "-" * 4)
在这种方法中,问题变成了缩进块,就像 for
循环一样。
理想情况下,像这样的东西只使用普通的 python
(而不是 ipython
)就可以工作。
这不完全您想要的,但很接近。 trace
模块做的事情非常相似。
so.py:
print("Hello world")
for i in range(3):
print(f"i is: {i}")
python -m trace --trace so.py
--- modulename: so, funcname: <module>
so.py(1): print("Hello world")
Hello world
so.py(3): for i in range(3):
so.py(4): print(f"i is: {i}")
i is: 0
so.py(3): for i in range(3):
so.py(4): print(f"i is: {i}")
i is: 1
so.py(3): for i in range(3):
so.py(4): print(f"i is: {i}")
i is: 2
so.py(3): for i in range(3):
code.InteractiveConsole.interact
完全按照要求去做。