有没有一种方法可以交互式地编写 Python Curses 应用程序?
有没有办法创建第二个终端,以便对 curses 函数的所有调用都在该终端上运行,而不是在现有终端中运行?当我可以交互地尝试时,我的工作速度会更快,因此我希望能够在一个终端中运行交互式 python 解释器,并在另一个终端中查看curses 输出。
事实上,在交互式窗口中调用 initscr() 要么失败 (PyDev),要么永久取消主机的窗口刷新 (Spyder),或者导致控制台出现奇怪的行为 (IPython)。
是否可以使用 setupterm() 接管不同的终端?如果是这样,我在哪里可以获得不同的 TERM
字符串来调用它?
Is there a way to create a second terminal so that all calls to curses
functions operate on that, rather than in the existing terminal? I work much faster when I can try things out interactively, so I'd like to be able to run an interactive python interpreter in one terminal and see the curses output in another.
As it is, calling initscr()
in an interactive window either fails (PyDev) or permanently takes away window refresh from the host (Spyder) or causes weird behavior in the console (IPython).
Is it possible to take over a different terminal using setupterm()
? If so, where do I get a different TERM
string to call it with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 code.InteractiveConsole 和 SocketServer 将 python 交互式 shell 附加到套接字,并通过它进行开发。一个简单的示例如下所示:
一旦您启动并运行了该程序,您就可以从另一个终端连接到端口 9999 并执行您的操作。您可以在此屏幕截图 (PNG) 中看到此操作。
使用 InteractiveConsole 的基础知识是采取来自这篇文章。我对其进行了修改,以便与我正在从事的另一个项目的 SocketServer 一起使用。
You could use code.InteractiveConsole and SocketServer to attach a python interactive shell to a socket and do your development through that. A simple example looks like:
Once you've got that up and running you can connect to port 9999 from another terminal and do your thing. You can see this working in this screenshot (PNG)
The basics for using the InteractiveConsole were taken from this post. I modified it to work with the SocketServer for another project I was working on.
我不这么认为,因为curses 模块大部分(完全?)是在C 级别实现的。它不太可能提供这样的钩子,尽管如果您熟悉该语言,可能值得查看源代码。
然而,在阅读你的问题时,我想到了我在其他情况下使用的另一种技术。您可以通过另一个终端/编辑器保存脚本,并使用类似于 dnotify 命令的技术(甚至简单的轮询)将其加载到正在运行的程序中。
另一个想法是使用套接字发送命令并执行它们。当然,这在安全方面是危险的,因此请采取必要的预防措施。
您必须构建一些基础设施,但这可能比向诅咒添加多设备支持容易得多。
I don't believe so as the curses module is mostly (totally?) implemented at the C level. It's unlikely that it would provide such hooks, although if you are familiar with the language it might be worth looking thru the source.
However while reading your question I thought of another technique which I use in other contexts. You can save a script via another terminal/editor and use a technique similar to the dnotify command (or even simple polling) to load it into your running program.
Another idea would be to use sockets to send commands over and execute them. Of course this is dangerous security-wise so take the necessary precautions.
You'll have to build some infrastructure, but it would likely be much easier than adding multiple device support to curses.
好吧,我不确定我是否完全理解你想要做什么。但我的理解是,你想要一个标准的 python 控制台,你可以在其中动态输入代码。但是,当您调用一个函数时,该函数的处理输出会出现在另一个终端中吗?
嗯......为了让它工作,我认为使用的架构将是“客户端-服务器”。
因为进程有一个 stdout 和一个 stderr,并且在多处理架构中,您可以使用 stderr 作为函数的输出管道。但问题是初始化与主终端分离的另一个终端。 (同一空间内不重叠)。
如果你的主程序初始化一个服务器(在另一个Python进程上,由于服务器本身的性质),它将输出发送到所有连接到它的客户端。通过这种方式,您可以在多个终端客户端和/或另一台能够连接到您的服务器的计算机上可视化该函数的输出。
在我看来,这比尝试使用“curses”包要容易得多。
但如果唯一的目的是深入了解您的代码,我认为它过于复杂(没有附加价值)。
您仍然可以选择将函数的输出转储到文本文件 (log.txt)
Well, I'm not sure I understand completly what you're trying to do. But what I've understood is this that you want to have a standard python console where you can type your code dynamically. But when you call, for exemple a function, the output of the processing of this function would appear into another terminal?
Well... for it to work, I think the architecture to use would be a "client-server".
Because a process has an stdout and a stderr, and in a multiprocessing architecture you could use the stderr as the function's output pipe. But the problem is initializing the other terminal that is separated from the main one. (no overlapping inside the same space).
If your main program Initialize a Server (on another Python process, because of the nature itself of a server) which sends the output to all client connected to it. This way you could visualize the function's output on several terminal clients and/or another computer able to connect to your server.
It is, at my opinion, much easier than trying to use the 'curses' package.
But if the only purpose is to gain an insight of your code, I think it's overcomplicated (no added value).
You still have the option of dumping the function's output into a text file (log.txt)