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