IPython Modules

I have several IPython scripts that have redundant functionality. I would like to reorganize the general functionality into one module and include these modules in an existing script. The problem is that it cannot be created as a python module, since the code uses the extensions of the Ipython language (!, $ Etc.). Is it possible to create a module with IPython code and include it in other IPython scripts?

+4
source share
6 answers

You should not save IPython extension files ( ? , ! , %run ) in files. Ever. These are interactive tools, and they are what you type with your hands but never save to a file.

  • Find common functions among your files. You have exactly four types of things that are candidates for this.

    • Import

    • Function Definitions ( def )

    • Class definitions

    • Global Variable Assignments

    You must remove all IPython interactive features from this code. All this.

  • Rewrite your scripts so that they (1) import your common things, (2) do the useful work that they have to do.

    You must remove all IPython interactive features from this code. All this.

    Now you can run your scripts and they do their job as the correct Python scripts assume.

You can use IPython extension functions such as ! , ? and %run when you print, but you should not save them in files.

+7
source

technically, if you save the script with the .ipy extension, ipython will see this and use all this fantasy, and not pass it directly to the python interpreter. however, I would recommend against this and go the route of S. Lott above.

+4
source

Many people think that you should not have scripts with IPython syntax, but if you were curious enough (like me) and looking for some interesting ways to mix python and shell scripts, you should checkout out a wrapper program on github

Usage example:

 $ 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 turned out, the IPython kernel 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. 
+3
source

If you enter commands into the interactive version of IPython, and then use the hist command (with -n to delete line numbers), IPython spits out all the commands that you ran with the actual python code used instead! cd! ls etc. Here is 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 by reference):

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

Now all the code that you pasted from the IPython shell command line should work fine.

+2
source

Have you looked at the IPython module ( pydoc IPython )? perhaps you can access IPython utilities through pure Python code.

0
source

Can you do this.

Here is an example.

This is the contents of the a.ipy file:

 %run b.ipy print(myvar) print(myfunc()) 

This is the contents of the 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" the "modules" not the way you do it in python, but the way you do it in the shell with source .

But I'm not sure if this is right, maybe it is. At least it works and allows you to extract common functions and reuse them from other script files.

0
source

Source: https://habr.com/ru/post/1286615/


All Articles