IPython模块

发布于 2024-07-26 00:51:10 字数 158 浏览 12 评论 0原文

我有一些具有冗余功能的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

用心笑 2024-08-02 00:51:11

从技术上讲,如果您保存带有 .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.

故事灯 2024-08-02 00:51:11

很多人坚信你不应该在其中包含带有 IPython 语法的脚本,但如果你足够好奇(就像我一样)并且正在寻找一些有趣的方法来混合 python 和 shell 脚本,你应该查看我的github 上的包装程序

示例用例:

$ cat > example.ipy

rehashx

a = !ls -l | tail -n 3
print a.fields(0)

b = 'foo'
echo bar ${b}

$ ipyscript.py example.ipy

['-rw-r--r--', 'drwxr-xr-x', 'drwxrwxr-x']
bar foo

事实证明,IPython 核心还支持上述脚本的(几乎没有功能)版本:

In [2]: IPython.core.interactiveshell.InteractiveShell.safe_execfile_ipy?
Type:       instancemethod
Base Class: <type 'instancemethod'>
String Form:<unbound method InteractiveShell.safe_execfile_ipy>
Namespace:  Interactive
File:       /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py
Definition: IPython.core.interactiveshell.InteractiveShell.safe_execfile_ipy(self, fname)
Docstring:
Like safe_execfile, but for .ipy files with IPython syntax.

Parameters
----------
fname : str
    The name of the file to execute.  The filename must have a
    .ipy extension.

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:

$ cat > example.ipy

rehashx

a = !ls -l | tail -n 3
print a.fields(0)

b = 'foo'
echo bar ${b}

$ ipyscript.py example.ipy

['-rw-r--r--', 'drwxr-xr-x', 'drwxrwxr-x']
bar foo

As it turns out, the IPython core also supports a (barely functional) version of the above script:

In [2]: IPython.core.interactiveshell.InteractiveShell.safe_execfile_ipy?
Type:       instancemethod
Base Class: <type 'instancemethod'>
String Form:<unbound method InteractiveShell.safe_execfile_ipy>
Namespace:  Interactive
File:       /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py
Definition: IPython.core.interactiveshell.InteractiveShell.safe_execfile_ipy(self, fname)
Docstring:
Like safe_execfile, but for .ipy files with IPython syntax.

Parameters
----------
fname : str
    The name of the file to execute.  The filename must have a
    .ipy extension.
遥远的绿洲 2024-08-02 00:51:11

如果您将命令输入 IPython 的交互式版本,然后使用 hist 命令(使用 -n 删除行号),IPython 会吐出您运行的所有命令,并使用实际的 python 代码代替 !cd ! ls 等。这是一个例子。

_ip.system("ls")
_ip.system("ls -F ")
_ip.magic("cd ")

http://ipython.scipy.org/moin/IpythonExtensionApi 解释了此对象。 这基本上就是您需要做的(改编自链接):

import IPython.ipapi
_ip = IPython.ipapi.get()

现在您从 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.

_ip.system("ls")
_ip.system("ls -F ")
_ip.magic("cd ")

http://ipython.scipy.org/moin/IpythonExtensionApi explains this object. This is basically what you need to do (adapted from the link):

import IPython.ipapi
_ip = IPython.ipapi.get()

Now all of the code you pasted from the IPython shell's hist command should work fine.

ゞ花落谁相伴 2024-08-02 00:51:11

你能行的。

这是一个例子。

这是 a.ipy 文件的内容:

%run b.ipy

print(myvar)
print(myfunc())

这是 b.ipy 文件的内容:

myvar = !echo 1

def myfunc():
  tmp_var = !echo 2
  return tmp_var

正如您所看到的 b.ipy 使用! 操作员。
当你执行 a.ipy 时,你会得到以下结果:

ipython a.ipy 
['1']
['2']

因此,你“导入”“模块”不像在 python 中那样,而是像在 shell 中使用 source 那样。

但我不确定这是否是正确的方法,也许是。 至少它可以工作并允许您提取通用功能并从其他脚本文件中重用它。

You can do it.

Here is an example.

This is content of a.ipy file:

%run b.ipy

print(myvar)
print(myfunc())

This is content of b.ipy file:

myvar = !echo 1

def myfunc():
  tmp_var = !echo 2
  return tmp_var

As you can see b.ipy uses ! operator.
When you execute a.ipy you get the following result:

ipython a.ipy 
['1']
['2']

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.

山有枢 2024-08-02 00:51:11

您看过 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.

送舟行 2024-08-02 00:51:10

您不应该将 IPython 扩展内容(?!%run)保存在文件中。 曾经。 这些是交互式工具,您可以用手输入它们,但永远不会保存到文件中。

  1. 查找文件中的共同特征。 恰好有四种事物适合此目的。

    • 导入(导入

    • 函数定义 (def)

    • 类定义(class

    • 全局变量赋值

    您必须从此代码中删除所有 IPython 交互功能。 所有这些。

  2. 重写您的脚本,以便它们 (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.

  1. 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.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文