IPython模块
我有一些具有冗余功能的 IPython 脚本。 我想将常见功能重构为一个模块,并将该模块包含在现有脚本中。 问题是它不能成为 python 模块,因为代码使用 Ipython 的语言扩展(!、$ 等)。 是否可以制作一个具有 IPython 代码的模块并将其包含在另一个 IPython 脚本中?
I have a few IPython scripts which have redundant functionality. I would like to refactor the common functionality into one module and include that modules in the existing script. The problem is it cannot be made a python module as the code uses Ipython's language extensions (!, $ etc). Is it possible to make a module having IPython code and include it in another IPython scripts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从技术上讲,如果您保存带有
.ipy
扩展名的脚本,ipython 会看到它并使用它的所有花哨的东西,而不是直接传递给 python 解释器。 然而,我通常会建议不要这样做,并走上面 S.Lott 的路线。technically if you save a script with the
.ipy
extension, ipython will see that and use all it's fancy stuff rather than passing directly to the python interpreter. however, i would generally recommend against this, and go the route of S.Lott above.很多人坚信你不应该在其中包含带有 IPython 语法的脚本,但如果你足够好奇(就像我一样)并且正在寻找一些有趣的方法来混合 python 和 shell 脚本,你应该查看我的github 上的包装程序
示例用例:
事实证明,IPython 核心还支持上述脚本的(几乎没有功能)版本:
A lot of people strongly believe you're not supposed to have scripts with IPython syntax in them, but if you were curious enough (as I am) and are looking for some fun ways to mix python and shell scripts, you should checkout out my wrapper program on github
Example use case:
As it turns out, the IPython core also supports a (barely functional) version of the above script:
如果您将命令输入 IPython 的交互式版本,然后使用 hist 命令(使用 -n 删除行号),IPython 会吐出您运行的所有命令,并使用实际的 python 代码代替 !cd ! ls 等。这是一个例子。
http://ipython.scipy.org/moin/IpythonExtensionApi 解释了此对象。 这基本上就是您需要做的(改编自链接):
现在您从 IPython shell 的 hist 命令粘贴的所有代码都应该可以正常工作。
If you enter the commands into an interactive version of IPython and then use the hist command (with -n to remove line numbers), IPython spits out all of the commands that you ran, with the actual python code used in place of !cd !ls, etc. Here's an example.
http://ipython.scipy.org/moin/IpythonExtensionApi explains this object. This is basically what you need to do (adapted from the link):
Now all of the code you pasted from the IPython shell's hist command should work fine.
你能行的。
这是一个例子。
这是 a.ipy 文件的内容:
这是 b.ipy 文件的内容:
正如您所看到的 b.ipy 使用! 操作员。
当你执行 a.ipy 时,你会得到以下结果:
因此,你“导入”“模块”不像在 python 中那样,而是像在 shell 中使用
source
那样。但我不确定这是否是正确的方法,也许是。 至少它可以工作并允许您提取通用功能并从其他脚本文件中重用它。
You can do it.
Here is an example.
This is content of a.ipy file:
This is content of b.ipy file:
As you can see b.ipy uses ! operator.
When you execute a.ipy you get the following result:
So, you "import" "modules" not like you do it in python, but like you do it in shell with
source
.But I'm not sure if it's right way, may be it is. At least it works and allows you to extract common functionality and reuse it from other script files.
您看过 IPython 模块 (
pydoc IPython
) 吗? 也许你可以通过纯Python代码访问IPython的实用程序。Have you had a look at the IPython module (
pydoc IPython
)? maybe you can access IPython's utilities through pure Python code.您不应该将 IPython 扩展内容(
?
、!
、%run
)保存在文件中。 曾经。 这些是交互式工具,您可以用手输入它们,但永远不会保存到文件中。查找文件中的共同特征。 恰好有四种事物适合此目的。
导入(
导入
)函数定义 (
def
)类定义(
class
)全局变量赋值
您必须从此代码中删除所有 IPython 交互功能。 所有这些。
重写您的脚本,以便它们 (1) 导入您的常用内容,(2) 完成它们应该做的有用工作。
您必须从此代码中删除所有 IPython 交互功能。 所有这一切。
现在您可以运行您的脚本,它们会像正确的 Python 脚本一样完成工作。
在输入时,您仍然可以使用 IPython 扩展功能,例如
!
、?
和%run
,但不应将这些功能保存到文件中。You should not be saving the IPython extension stuff (
?
,!
,%run
) in files. Ever. Those are interactive tools and they are something you type with your hands but never save to a file.Find the common features among your files. You have exactly four kinds of things that are candidates for this.
Imports (
import
)Function definitions (
def
)Class definitions (
class
)Global variable assignments
You must remove all IPython interactive features from this code. All of it.
Rewrite your scripts so they (1) import your common stuff, (2) do the useful work they're supposed to do.
You must remove all IPython interactive features from this code. All of it.
Now you can run your scripts and they're do their work like proper Python scripts are supposed.
You can still use IPython extension features like
!
,?
and%run
when you're typing, but you should not save these into files.