📜 ⬆️ ⬇️

IPython: replacing the standard Python shell

The Python shell is quite a handy thing for testing and exploring the possibilities of the language, someone even uses it as a calculator (which is very convenient, by the way), in this series of articles I would like to talk about IPython - replacing the standard Python shell that the advanced feature list that makes working with it more efficient.


You can download and install IPython from the official site.

Namespace


We start the IPython shell with the 'ipython' command, as in the standard Python shell, we can assign values ​​to variables:
In [1]: msgflag = 1

In [2]: pi = 3

In [3]: msg = "hello"

In [4]: warning = None

During input, you can use autocomplete, for example, by typing msg and pressing Tab, we will get the possible options:
In [5]: msg
msg msgflag

If the option is one, it will automatically appear in the input field.
')
IPython provides the ability to use namespaces, for example, the 'who' command displays a list of available variables:
In [5]: who
msg msgflag pi warning

It has a number of options, for example, to display int type variables, you should write:
In [6]: who int
msgflag pi

If you need additional information about variables, you can use the 'whos' command:
 In [7]: whos Variable Type Data/Info -------------------------------- msg str hello msgflag int 1 pi int 3 warning NoneType None 

It is also possible to search by variable name, for this you need to use the 'psearch' command:
In [8]: psearch msg*
msg
msgflag

It is possible to extend the search criteria with a variable type, for example:
In [9]: psearch msg* int
msgflag

In order to save the variable in the profile (it will be available after restarting IPython) there is a command 'store':
In [10]: store msgflag
Stored 'msgflag' (int)

It is also possible to save the variable to a separate file:
In [11]: store msgflag > /tmp/m.txt
Writing 'msgflag' (int) to file '/tmp/m.txt'.

To view the stored variables, enter the command 'store' with no parameters:
In [12]: store
Stored variables and their in-db values:
msgflag -> 1

To clear the current namespace, use the 'reset' command and make sure that it is cleared using the 'who' command:
In [13]: reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y

In [14]: who
Interactive namespace is empty.

You can restore the variables that were previously saved with the 'store' command and make sure that they are restored with the 'who' command:
In [15]: store -r

In [16]: who
msgflag

You can also clear everything in the store and the next time you start the namespace will be empty:
In [17]: store -z

Logging


Another useful feature of IPython is logging. The 'logstate' command checks the current state of the logger:
In [18]: logstate
Logging has not been activated.

Using the 'logstart' command, you can start logging:
In [19]: logstart
Activating auto-logging. Current session state plus future input saved.
Filename : ipython_log.py
Mode : rotate
Output logging : False
Raw input log : False
Timestamping : False
State : active

The log is saved in the current directory as Python source code.
The 'logon' and 'logoff' commands are used to enable / disable logging:
In [20]: logoff
Switching logging OFF

In [21]: logon
Switching logging ON

Magic teams


The commands described above in IPython terminology are called magic commands. In order to see the full list of magic commands, you need to enter 'lsmagic':
In [22]: lsmagic
Available magic functions:
%Exit %Pprint %Quit %alias %autocall %autoindent %automagic %bg %bookmark %cd %clear %color_info %colors %cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %exit %hist %history %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %p %page %pdb %pdef %pdoc %pfile %pinfo %popd %profile %prun %psearch %psource %pushd %pwd %pycat %quickref %quit %r %rehash %rehashx %rep %reset %run %runlog %save %sc %store %sx %system_verbose %time %timeit %unalias %upgrade %who %who_ls %whos %xmode

Automagic is ON, % prefix NOT needed for magic functions.

Magic commands can use the percent sign as a prefix. This is useful if the variable name matches the command name.
To call for help on all magic commands, use the 'magic' command. To get help on a certain magic command, simply put a question mark at the end:
In [23]: lsmagic?
Type: Magic function
Base Class: <type 'instancemethod'>
String Form: <bound method InteractiveShell.magic_lsmagic of <IPython.iplib.InteractiveShell object at 0x9e5ef0>>
Namespace: IPython internal
File: /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/ipython-0.9.1-py2.5.egg/IPython/Magic.py
Definition: lsmagic(self, parameter_s='')
Docstring:
List currently available magic functions.

For more information, you should enter 2 question marks at the end of the command, in this case, the source code of the command function is also displayed.

For variables defined by yourself, you can also use a question mark:
In [24]: msgflag?
Type: int
Base Class: <type 'int'>
String Form: 1
Namespace: Interactive
Docstring:
int(x[, base]) -> integer

Convert a string or number to an integer, if possible. A floating point
argument will be truncated towards zero ...

Some "cosmetic" teams


You can use 'p' as an abbreviation for 'print':
In [25]: import sys

In [26]: p sys.path
['', '/Library/Frameworks/Python.framework/Versions/2.5/bin', '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg', '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/bdist_mpkg-0.4.3-py2.5.egg', '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/macholib-1.1-py2.5.egg',
...


You can also use an abbreviated form to call functions:
In [27]: def x(a, b):
....: print a, b
....:
....:

In [28]: x 3, 4
-------> x(3, 4)
3 4


To call a function, you can do this:
In [29]: def x(): print "123"
....:

In [30]: /x
-------> x()
123


You can call a function that accepts strings as parameters, without having to enclose them in quotes:
In [31]: def x(a,b): print "%s-%s" % (a, b)
....:

In [32]: , x astr bstr
-------> x("astr", "bstr")
astr-bstr

Introspection


If you forget what arguments are needed to call a function or method, you can always find out with the help of the 'pdef' command:
In [37]: import re

In [38]: pdef re.match
re.match(pattern, string, flags=0)


You can also see the documentation for this function:
In [39]: pdoc re.match
Class Docstring:
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
Calling Docstring:
x.__call__(...) <==> x(...)


All information about the function can be viewed using the 'pinfo' command:
In [40]: pinfo re.match
Type: function
Base Class: <type 'function'>
String Form: <function match at 0xaa2b70>
Namespace: Interactive
File: /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py
Definition: re.match(pattern, string, flags=0)
Docstring:
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.


You can also see the source code of the function:
In [41]: psource re.match
def match(pattern, string, flags=0):
"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).match(string)


If you want to see the source code of the file in which an object is defined, use the 'pfile' command:
In [42]: pfile re.match


You can also run your favorite editor to edit the file in which this or that object is defined:
In [43]: edit -x re.match
Editing...

References to sources


Screencast - http://showmedo.com/videos/video?name=1000010&fromSeriesID=100
Official documentation - http://ipython.scipy.org/moin/Documentation

PS: That's all for now, if this topic is interesting to someone, then in the following articles I will continue to describe the features of IPython.

Source: https://habr.com/ru/post/49685/


All Articles