Original: http://kochiya.me/www/posts/Ipython!.html
A few days ago accidentally dragged a copy of learning IPython for Interactive Computing and Data Visualizatioon the company's web site, as the name says, The main content of this book is to discuss the use of Ipython and the visualization of data. Because it is very thin, yesterday in order to pass the time casually turned over, read the first three chapters, recognize a problem: I have never really used ipython--before just very simple to think of Ipython as a support auto-completion and indentation of the interactive shell, until really read the Ipython document , only to find this kind of cognition quite rude.
This note is basically the first two chapters of the book mentioned above and the official copy as the main reference, if you want to further study, please be sure to see. Official copy attached to this: documents, books, I did not search, but there should be a PDF version of the online, if it is not found, I can also upload a copy.
This article does not involve installation steps, and also assumes that the reader understands the meaning of a particular term (such as introspection, profile).
Preliminary
Once you enter Ipython,ipython you will be prompted to type one of the following four commands to try to use Ipython:
| command name |
Description |
| ? |
Introduction and overview of Ipython features |
| %quickref |
A handbook that contains all the commands |
| Help |
Python's Help system |
| Object? |
For more information about object, if you type object?? will be more detailed |
Ok, we start here, of course, first of all, it is recommended that you look at quickref. You notice that this command starts with%. All methods that begin with% are known as magic Methods (Magic function), which are some of the methods built into Ipython. It is important to note that the Magic method has% and percent of the percentage, such as %timeit and %%timeit . This thing has a special name in the Ipython, the former called the line magic latter cell magic . As the name implies, the former is specific to a single line of commands, the latter for multiple lines of command (this is the term in notebook, about notebook see last), as it is.
All the Magic method can view the source code, carefully read the words, to write their own codes are also very useful, the specific method is in the Magic method behind the addition ?? , for example %timeit?? . The code is highlighted and formatted, and it should be easy to read. If you have a curious curiosity, you must have tried it just now %timeit? . The difference is, this time no source appears, right. But other information, such as the type of what the instructional content will show.
The Magic method will be discussed in a special section below, and here is an impression.
Now, let's talk ? .
? In fact, the above has been involved, in short you know you can use the Magic method to obtain more detailed information. But it's not just that, it's actually a way to modify all of Python's objects. Considering that everything in Python is an object, so you know, what? In fact, you can see all the variables (here is the variable, so 1?) Why do you get an error? Hmm, huh? )。
Try entering the following code and you will have a better understanding.
Inch[1]:A=[1,2,3,4]in [2]: ain [3]: list ? in [4]: import osin [5]: os< Span class= "Err"? in [6]: os.< span class= "n" >open? In [7]: super
The above represent variables, classes, packages, methods in classes, built-in functions, as you can see, are working well. ?replace them ?? with more detailed information, but ? usually enough (for me). In addition, ? as a prefix and as a suffix effect is the same, but I am more accustomed to put in the back.
The above content is the most basic part of Ipython, please be familiar with it.
Magic method
Ipython a very (most) important feature is the Magic method, but the built-in magic method is too many points (try to use to %lsmagic see all the Magic method), this note is certainly impossible to detail all the Magic method, here will pick some commonly used to say, right to make a point.
Pwd
If you look closely at the previous article, you will find that this command does not have a%, but this is also a magic method, because, in fact, the PWD command is just a link, pointing to the%pwd method,%pwd the command pointing to the system. Of course, this command has a clear meaning, that is, printing the current path. Ipython has done similar processing for some of the bash commands commonly used under Linux, which is quite handy to use. I tried it, roughly pwd,cd,whos,history,rm,ll,ls,mv,cp,alias,mkdir .
!xxx
Ipython Although a lot of commonly used commands are encapsulated, but after all, not a lot, such as suddenly want to execute vim or top the kind of Linux command, still need to cut out? It doesn't really need to, because Ipython provides an extra ! syntax to execute Linux commands directly. For example, I need to execute the top command, so just write it in Ipython !top .
More complex Linux commands are also supported, in fact, as long as ! you add, you can use Ipython as a bash.
Also, you can assign values, such as this:
files = !ls -1 -S | grep hello
%edit
The editor is used so frequently under Ipython that Ipython has a%edit method.
However, the default effect is estimated to disappoint you, because if not specified, Linux defaults to use VI as the editor, under Windows is notepad++ (seems?). )。 If you want%edit to open your favorite editor, you need to add the editor entry in path.
%run
Ipython is generally used only to test whether a line or a few lines of code can be executed. The reason, or because the command line writing long code is not convenient to write code in the file, on the one hand is the format, on the other hand, on the command line to write code, once the middle code is wrong, the modification is still very troublesome. So we have to quit Ipython, to python xxx.py execute. Or Tmux open two windows to test.
But in fact Ipython has provided a very convenient mechanism for executing a script, that is, the%run command,%run can be simplified to run.
Use is similar to familiar python test.py , use run test.py is good. The Run command can also be attached with options, which -t will print out the executed CPU timings when the script finishes executing. If you specify an -t option, you can follow one -Nx , and X represents a number. Indicates that x times are executed repeatedly.
The more useful option is -d that this will run the script in the PDB. Followed by a -bxx break point. Personally there is no experience in Python debugging and you are interested to try it yourself.
There is also an -p option that opens the Python Profiler to print detailed execution times, function calls, and so forth for optimal reference.
%timeit
This command is used to test the time of execution of a command, and in general, it repeats 100 times to run the command, and then takes out the best 3 results to average.
Try executing the following command:
In[1[x*range(100000)]
Similarly, you can specify options.
-nSpecify the number of repetitions, default 100
-rIs the average of how many results, the default is 3
Incidentally, the results of the%timeit are much faster than the real run, because the garbage collection (GC) is temporarily shut down during Timeit execution.
Using Python variables in the shell
In fact, this is not a magic method, but it is not appropriate to list alone, so it is written here.
Take a look at the following code:
' Data '$folder%folder
Notice, mkdir is a magic method, it is a link to%mkdir,%mkdir is actually the mkdir method in the shell. The folder is a Python variable. However, it is true that the command under the shell accesses the Python variable, just before the variable name $ . If you are familiar with shell scripts, this approach should be very cordial.
%bookmark
This method is usually used to memorize the path, for example, you need a CD into a directory each time, so github/miko/loli/shoujo you can bookmark this path, so you %bookmark shoujo github/miko/loli/shoujo can do this later cd shoujo .
By the way, if no path is specified, the current path is used by default.
%alias
As the name suggests, this method is used to specify aliases. However, Ipython's%alias can also be passed as a parameter.
Take a look at the following paragraph:
[1%-1s[2circles
It's easy to understand, isn't it? The parameter followed by%alias is the alias, which can then be specified by%s and passed in when used.
%store
Default, aliases will only exist in this session, if you want to use this alias next time, use the%store method to save it.%store latest
In addition, it does not matter if it is not saved, it %store -r restores the alias of the last session.
%hist
The history in Ipython will save all the records in the session, so there will be quite a lot of records for every other year.
Although I know you all use Up,down to find history ... Well, try ctrl+r it, there's a surprise.
In addition, similar to what hist 3-7 , it seems that I do not have to explain it.
-gThe option is similar to grep, you can look up something, then -n display the line number, -f save the history to the file, -p show the line number as n:>>> , and -n show the result of the input (I guess not much of it ...).
Introspection
Explained at the beginning ? ?? , if you are familiar with Python, you will realize that this is actually introspection.
Here are a few of the methods that might not be used ...
%psource,%pfile,%pdoc,%pdef
In fact, it is the result of the excerpt ... Well, is anyone going to use it?
Notebook
The most amazing thing about Ipython (I think) is this notebook feature, but to enable this, you need to install it first pyZMQ . Then execute ipython notebook , hey, what you see. A web-based Ipython shell!
Notebook will actually open a server (tornado) on the local 8888 port, and the first page you see, usually called notebook dashboard , will list all the notebook.
Each notebook file is an extended file format called. IPYNB, which is a file that contains JSON.
The file contains the input output obtained from the interactive session, and then there are some metadata for Ipython to use.
The code snippet in notebook has a special term called cell , which is described in advance.
Notebook use and Ipython is similar, but it is much more convenient to modify, and can build multiple notebook, so use a variety of convenience. However, the need for networking libraries should not be used, I test requests did not succeed ...
Perform a cell use ctrl+Enter , execute and create a new notebook use shift+enter , of course you can poke the button above is also possible.
You can use typing ctrl+M 然后 H to know that keyboard shortcuts are available.
The Magic method can also be used in the cell, and, because of the unique advantages of notebook, you might want to test multiple lines of code, so don't forget to %% replace it % .
There is a special method in the cell that %%file allows you to create a file that is used as follows:
PercentNyanya. TXTworldnyanya
It's simple, isn't it?
Then there's another %%! way. This method can execute some bash commands.
Finally, notebook can also act as a markdown editor ...
Finally
Above about Ipython has said enough, if you read carefully, I believe you will be surprised: the original Ipython can do so much! It would be my pleasure if I could give you such an idea.
However, after all, only so many words, in fact, the discussion is still not enough, more, you are afraid to use in practice to know some beautiful usage and will remember more magic methods.
But you will slowly become skilled and fall in love with it, I believe.
Thank you for reading.
Above.
I have been to the animation exhibition and have been in the hole to lie in bed do not want to rise as.
Basic functions of Ipython (RPM)