Show More
@@ -652,47 +652,33 b" typing ``!ls`` will run 'ls' in the current directory." | |||
|
652 | 652 | Manual capture of command output |
|
653 | 653 | -------------------------------- |
|
654 | 654 | |
|
655 | If the input line begins with two exclamation marks, !!, the command is | |
|
656 | executed but its output is captured and returned as a python list, split | |
|
657 | on newlines. Any output sent by the subprocess to standard error is | |
|
658 | printed separately, so that the resulting list only captures standard | |
|
659 | output. The !! syntax is a shorthand for the %sx magic command. | |
|
660 | ||
|
661 | Finally, the %sc magic (short for 'shell capture') is similar to %sx, | |
|
662 | but allowing more fine-grained control of the capture details, and | |
|
663 | storing the result directly into a named variable. The direct use of | |
|
664 | %sc is now deprecated, and you should ise the ``var = !cmd`` syntax | |
|
665 | instead. | |
|
655 | You can assign the result of a system command to a Python variable with the | |
|
656 | syntax ``myfiles = !ls``. This gets machine readable output from stdout | |
|
657 | (e.g. without colours), and splits on newlines. To explicitly get this sort of | |
|
658 | output without assigning to a variable, use two exclamation marks (``!!ls``) or | |
|
659 | the ``%sx`` magic command. | |
|
660 | ||
|
661 | The captured list has some convenience features. ``myfiles.n`` or ``myfiles.s`` | |
|
662 | returns a string delimited by newlines or spaces, respectively. ``myfiles.p`` | |
|
663 | produces `path objects <http://pypi.python.org/pypi/path.py>`_ from the list items. | |
|
666 | 664 | |
|
667 | 665 | IPython also allows you to expand the value of python variables when |
|
668 |
making system calls. |
|
|
669 | with $ will get expanded before the system call is made:: | |
|
666 | making system calls. Wrap variables or expressions in {braces}:: | |
|
670 | 667 | |
|
671 | In [1]: pyvar='Hello world' | |
|
672 |
In [2]: !echo "A python variable: |
|
|
668 | In [1]: pyvar = 'Hello world' | |
|
669 | In [2]: !echo "A python variable: {pyvar}" | |
|
673 | 670 | A python variable: Hello world |
|
671 | In [3]: import math | |
|
672 | In [4]: x = 8 | |
|
673 | In [5]: !echo {math.factorial(x)} | |
|
674 | 40320 | |
|
674 | 675 | |
|
675 | If you want the shell to actually see a literal $, you need to type it | |
|
676 | twice:: | |
|
677 | ||
|
678 | In [3]: !echo "A system variable: $$HOME" | |
|
679 | A system variable: /home/fperez | |
|
680 | ||
|
681 | You can pass arbitrary expressions, though you'll need to delimit them | |
|
682 | with {} if there is ambiguity as to the extent of the expression:: | |
|
676 | For simple cases, you can alternatively prepend $ to a variable name:: | |
|
683 | 677 | |
|
684 | In [5]: x=10 | |
|
685 | In [6]: y=20 | |
|
686 | In [13]: !echo $x+y | |
|
687 | 10+y | |
|
688 | In [7]: !echo ${x+y} | |
|
689 | 30 | |
|
690 | ||
|
691 | Even object attributes can be expanded:: | |
|
692 | ||
|
693 | In [12]: !echo $sys.argv | |
|
678 | In [6]: !echo $sys.argv | |
|
694 | 679 | [/home/fperez/usr/bin/ipython] |
|
695 | ||
|
680 | In [7]: !echo "A system variable: $$HOME" # Use $$ for literal $ | |
|
681 | A system variable: /home/fperez | |
|
696 | 682 | |
|
697 | 683 | System command aliases |
|
698 | 684 | ---------------------- |
@@ -74,9 +74,9 b' After an exception occurs, you can call ``%debug`` to jump into the Python' | |||
|
74 | 74 | debugger (pdb) and examine the problem. Alternatively, if you call ``%pdb``, |
|
75 | 75 | IPython will automatically start the debugger on any uncaught exception. You can |
|
76 | 76 | print variables, see code, execute statements and even walk up and down the |
|
77 |
call stack to track down the true source of the problem. |
|
|
78 | %run and pdb active can be an efficient way to develop and debug code, in many | |
|
79 |
|
|
|
77 | call stack to track down the true source of the problem. This can be an efficient | |
|
78 | way to develop and debug code, in many cases eliminating the need for print | |
|
79 | statements or external debugging tools. | |
|
80 | 80 | |
|
81 | 81 | You can also step through a program from the beginning by calling |
|
82 | 82 | ``%run -d theprogram.py``. |
@@ -88,10 +88,9 b' IPython stores both the commands you enter, and the results it produces. You' | |||
|
88 | 88 | can easily go through previous commands with the up- and down-arrow keys, or |
|
89 | 89 | access your history in more sophisticated ways. |
|
90 | 90 | |
|
91 |
Input and output history are kept in variables called ``In`` and ``Out``, |
|
|
92 | can both be indexed by the prompt number on which they occurred, e.g. ``In[4]``. | |
|
93 | The last three objects in output history are also kept in variables named ``_``, | |
|
94 | ``__`` and ``___``. | |
|
91 | Input and output history are kept in variables called ``In`` and ``Out``, keyed | |
|
92 | by the prompt numbers, e.g. ``In[4]``. The last three objects in output history | |
|
93 | are also kept in variables named ``_``, ``__`` and ``___``. | |
|
95 | 94 | |
|
96 | 95 | You can use the ``%history`` magic function to examine past input and output. |
|
97 | 96 | Input history from previous sessions is saved in a database, and IPython can be |
General Comments 0
You need to be logged in to leave comments.
Login now