iPython Notebook中的OptionParser? [英] OptionParser in ipython notebook?

查看:92
本文介绍了iPython Notebook中的OptionParser?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢在ipython笔记本中进行开发,但是当我想编写一个main()函数来读取命令行args(例如,使用OptionParser)时遇到了问题.我希望能够将代码导出到.py文件并从命令行运行它,但是我还没有找到一种方法来让main()既可以在带有预定义参数的笔记本中运行,也可以从命令行运行使用python和命令行参数.秘密是什么?

I am enjoying developing inside the ipython notebook, but I am having a problem when I want to write a main() function that reads the command line args (with OptionParser, for example). I want to be able to export the code to a .py file and run it from the command line, but I haven't found a way to have a main() that runs both in the notebook with predefined arguments or from the command line with python and command line args. What is the secret?

如果不清楚,我想做这样的事情:

In case that is not clear, I would like to do something like this:

if __name__ == '__main__':
    # if in the notebook
    vals = {'debug':True, 'tag_file': 't.tags'}
    options = Object()
    for k,v in vals.items():
        options.setattr(k,v)
    args = 'fname1.txt'
    # if running as a command line python script
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option('-d','--debug',action='store_true',dest='debug')
    parser.add_option('-t','--tags',action='store',dest='tag_file')
    options,args = parser.parse_args()

推荐答案

由于3可以连接到同一内核的简单原因,您不能确定自己在IPython笔记本,qtconsole还是简单的IPython shell中同时.

You cannot determine that you are in an IPython notebook or a qtconsole, or a simple IPython shell, for the simple reason the 3 can be connected to the same kernel at the same time.

这就像询问用户键入的当前键是什么颜色.您可以通过查看插入的USB设备并在互联网上查找图像并猜测键盘颜色来获得它,但是没有任何保证可以保证它准确无误,也不会改变它,并且用户可以插入多个键盘,甚至彩绘键盘.

It would be like asking, what color is the current key the user is typing. You could get it by looking the plugged usb devices and look for images on the internet and guess the keyboard color, but nothing guarantees you it will be accurate, nor that it won't change, and user can have multiple keyboard plugged, or even painted keyboard.

笔记本确实是一样的,即使您确定自己在ZMQKernel中,您是否也打算使用qtconsole或Web服务器?再次,您发现您正在与Web服务器通信,您是否正在与JS或Emacs通信?依此类推.

It is really the same with the notebook, Even if you determine you are in ZMQKernel, are you speeking to qtconsole or webserver ? Again, you found that you were talking to the webserver, are you talking to JS or Emacs ? And so on and so forth.

您唯一可以做的是,您可以询问用户.

The only thing you can do, you can ask the user.

可靠的方法是测试您是否在IPython中.

What is reliable, is test wether you are in IPython or not.

如果您真的非常轻松想要一种方法,那么到目前为止,笔记本电脑是唯一可以显示Javascript的东西.而且javascript可以在pyton中执行代码.因此,您可能可以创建一些显示JS的东西,这些JS将信息发送回内核.使用线程和计时器,您可以说您不在笔记本电脑中(但是您将处于竞赛状态).

If you really but reeaaalllyy want a way, as until now, the notebook is the only thing that can display Javascript. And javascript can execute code in pyton. So you might be able to create something that display JS that send back info to the kernel. And using thread and timer you can say that you were not in a notebook (but you will have a race condition).

这篇关于iPython Notebook中的OptionParser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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