Last active
April 6, 2020 22:29
-
-
Save kpalin/ec5264445dbb56616d6e to your computer and use it in GitHub Desktop.
IPython extension for system calls with file like return value. Good for e.g. reading awk output to pandas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pipex(line): | |
"Execute command and return stdout as file like object" | |
import subprocess as sp | |
cmd = get_ipython().var_expand(line) | |
p = sp.Popen(cmd,shell=True,stdout=sp.PIPE) | |
return p.stdout | |
def load_ipython_extension(ipython): | |
"""This function is called when the extension is loaded. | |
It accepts an IPython InteractiveShell instance. | |
We can register the magic with the `register_magic_function` | |
method of the shell instance.""" | |
ipython.register_magic_function(pipex, 'line') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install by downloading the above snippet of code
Set up for current IPython session
or for default session by adding it to
c.InteractiveShellApp.extensions
list.Usage for e.g. reading awk output to pandas DataFrame