##// END OF EJS Templates
Merge pull request #13024 from aadcg/master...
Merge pull request #13024 from aadcg/master Documentation fixes

File last commit:

r26662:55d4c79f
r26663:b2605dd3 merge
Show More
python-ipython-diff.rst
240 lines | 6.4 KiB | text/x-rst | RstLexer
/ docs / source / interactive / python-ipython-diff.rst
Matthias Bussonnier
Fix typos spotted damianavila and njs
r21667 =================
Python vs IPython
=================
Matthias Bussonnier
python-diff
r21662
Matthias Bussonnier
Fix typos spotted damianavila and njs
r21667 This document is meant to highlight the main differences between the Python
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 language and what are the specific constructs you can do only in IPython.
Matthias Bussonnier
python-diff
r21662
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 Unless expressed otherwise all of the constructs you will see here will raise a
Matthias Bussonnier
python-diff
r21662 ``SyntaxError`` if run in a pure Python shell, or if executing in a Python
klonuo
Various small fixes to docs
r22475 script.
Matthias Bussonnier
python-diff
r21662
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 Each of these features is described more in detail in the further parts of the documentation.
Matthias Bussonnier
python-diff
r21662
Quick overview:
===============
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 All the following constructs are valid IPython syntax:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: ?
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: ?object
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: object?
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: *pattern*?
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: %shell like --syntax
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: !ls
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Eren Halici
Styling of bang operator
r23867 In [1]: my_files = !ls ~/
André A. Gomes
Fix erroneous code snippets.
r26585 In [1]: for i, file in enumerate(my_files):
Min RK
fix code blocks in python-ipython-diff...
r21713 ...: raw = !echo $file
André A. Gomes
Fix erroneous code snippets.
r26585 ...: !echo {file[0].upper()} $raw
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: %%perl magic --function
...: @months = ("July", "August", "September");
...: print $months[0];
klonuo
Various small fixes to docs
r22475
Matthias Bussonnier
python-diff
r21662
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 Each of these constructs is compiled by IPython into valid python code and will
Mark Schmitz
fix mistakes introduced, and a few additional words
r26381 do most of the time what you expect it will do. Let's see each of these examples
Matthias Bussonnier
python-diff
r21662 in more detail.
Accessing help
==============
As IPython is mostly an interactive shell, the question mark is a simple
shortcut to get help. A question mark alone will bring up the IPython help:
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 In [1]: ?
Matthias Bussonnier
python-diff
r21662
IPython -- An enhanced Interactive Python
=========================================
IPython offers a combination of convenient shell features, special commands
and a history mechanism for both input (command history) and output (results
caching, similar to Mathematica). It is intended to be a fully compatible
replacement for the standard Python interpreter, while offering vastly
improved functionality and flexibility.
At your system command line, type 'ipython -h' to see the command line
options available. This document only describes interactive features.
MAIN FEATURES
-------------
...
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 A single question mark before or after an object available in the current
Mark Schmitz
fix mistakes introduced, and a few additional words
r26381 namespace will show help relative to this object:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In [6]: object?
Docstring: The most base type
Type: type
A double question mark will try to pull out more information about the object,
klonuo
Various small fixes to docs
r22475 and if possible display the python source code of this object.
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In[1]: import collections
Pavel Karateev
Fix "collections" module typo
r24843 In[2]: collections.Counter??
Matthias Bussonnier
python-diff
r21662
Init signature: collections.Counter(*args, **kwds)
Source:
class Counter(dict):
'''Dict subclass for counting hashable items. Sometimes called a bag
or multiset. Elements are stored as dictionary keys and their counts
are stored as dictionary values.
>>> c = Counter('abcdeabcdabcaba') # count elements from a string
>>> c.most_common(3) # three most common elements
[('a', 5), ('b', 4), ('c', 3)]
>>> sorted(c) # list all unique elements
['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements())) # list elements with repetitions
'aaaaabbbbcccdde'
...
Mark Schmitz
fix mistakes introduced, and a few additional words
r26381 If you are looking for an object, the use of wildcards ``*`` in conjunction
with a question mark will allow you to search the current namespace for objects with
Matthias Bussonnier
Fix typos spotted damianavila and njs
r21667 matching names:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662 In [24]: *int*?
FloatingPointError
int
print
Shell Assignment
================
Mark Schmitz
fix mistakes introduced, and a few additional words
r26381 When doing interactive computing it is a common need to access the underlying shell.
klonuo
Various small fixes to docs
r22475 This is doable through the use of the exclamation mark ``!`` (or bang).
Matthias Bussonnier
python-diff
r21662
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 This allows to execute simple commands when present in beginning of the line:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In[1]: !pwd
/User/home/
Change directory:
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In[1]: !cd /var/etc
Or edit file:
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In[1]: !mvim myfile.txt
Matthias Bussonnier
Fix typos spotted damianavila and njs
r21667 The line after the bang can call any program installed in the underlying
Matthias Bussonnier
python-diff
r21662 shell, and support variable expansion in the form of ``$variable`` or ``{variable}``.
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 The later form of expansion supports arbitrary python expressions:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In[1]: file = 'myfile.txt'
In[2]: !mv $file {file.upper()}
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 The bang (``!``) can also be present on the right hand side of an assignment, just
after the equal sign, or separated from it by a white space. In this case the
standard output of the command after the bang will be split out into lines
in a list-like object and assigned to the left hand side.
Matthias Bussonnier
python-diff
r21662
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 This allows you, for example, to put the list of files of the current working directory in a variable:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Eren Halici
Styling of bang operator
r23867 In[1]: my_files = !ls
Matthias Bussonnier
python-diff
r21662
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 You can combine the different possibilities in for loops, conditions, functions...:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
Eren Halici
Styling of bang operator
r23867 my_files = !ls ~/
André A. Gomes
Fix erroneous code snippets.
r26585 for i, file in enumerate(my_files):
Matthias Bussonnier
python-diff
r21662 raw = !echo $backup $file
André A. Gomes
Fix erroneous code snippets.
r26585 !cp $file {file.split('.')[0] + '.bak'}
Matthias Bussonnier
python-diff
r21662
Magics
------
Mark Schmitz
fix mistakes introduced, and a few additional words
r26381 Magic functions (magics) are often present in the form of shell-like syntax, but they are
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 python functions under the hood. The syntax and assignment possibilities are
Matthias Bussonnier
Fix typos spotted damianavila and njs
r21667 similar to the one with the bang (``!``) syntax, but with more flexibility and
Mark Schmitz
fix mistakes introduced, and a few additional words
r26381 power. Magic functions start with a percent sign (``%``) or double percent signs (``%%``).
Matthias Bussonnier
python-diff
r21662
Mark Schmitz
fix mistakes introduced, and a few additional words
r26381 A magic call with a single percent sign will act only on one line:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In[1]: %xmode
Exception reporting mode: Verbose
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 Magics support assignment:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In [1]: results = %timeit -r1 -n1 -o list(range(1000))
1 loops, best of 1: 21.1 µs per loop
In [2]: results
Out[2]: <TimeitResult : 1 loops, best of 1: 21.1 µs per loop>
Mark Schmitz
fix grammar/typos in docs/interactive
r26380 Magics with double percent signs (``%%``) can spread over multiple lines, but they do not support assignments:
Matthias Bussonnier
python-diff
r21662
Min RK
fix code blocks in python-ipython-diff...
r21713 .. code-block:: ipython
Matthias Bussonnier
python-diff
r21662
In[1]: %%bash
... : echo "My shell is:" $SHELL
... : echo "My disk usage is:"
... : df -h
My shell is: /usr/local/bin/bash
My disk usage is:
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk1 233Gi 216Gi 16Gi 94% 56788108 4190706 93% /
devfs 190Ki 190Ki 0Bi 100% 656 0 100% /dev
map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /hom