Ipython Using study notes

Source: Internet
Author: User
Tags aliases clear screen instance method stack trace python script

Reference: http://www.cnblogs.com/zzhzhao/p/5295476.html

Learn "data analysis using Python" chapter III IPython: A kind of interactive computing and development environment notes, shared to everyone, but also for their own as a memo.

Install Ipython with PIP. PS. Bloggers use the Win7 system, so the next thing is to do it under the Windows system.

I. Ipython Foundation

Start: Start Menu-Enter cmd-enter-input Ipython

First attempt

Two. TAB key Auto-complete

When you enter an expression in the shell, any variables (objects, functions, and so on) that match the input string in the current command control are identified as long as the TAB key is pressed.

PS. Before I found out that my ipython does not have the TAB key Auto-complete function, the TAB key function is indentation. The last pip install Pyreadline can be resolved.

In the following example, enter B. and press the <tab> button to get the result.

Three. Introspection

Add a question mark (?) before or after a variable. You can display some common information about the object. This is called introspection of the object.

If the object is a function or an instance method, its docstring will also be realistic.

Use?? The source code for the function is also displayed.

Some strings with a wildcard character (*) display all names that match the wildcard expression.

For example, we can list all the functions in the NumPy top-level namespace that contain "load"

It's so great, there's Wood and!!!!!!!.

Four.%run command

In a Ipython session environment, all files can be run by the%run command as a python program.

Enter the%run path +python file name.

PS. The implementation of code in collective intelligence programming can use this

Five. Interrupt the code being executed (must 23333)

Pressing CTRL-C will trigger a keyboardinterrupt. In addition to some very special cases, most Python programs immediately stop executing

Six. Execute the code in the Clipboard

The book says to use Ctrl+shift+v to paste the code snippet of the Clipboard, but the Windows system does not seem feasible, so the right mouse button is pasted.

%paste can host all the text in the Clipboard and execute it as a whole in the shell.

%cpaste is similar to%paste, except that it has a special prompt for pasting code. If you find that the pasted code is wrong, just press CTRL + C to terminate the%CPASTE hint.

Seven. Keyboard shortcuts

In fact, I can not remember the actual use of 23333, paste this to everyone as a memo.

    1. Ctrl-p or up ARROW key to search command history for commands that begin with the currently entered text
    2. Ctrl-n or DOWN ARROW key forward to search command history with the currently entered text beginning with the command
    3. Ctrl-r Reverse History Search (partial match) read by line
    4. Ctrl-shift-v pasting text from the Clipboard
    5. Ctrl-c aborting code that is currently executing
    6. CTRL-A move the cursor to the beginning of the line
    7. CTRL-E move the cursor to the end of the line
    8. Ctrl-k Delete text from the beginning of the cursor to the end of the line
    9. Ctrl-u clears all text from the current line 12
    10. CTRL-F moves the cursor forward one character
    11. Ctrl-b move the cursor backward one character
    12. Ctrl-l Clear Screen

Eight. Exceptions and tracking

Nine. Magic command

Paste here to make memo

Command description
%quickref Show Quick reference for Ipython
%magic Show detailed documentation of all magic commands
%debug from the bottom of the latest exception trace into the interactive debugger
%hist input (optional output) History of the Print command
%PDB automatically enters the debugger after an exception occurs
%paste executing the Python code in the Clipboard
%cpaste open a special prompt to manually paste the python code you want to execute
%reset Delete all variables/names in the interactive namespace
%page Object prints output object through the pager
%run script.py Execute a Python script file in Ipython
%prun statement performs statement through cprofile and prints the output of the parser
%time Statement report statement execution time
%timeit statement executes the statement multiple times to calculate the average time to perform the ensemble. Useful for code that executes very little time
%who,%who_ls,%whos display variables defined in the interactive namespace, variable information level/redundancy
%xdel variable Delete variable and try to clear all references to its objects in Ipython

10. QT-based Rich GUI console

The Ipython team developed a GUI console based on the QT Framework, which is designed to provide rich text editing capabilities such as inline images, multi-line edits, syntax highlighting, and so on for terminal applications.

Use

Ipython Qtconsole--pylab=inline

To start, you can add drawing functionality to it.

PS. This step is just beginning to fail, and my workaround is:

Pip install Qtconsole, you can run it--..

11. Matplotlib Integration and Pylab mode

Usually we integrate matplotlib by adding--pylab tags when starting Ipython

Note the space Ah ~ is

Ipython--pylab

12. Using the history command

13. Search and Reuse history commands

Historical command with up and down arrows just fine, ctrl+p and CTRL + N too troublesome.

Ctrl+r is used to implement a partial incremental search, press Ctrl+r and enter several characters in the line you want to search. Pressing CTRL+R will loop through each line in the history command that matches the input.

14. Input and Output variables

Ipython saves the last two outputs in _ (an underscore) and __ (two underscore) variables

The input text is saved in a variable named _ix, where x is the line number of the input line. For example, after 27 lines are entered, two new variables _27 (output variable) and _i27 (input variable) are generated

I'll take the eighth line for example. 2333

15. Record input and output

Execution%logstart can start logging

16. Interacting with the operating system

17. Shell commands and aliases (here I do not have a lot of code to do, to be resolved.) )

In Ipython, with an exclamation point (!) The beginning of the command line indicates that all subsequent content needs to be executed in the system shell.

# #再启动一个python解释器

In use! , Ipython also allows the use of Python values defined in the current environment. You just need to precede the variable name with $

Magic Command%alias can customize abbreviations for shell commands.

What's going on here!

18. Directory Bookmark System

Ipython has a simple directory bookmarking system that allows you to save aliases for common directories for quick jumps.

(Here My computer also does not work out, pending.) )

19. Software development Tools

(i) Interactive debugger

One of the best times to debug your code is when the error just happened.

The%debug command (entered immediately after an exception) will invoke which "post debugger" and jump directly to the stack frame where the exception was thrown (stack frame)

Here need to open the ipython_bug.py file, the students need to ask me, the path also need to change their own.

In this debugger, you can execute any Python code and look at all the objects and data in each stack frame (that is, those that explain "leaving the path").

The default starts at the lowest level (that is, where the error occurs).

Enter U (UP) and D (down) to switch between levels of the stack trace

Executing the%pdb command allows Ipython to invoke the debugger automatically after an exception occurs.

In addition, the debugger can help with code development work, especially if you want to set breakpoints or step through functions and scripts to see the execution of each statement.

There are several ways to achieve this:

Ready to be perfected

(ii) Other usage scenarios of the debugger

Ready to be perfected

(iii) test code execution Time:%time and%timeit

Sometimes you might want to know which functions take up the most time in a complex calculation.

Ipython specifically provides two magic functions (%time and%timeit) to automate the process.

%time executes one statement at a time, and then reports the overall execution time.

Example: Here is an array of 600,000 strings, and two different ways to "pick a string that starts with Foo":

#一个非常大的字符串数组strings = [' foo ', ' foobar ', ' baz ', ' qux ', ' python ', ' Guido Van rossum ']*100000method1 = [x for x in strings if X.st Artswith (' foo ')]mathod2 = [x for x in strings if x[:3] = = ' Foo ']

To test by%time:

The cheer gap is out.

But this is not a very precise result, and if you execute%time multiple times with the same statement, you will find that the result will change. (The pro-test will change 233 see)

To get more accurate results, you need to use the Magic function%timeit, which automatically executes multiple times to produce a very precise average execution time for any statement.

(iv) Basic performance analysis:%prun and%run-p

(v) Progressive analysis of function performance

Twenty. Ipython HTML Notebook

IPython notebook uses the browser as the interface to send requests to the IPython server in the background and display the results. Use the unit (cell) to store various information in the browser's interface. There are several types of cells, often using markdown cells that represent formatted text, and code units that represent codes.

Each code unit has an output area, enters the code in the Codes unit, presses Shift-enter to run the code, and the value of the last expression in the code displays the output area. If you want to mask the output, you can add a semicolon after the last statement: ";". In addition, you can use the Print statement to display information in the output area in your code.

You can also use HTML and JavaScript directly in the markdown unit.

My computer can't get in!! Pending resolution

21. Some tips for improving code development efficiency with Ipython

(i) Reload the module dependencies

(ii) Code design tips

1. Preserving meaningful objects and data

2. The flat structure is better than the nesting structure

3. No fear of large files

21. Advanced Ipython function

(a) Make your class more friendly to Ipython

(ii) Personalization and configuration

Ipython Using study notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.