Master the rich features of IPython

IPython is a very powerful interactive environment that is essential for exploratory data analysis. You will be able to execute code from an interactive operating environment, and you will be able to fully experience the productivity of the interpreted language. I think you can mention pry in Ruby as a similar environment, but IPython may be one step ahead in the perfection of the interactive shell. I think not.

Use the latest IPython

The IPython community is developing fast, from version 1.0 released last summer 2013 to IPython 2.0 released this April 2014. Up to /version2.0.html), 650 pull requests have been merged with 400 issues and 4000 commits. Also, the 3.0 series is currently under development, and its roadmap can be found on GitHub. It has evolved in various points such as support for Python 3.x series based on a single code, and security has also been improved, so if you are using the old 1.x series, immediately update to the latest 2.0 series. Should be done. The latest version at the time of writing was released on 8/6 and has just been security fix version 2.2. /ipython/ipython/issues?q=milestone%3A2.2).

You can check the latest information from here. In addition, upgrade to the latest version as follows.

ipython --version #Check the version of IPython
pip install --upgrade ipython #Upgrade to the latest if old

You can also upgrade pip-installed packages to the latest, except for frozen ones:

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs pip install -U

Initialization script

I think that Python always has a standard library to import. For example, sys, os and numpy as np.

It will be executed if you put the script you want to execute when IPython starts in the ~ / .ipython / profile_default / startup / directory. Note that the reading order is the dictionary order of the file names, so it will be easier to manage if you add a number to the beginning such as 00-first.py like the GNU / Linux daemon startup script.

import sys, os
import readline
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt

It would be convenient to write at least this much.

Introspection

A single? Before or after an object displays the docstring, and a?? Displays the source code. The source code is very powerful because it is syntax highlighted and displayed depending on the environment. Being able to easily see the code for each function in the library you want to use helps you understand the behavior.

ipython
df = pd.read_csv("data.csv")
df?? #The source code of the data frame is displayed
df.plot?? #The source code of the plot function is displayed(You can check options etc.)

Executing existing code

It's too nonsense to copy the source code into an interactive shell because you want to test a piece of code in the middle of writing.

IPython can run existing source code with the% run command. Commands that start with this% are called magic commands. You can see what magic commands exist by pressing the tab key after the%. You can usually guess the meaning by just looking at the name.

%run ~/source/your_code.py

In the above example, not only will your_code.py be executed, but objects such as classes, variables, etc. will still be available in IPython.

When copying and pasting the shell

There are times when you want to paste the code into an interactive shell, albeit a bit clunky. My recommendation is to use% cpaste.

%cpaste
#Then paste the code

Save history

Last time was also introduced at the end, but you can save the history to a file with readline.write_history_file.

readline.write_history_file("history.py")

It is also convenient to log with% logstart to save the entire session. By default, the history is saved in a file called ipython_log.py.

Other rich features

IPython has a lot of useful features that I can't even introduce. Read the original documentation is a good choice.

Recommended Posts

Master the rich features of IPython
Mastering the rich features of IPython (2)
About the features of Python
I touched some of the new features of Python 3.8 ①
Convenient use of ipython
The meaning of self
Master the inclusion notation
the zen of Python
The story of sys.path.append ()
Features of Go language
A story about changing the master name of BlueZ
Main features of ChainMap
Revenge of the Types: Revenge of types
Measure the importance of features with a random forest tool
I want to know the features of Python and pip
Try to extract the features of the sensor data with CNN
Align the version of chromedriver_binary
Scraping the result of "Schedule-kun"
10. Counting the number of lines
Towards the retirement of Python2
Get the number of digits
Features of programming languages [Memo]
Explain the code of Tensorflow_in_ROS
Reuse the results of clustering
GoPiGo3 of the old man
Launch the IPython notebook server
Calculate the number of changes
Change the theme of Jupyter
The popularity of programming languages
Change the style of matplotlib
Visualize the orbit of Hayabusa2
About the components of Luigi
Connected components of the graph
Filter the output of tracemalloc
Simulation of the contents of the wallet
The Power of Pandas: Python
The story of outputting the planetarium master in pdf format with Pycairo
About the SystemChannels API to take advantage of Flutter platform-specific features