Show More
@@ -0,0 +1,251 b'' | |||||
|
1 | ================== | |||
|
2 | IPython vs IPython | |||
|
3 | ================== | |||
|
4 | ||||
|
5 | This document is meant to highlight the main differences between the python | |||
|
6 | language and what are the specific construct you can do only in IPython. | |||
|
7 | ||||
|
8 | Unless expressed otherwise all of the construct you will see here will raise a | |||
|
9 | ``SyntaxError`` if run in a pure Python shell, or if executing in a Python | |||
|
10 | script. | |||
|
11 | ||||
|
12 | Each of these features are describe more in details in further part of the documentation. | |||
|
13 | ||||
|
14 | ||||
|
15 | Quick overview: | |||
|
16 | =============== | |||
|
17 | ||||
|
18 | ||||
|
19 | All the following construct are valid IPython syntax : | |||
|
20 | ||||
|
21 | .. code-block:: | |||
|
22 | ||||
|
23 | ? | |||
|
24 | ||||
|
25 | .. code-block:: | |||
|
26 | ||||
|
27 | ?object | |||
|
28 | ||||
|
29 | ||||
|
30 | .. code-block:: | |||
|
31 | ||||
|
32 | object? | |||
|
33 | ||||
|
34 | .. code-block:: | |||
|
35 | ||||
|
36 | *patern*? | |||
|
37 | ||||
|
38 | .. code-block:: | |||
|
39 | ||||
|
40 | %shell like --syntax | |||
|
41 | ||||
|
42 | .. code-block:: | |||
|
43 | ||||
|
44 | !ls | |||
|
45 | ||||
|
46 | .. code-block:: | |||
|
47 | ||||
|
48 | my_files =! ls ~/ | |||
|
49 | for i,file in enumerate(my_file): | |||
|
50 | raw = !echo $file | |||
|
51 | !echo {files[0].upper()} $raw | |||
|
52 | ||||
|
53 | ||||
|
54 | .. code-block:: | |||
|
55 | ||||
|
56 | %%perl magic --function | |||
|
57 | @months = ("July", "August", "September"); | |||
|
58 | print $months[0]; | |||
|
59 | ||||
|
60 | ||||
|
61 | Each of these construct is compile by IPython into valid python code and will | |||
|
62 | do most of the time what you expect it will do. Let see each of these example | |||
|
63 | in more detail. | |||
|
64 | ||||
|
65 | ||||
|
66 | ||||
|
67 | ||||
|
68 | ||||
|
69 | Accessing help | |||
|
70 | ============== | |||
|
71 | ||||
|
72 | As IPython is mostly an interactive shell, the question mark is a simple | |||
|
73 | shortcut to get help. A question mark alone will bring up the IPython help: | |||
|
74 | ||||
|
75 | .. code-block:: | |||
|
76 | ||||
|
77 | In[1]:? | |||
|
78 | ||||
|
79 | IPython -- An enhanced Interactive Python | |||
|
80 | ========================================= | |||
|
81 | ||||
|
82 | IPython offers a combination of convenient shell features, special commands | |||
|
83 | and a history mechanism for both input (command history) and output (results | |||
|
84 | caching, similar to Mathematica). It is intended to be a fully compatible | |||
|
85 | replacement for the standard Python interpreter, while offering vastly | |||
|
86 | improved functionality and flexibility. | |||
|
87 | ||||
|
88 | At your system command line, type 'ipython -h' to see the command line | |||
|
89 | options available. This document only describes interactive features. | |||
|
90 | ||||
|
91 | MAIN FEATURES | |||
|
92 | ------------- | |||
|
93 | ... | |||
|
94 | ||||
|
95 | A single question mark before, or after an object available in current | |||
|
96 | namespace will show help relative to this object: | |||
|
97 | ||||
|
98 | .. code-block:: | |||
|
99 | ||||
|
100 | In [6]: object? | |||
|
101 | Docstring: The most base type | |||
|
102 | Type: type | |||
|
103 | ||||
|
104 | ||||
|
105 | A double question mark will try to pull out more information about the object, | |||
|
106 | and if possible display the python source code of this object. | |||
|
107 | ||||
|
108 | .. code-block:: | |||
|
109 | ||||
|
110 | In[1]: import collections | |||
|
111 | In[2]: collection.Counter? | |||
|
112 | ||||
|
113 | Init signature: collections.Counter(*args, **kwds) | |||
|
114 | Source: | |||
|
115 | class Counter(dict): | |||
|
116 | '''Dict subclass for counting hashable items. Sometimes called a bag | |||
|
117 | or multiset. Elements are stored as dictionary keys and their counts | |||
|
118 | are stored as dictionary values. | |||
|
119 | ||||
|
120 | >>> c = Counter('abcdeabcdabcaba') # count elements from a string | |||
|
121 | ||||
|
122 | >>> c.most_common(3) # three most common elements | |||
|
123 | [('a', 5), ('b', 4), ('c', 3)] | |||
|
124 | >>> sorted(c) # list all unique elements | |||
|
125 | ['a', 'b', 'c', 'd', 'e'] | |||
|
126 | >>> ''.join(sorted(c.elements())) # list elements with repetitions | |||
|
127 | 'aaaaabbbbcccdde' | |||
|
128 | ... | |||
|
129 | ||||
|
130 | ||||
|
131 | ||||
|
132 | If you are looking for an object, the use of wildcards ``*`` in conjonction | |||
|
133 | with question mark will allow you to search current namespace for object with | |||
|
134 | mathcing names: | |||
|
135 | ||||
|
136 | .. code-block:: | |||
|
137 | In [24]: *int*? | |||
|
138 | FloatingPointError | |||
|
139 | int | |||
|
140 | ||||
|
141 | ||||
|
142 | ||||
|
143 | Shell Assignment | |||
|
144 | ================ | |||
|
145 | ||||
|
146 | ||||
|
147 | When doing interactive computing it is common to need to access the underlying shell. | |||
|
148 | This is doable through th use of the exclamation mark ``!`` (or bang). | |||
|
149 | ||||
|
150 | This allow to execute simple command when present in beginning of line: | |||
|
151 | ||||
|
152 | .. code-block:: | |||
|
153 | ||||
|
154 | In[1]: !pwd | |||
|
155 | /User/home/ | |||
|
156 | ||||
|
157 | Change directory: | |||
|
158 | ||||
|
159 | .. code-block:: | |||
|
160 | ||||
|
161 | In[1]: !cd /var/etc | |||
|
162 | ||||
|
163 | Or edit file: | |||
|
164 | ||||
|
165 | .. code-block:: | |||
|
166 | ||||
|
167 | In[1]: !mvim myfile.txt | |||
|
168 | ||||
|
169 | ||||
|
170 | The line after the bang can can call any program installed in the underlying | |||
|
171 | shell, and support variable expansion in the form of ``$variable`` or ``{variable}``. | |||
|
172 | The later for of expansion support arbitrary python expression: | |||
|
173 | ||||
|
174 | .. code-block:: | |||
|
175 | ||||
|
176 | In[1]: file = 'myfile.txt' | |||
|
177 | ||||
|
178 | In[2]: !mv $file {file.upper()} | |||
|
179 | ||||
|
180 | ||||
|
181 | The bang can also be present in the right hand side of an assignment, just | |||
|
182 | after the equal sign, or separated from it by a white space. In which case the | |||
|
183 | standard output of the command after the bang ``!`` will be spilt out into line | |||
|
184 | in a list-like object (:see:`IPython Slist`) and assign to the left hand side. | |||
|
185 | ||||
|
186 | This allow you for example to put th list of files of the current working directory in a variable: | |||
|
187 | ||||
|
188 | .. code-block:: | |||
|
189 | ||||
|
190 | In[1]: my_files != ls | |||
|
191 | ||||
|
192 | ||||
|
193 | You can combine the different possibilities in for loops, condition, functions...: | |||
|
194 | ||||
|
195 | .. code-block:: | |||
|
196 | ||||
|
197 | my_files =! ls ~/ | |||
|
198 | b = "backup file" | |||
|
199 | for i,file in enumerate(my_file): | |||
|
200 | raw = !echo $backup $file | |||
|
201 | !cp $file {file.split('.')[0]+'.bak'} | |||
|
202 | ||||
|
203 | ||||
|
204 | Magics | |||
|
205 | ------ | |||
|
206 | ||||
|
207 | Magics function are often present in the form of shell-like syntax, but are | |||
|
208 | under the hood python function. The syntax and assignment possibility are | |||
|
209 | similar to the one with the bang (``~``) syntax, but with more flexibility and | |||
|
210 | power. Magic function start with a percent sign (``%``) or double percent (``%%``). | |||
|
211 | ||||
|
212 | A magic call with a sign percent will act only one line: | |||
|
213 | ||||
|
214 | .. code-block:: | |||
|
215 | ||||
|
216 | In[1]: %xmode | |||
|
217 | Exception reporting mode: Verbose | |||
|
218 | ||||
|
219 | And support assignment: | |||
|
220 | ||||
|
221 | .. code-block:: | |||
|
222 | ||||
|
223 | In [1]: results = %timeit -r1 -n1 -o list(range(1000)) | |||
|
224 | 1 loops, best of 1: 21.1 µs per loop | |||
|
225 | ||||
|
226 | In [2]: results | |||
|
227 | Out[2]: <TimeitResult : 1 loops, best of 1: 21.1 µs per loop> | |||
|
228 | ||||
|
229 | Magic with two percent sign can spread over multiple lines, but do not support assignment: | |||
|
230 | ||||
|
231 | .. code-block:: | |||
|
232 | ||||
|
233 | In[1]: %%bash | |||
|
234 | ... : echo "My shell is:" $SHELL | |||
|
235 | ... : echo "My disk usage is:" | |||
|
236 | ... : df -h | |||
|
237 | My shell is: /usr/local/bin/bash | |||
|
238 | My disk usage is: | |||
|
239 | Filesystem Size Used Avail Capacity iused ifree %iused Mounted on | |||
|
240 | /dev/disk1 233Gi 216Gi 16Gi 94% 56788108 4190706 93% / | |||
|
241 | devfs 190Ki 190Ki 0Bi 100% 656 0 100% /dev | |||
|
242 | map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net | |||
|
243 | map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /hom | |||
|
244 | ||||
|
245 | ||||
|
246 | Combining it all | |||
|
247 | ---------------- | |||
|
248 | ||||
|
249 | .. code-block:: | |||
|
250 | ||||
|
251 | find a snippet that comboine all that into one thing ! |
General Comments 0
You need to be logged in to leave comments.
Login now