官术网_书友最值得收藏!

Configuring IPython

IPython has an elaborate configuration and customization system. The components of the system are as follows:

  • IPython provides default profiles, but we can create our own profiles
  • Various settable options for the shell, kernel, Qt console, and notebook
  • Customization of prompts and colors
  • Extensions we saw in Keeping track of package versions and history in IPython notebooks
  • Startup files per profile

I will demonstrate some of these components in this recipe.

Getting ready

You need IPython for this recipe, so (if necessary) have a look at the Getting ready section of Keeping track of package versions and history in IPython notebooks.

How to do it...

Let's start with a startup file. I have a directory in my home directory at .ipython/profile_default/startup, which belongs to the default profile. This directory is meant for startup files. IPython detects Python files in this directory and executes them in lexical order of filenames. Because of the lexical order, it is convenient to name the startup files with a combination of digits and strings, for example, 0000-watermark.py. Put the following code in the startup file:

get_ipython().magic('%load_ext watermark')
get_ipython().magic('watermark -a "Ivan Idris" -v -p numpy,scipy,matplotlib -c \'%b %Y\' -w')

This startup file loads the extension we used in Keeping track of package versions and history in IPython notebooks and shows information about package versions. Other use cases include importing modules and defining functions. IPython stores commands in a SQLite database, so you could gather statistics to find common usage patterns. The following script prints source lines and associated counts from the database for the default profile sorted by counts (the code is in the ipython_history.py file in this book's code bundle):

import sqlite3
from IPython.utils.path import get_ipython_dir
import pprint
import os

def print_history(file):
    with sqlite3.connect(file) as con:
        c = con.cursor()
 c.execute("SELECT count(source_raw) as csr,\
 source_raw FROM history\
 GROUP BY source_raw\
 ORDER BY csr")
        result = c.fetchall()
        pprint.pprint(result)
        c.close()

hist_file = '%s/profile_default/history.sqlite' % get_ipython_dir()

if os.path.exists(hist_file):
    print_history(hist_file)
else:
    print("%s doesn't exist" % hist_file)

The highlighted SQL query does the bulk of the work. The code is self-explanatory. If it is not clear, I recommend reading Chapter 8, Text Mining and Social Network, of my book Python Data Analysis, Packt Publishing.

The other configuration option I mentioned is profiles. We can use the default profiles or create our own profiles on a per project or functionality basis. Profiles act as sandboxes and you can configure them separately. Here's the command to create a profile:

$ ipython profile create [newprofile]

The configuration files are Python files and their names end with _config.py. In these files, you can set various IPython options. Set the option to automatically log the IPython session as follows:

c = get_config()

c.TerminalInteractiveShell.logstart=True

The first line is usually included in configuration files and gets the root IPython configuration object. The last line tells IPython that we want to start logging immediately on startup so you don't have to type %logstart.

Alternatively, you can also set the log file name with the following command:

c.TerminalInteractiveShell.logfile='mylog_file.py'

You can also use the following configuration line that ensures logging in append mode:

c.TerminalInteractiveShell.logappend='mylog_file.py'

See also

主站蜘蛛池模板: 康定县| 若尔盖县| 藁城市| 英超| 手游| 嵩明县| 昂仁县| 潞西市| 什邡市| 休宁县| 丹凤县| 崇州市| 新巴尔虎左旗| 盱眙县| 诏安县| 白朗县| 关岭| 汝南县| 奎屯市| 无锡市| 宜宾市| 泽普县| 五河县| 平舆县| 临澧县| 汪清县| 汶上县| 翁源县| 米脂县| 咸阳市| 广南县| 云南省| 洪泽县| 唐海县| 扶余县| 清远市| 天全县| 监利县| 兴城市| 上虞市| 射洪县|