开发者

Is there a way to interactively program a Python curses application?

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 int开发者_如何转开发eractive 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?


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:

import sys
import SocketServer
from code import InteractiveConsole

class InteractiveServer(SocketServer.BaseRequestHandler):
   def handle(self):
        file = self.request.makefile(mode='rw')
        shell = Shell(file)
        try:
           shell.interact()
        except SystemExit:
           pass


class Shell(InteractiveConsole):
    def __init__(self, file):
        self.file = sys.stdout = file
        InteractiveConsole.__init__(self)
        return

    def write(self, data):
       self.file.write(data)
       self.file.flush()

    def raw_input(self, prompt=""):
       self.write(prompt)
       return self.file.readline()

if __name__ == '__main__':
   HOST, PORT = "127.0.0.1", 9999

   server = SocketServer.TCPServer((HOST, PORT), InteractiveServer)
   server.serve_forever()

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.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜