clipy_shity

Magic commands

First of all, we can use built-in magic commands. The full list of commands can be obtained:

In [1]:
%lsmagic
Out[1]:
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

We can use also shell commands:

In [2]:
!ls
!pwd
Untitled.ipynb
iris.csv
/c/temp_folder

It can be useful, for example if we want to use some fast shell command like sed, awk or just

In [5]:
#first 10 lines of the file:
!head -2 iris.csv
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
In [11]:
#how to find a specific line with a string using grep:
!grep -m2 -i 'versicolor' 'iris.csv'
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
In [13]:
#replace string, where searchingStr, newStr are searching string and new string respectively:
!sed -e 's/setosa/versicolor/g' 'iris.csv'
5.1,3.5,1.4,0.2,Iris-versicolor
4.9,3.0,1.4,0.2,Iris-versicolor
4.7,3.2,1.3,0.2,Iris-versicolor
4.6,3.1,1.5,0.2,Iris-versicolor
5.0,3.6,1.4,0.2,Iris-versicolor
5.4,3.9,1.7,0.4,Iris-versicolor
4.6,3.4,1.4,0.3,Iris-versicolor
5.0,3.4,1.5,0.2,Iris-versicolor
4.4,2.9,1.4,0.2,Iris-versicolor
4.9,3.1,1.5,0.1,Iris-versicolor
5.4,3.7,1.5,0.2,Iris-versicolor
4.8,3.4,1.6,0.2,Iris-versicolor
4.8,3.0,1.4,0.1,Iris-versicolor
4.3,3.0,1.1,0.1,Iris-versicolor
5.8,4.0,1.2,0.2,Iris-versicolor
5.7,4.4,1.5,0.4,Iris-versicolor
5.4,3.9,1.3,0.4,Iris-versicolor
5.1,3.5,1.4,0.3,Iris-versicolor
5.7,3.8,1.7,0.3,Iris-versicolor
5.1,3.8,1.5,0.3,Iris-versicolor
5.4,3.4,1.7,0.2,Iris-versicolor
5.1,3.7,1.5,0.4,Iris-versicolor
4.6,3.6,1.0,0.2,Iris-versicolor
5.1,3.3,1.7,0.5,Iris-versicolor
4.8,3.4,1.9,0.2,Iris-versicolor

All of this beauty we can use as variable in our notebook:

In [16]:
files = !ls ./*.csv
files[0]
Out[16]:
'./iris.csv'

Very powerfull command is psource, that can show us signature of functions and source code. Also we can use command ?, ?? for that. For capturing output from pager to our notebook we can overload method (also see, stackoverflow's discussion about it.):

In [24]:
from __future__ import print_function
from IPython.core import page
page.page = print
In [26]:
%psource %psource
    @line_magic
    def psource(self, parameter_s='', namespaces=None):
        """Print (or run through pager) the source code for an object."""
        if not parameter_s:
            raise UsageError('Missing object name.')
        self.shell._inspect('psource',parameter_s, namespaces)

If we want to run other python file or even notebook file (*.ipynb), command %run helps us.

In [ ]: