IPython手动迭代主循环? [英] IPython iterate main loop manually?

查看:89
本文介绍了IPython手动迭代主循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以以某种方式实例ipython(甚至更好,ipython-qtconsole)并手动执行它的(IPython)主循环吗?

Can i somehow instance ipython (or even better, ipython-qtconsole) and step trough its's (IPython's) main loop manually?

我想编辑panda3d程序飞行。

I want to edit panda3d programs on the fly.

EDIT1:
这是代码示例,应该澄清一下我想做的事情。

Here is code sample which should clarify a bit what i want to do.

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        self.qtApp = QApplication(sys.argv)
        label = QLabel("Hello World")
        label.show()
        self.m = loader.loadModel("frowney")
        self.m.reparentTo(render)

        while 1:
            self.qtApp.processEvents()  #manual step trough Qt loop
            taskMgr.step()              #manual step trough Panda3D loop

app = MyApp()

所以哟你可以看到我如何手动步入熊猫和qt,如果可能的话我想和ipython一样。

So you can see how i can manually step trough panda and qt, i want to do same with ipython if its possible.

ANSWER
完整文件:

ANSWER Complete file:

from direct.showbase.ShowBase import ShowBase
from IPython.lib import inputhook
class MyApp(ShowBase):

def __init__(self):
    ShowBase.__init__(self)
    self.m = loader.loadModel("frowney")
    self.m.reparentTo(render)

def stepMe(self):
    taskMgr.step()              #manual step trough Panda3D loop
    return 0  
if __name__ == "__main__":  
    app = MyApp()  
    inputhook.set_inputhook(app.stepMe)

在您的cmd行中,只需转到文件所在的目录并执行

In your cmd line, just go to directory where file is and do


  1. ipython

  2. 运行file.py

  3. app.m.setPos(1,1,1)

  1. ipython
  2. run file.py
  3. app.m.setPos(1,1,1)


推荐答案

通过编辑panda3d程序飞,你只是想换东西来测试你的跑步计划吗?或者实际上是在交互式环境中对程序的结构进行持久编辑?

By "edit panda3d programs on the fly", do you just mean changing things in order to test your running program? Or actually making persistent edits to your program's structure in the interactive environment?

在交互式python会话中简单地遍历循环非常简单。您可以使用方法声明替换而1:,例如 def step(self):,然后调用它每一步。

Simply stepping over your loop in an interactive python session is quite easy. You can just replace while 1: with a method declaration such as def step(self):, then call it for each step.

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        self.qtApp = QApplication(sys.argv)
        label = QLabel("Hello World")
        label.show()
        self.m = loader.loadModel("frowney")
        self.m.reparentTo(render)

    def step(self):
        self.qtApp.processEvents()  #manual step trough Qt loop
        taskMgr.step()              #manual step trough Panda3D loop
        return 0                    #PyOS_InputHook expects an integer

if __name__ == "__main__":
    app = MyApp()
    while 1: app.step()

使用 if __name __ ==__ main __对于主循环,您的文件仍然可以在独立运行时运行。但现在您可以将其导入交互式会话并在两个步骤之间进行修改。

With if __name__ == "__main__" for the main loop, your file will still work as it should when run standalone. But now you can import it into an interactive session and modify things in between steps.

>>> import myfile
>>> app = myfile.MyApp()
>>> app.step()
>>> app.something = something_else
>>> app.step()

现在将它添加到IPython的事件循环中,所以它会像你一样运行使用解释器,你可以使用 IPython.lib.inputhook.set_inputhook()(IPython 0.11中的新功能)。

Now to add it to IPython's event loop, so it will be run as you use the interpreter, you can use IPython.lib.inputhook.set_inputhook() (new in IPython 0.11).

>>> from IPython.lib import inputhook
>>> inputhook.set_inputhook(app.step)

这会导致程序在解释程序空闲时运行,但仍然允许像往常一样操纵。

This should cause your program to run while the interpreter is idle, but still allow manipulation as usual.

这篇关于IPython手动迭代主循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆