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

前端 未结 3 1427
盖世英雄少女心
盖世英雄少女心 2020-12-30 05:47

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

相关标签:
3条回答
  • 2020-12-30 06:28

    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 讨论(0)
  • 2020-12-30 06:32

    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()
            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 = "0.0.0.0", 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.

    0 讨论(0)
  • 2020-12-30 06:34

    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.

    0 讨论(0)
提交回复
热议问题