Mastering the rich features of IPython (2)

Previously, explanations for Using IPython's rich features and How to use IPython Notebook 72d744554f0454139c0f), Start IPython notebook server I wrote the method.

This time, I will continue to write about switching profiles, customizing prompts, and using the interactive debugger (pdb). Interactive debuggers are especially important, and if you can master them, your development productivity will increase dramatically.

Profile switching

The default profile can be generated with the following command:

ipython profile create default

This will create a directory like .ipython / profile_default in the location set as your home directory. This default part is the name of the profile.

You can mainly set the following items in the profile.

You can specify the profile name by specifying options such as ipython --profile = default when starting IPython. More information on profiles can be found in the Documentation (http://ipython.org/ipython-doc/2/config/intro.html#profiles).

Prompt customization

profile_default / ipython_config.py defines the overall settings for the IPython shell prompt. You can read more about this file in the IPython documentation (http://ipython.org/ipython-doc/2/development/config.html#python-configuration-files).

In this file, an instance is first created as c = get_config (), and the environment is set by setting the value of this instance. For example, in my environment, the prompt is as follows.

c.PromptManager.out_template = r'[\#]: '
c.PromptManager.in2_template = r'{color.Green}[{color.LightGreen}\D{color.Green}]: '
c.PromptManager.in_template = r'{color.Yellow}\h{color.LightBlue}[{color.LightCyan}\T{color.LightBlue}]{color.Green}[{color.LightGreen}\#{color.Green}]: '

out defines the output line, in2 defines the continuation line, and in defines the input line.

0.png

In the configuration file, the c instance is used for operation, but the% config magic command is used to operate the environment from the shell.

You can also refer to Documentation for prompt extension.

In the above example, some libraries are imported by the initialization script as written earlier.

Magic command and shell command

Commands that start with% are called magic commands. If you type only% and enter a tab, IPython's tab completion function will display the list. For example,% pwd will display the current directory. % who displays the namespace of a valid instance.

hoge = "fuga"
%who
#=> hoge

Also, commands starting with! Are passed to the shell as shell commands.

Interactive debugger

In IPython, the extension of pdb Debugger is available. The% debug command debugs the last exception that occurred. You can set the% pdb command to automatically start the debugger when an exception occurs.

The IPython documentation (http://ipython.org/ipython-doc/2/interactive/reference.html#using-the-python-debugger-pdb) also describes how to use pdb.

If you define the set_trace () and debug () functions as follows, you can write set_trace () anywhere in your code to stop execution and start the debugger.

def set_trace():
    from IPython.core.debugger import Pdb
    Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)

def debug(f, *args, **kwargs):
    from IPython.core.debugger import Pdb
    pdb = Pdb(color_scheme='Linux')
    return pdb.runcall(f, *args, **kwargs)

You can also use the debug () function to launch an interactive debugger for any function. For example, the example below launches an interactive debugger for the function f.

1.png

For example, in this state, pressing a will display a list of the arguments of the current function, pressing w will display the stack trace at the current position, pressing s will step in, and u / d will raise or lower the function call stack. I will. You can also display variables, add arbitrary operations, and so on.

Summary

Such an interactive environment is so convenient and productive that once you learn to develop in IPython, you can't let go of it without it. It's no exaggeration to say that IPython and your favorite editor are enough for developing in Python.

reference

An introduction to data analysis with Python http://www.oreilly.co.jp/books/9784873116556/

Recommended Posts

Mastering the rich features of IPython (2)
Master the rich features of IPython
About the features of Python
I touched some of the new features of Python 3.8 ①
Convenient use of ipython
The beginning of cif2cell
the zen of Python
The story of sys.path.append ()
Features of Go language
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
Scraping the result of "Schedule-kun"
The story of building Zabbix 4.4
Towards the retirement of Python2
[Apache] The story of prefork
Compare the fonts of jupyter-themes
About the ease of Python
Features of programming languages [Memo]
Explain the code of Tensorflow_in_ROS
Reuse the results of clustering
GoPiGo3 of the old man
Calculate the number of changes
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
First python ② Try to write code while examining the features of python
About the SystemChannels API to take advantage of Flutter platform-specific features