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