Show More
@@ -181,11 +181,11 b' magic, a cell one and one that works in both modes, using just plain functions:' | |||||
181 |
|
181 | |||
182 | from IPython.core.magic import (register_line_magic, register_cell_magic, |
|
182 | from IPython.core.magic import (register_line_magic, register_cell_magic, | |
183 | register_line_cell_magic) |
|
183 | register_line_cell_magic) | |
184 |
|
184 | |||
185 | @register_line_magic |
|
185 | @register_line_magic | |
186 | def lmagic(line): |
|
186 | def lmagic(line): | |
187 | "my line magic" |
|
187 | "my line magic" | |
188 |
|
|
188 | return line | |
189 |
|
189 | |||
190 | @register_cell_magic |
|
190 | @register_cell_magic | |
191 | def cmagic(line, cell): |
|
191 | def cmagic(line, cell): | |
@@ -195,11 +195,11 b' magic, a cell one and one that works in both modes, using just plain functions:' | |||||
195 | @register_line_cell_magic |
|
195 | @register_line_cell_magic | |
196 | def lcmagic(line, cell=None): |
|
196 | def lcmagic(line, cell=None): | |
197 | "Magic that works both as %lcmagic and as %%lcmagic" |
|
197 | "Magic that works both as %lcmagic and as %%lcmagic" | |
198 |
|
|
198 | if cell is None: | |
199 |
|
|
199 | print("Called as line magic") | |
200 |
|
|
200 | return line | |
201 | else: |
|
201 | else: | |
202 |
|
|
202 | print("Called as cell magic") | |
203 | return line, cell |
|
203 | return line, cell | |
204 |
|
204 | |||
205 | # We delete these to avoid name conflicts for automagic to work |
|
205 | # We delete these to avoid name conflicts for automagic to work | |
@@ -212,10 +212,11 b' potentially hold state in between calls, and that have full access to the main' | |||||
212 | IPython object: |
|
212 | IPython object: | |
213 |
|
213 | |||
214 | .. sourcecode:: python |
|
214 | .. sourcecode:: python | |
215 |
|
215 | |||
216 | # This code can be put in any Python module, it does not require IPython |
|
216 | # This code can be put in any Python module, it does not require IPython | |
217 | # itself to be running already. It only creates the magics subclass but |
|
217 | # itself to be running already. It only creates the magics subclass but | |
218 | # doesn't instantiate it yet. |
|
218 | # doesn't instantiate it yet. | |
|
219 | from __future__ import print_function | |||
219 | from IPython.core.magic import (Magics, magics_class, line_magic, |
|
220 | from IPython.core.magic import (Magics, magics_class, line_magic, | |
220 | cell_magic, line_cell_magic) |
|
221 | cell_magic, line_cell_magic) | |
221 |
|
222 | |||
@@ -226,8 +227,8 b' IPython object:' | |||||
226 | @line_magic |
|
227 | @line_magic | |
227 | def lmagic(self, line): |
|
228 | def lmagic(self, line): | |
228 | "my line magic" |
|
229 | "my line magic" | |
229 |
print |
|
230 | print("Full access to the main IPython object:", self.shell) | |
230 |
print |
|
231 | print("Variables in the user namespace:", list(self.shell.user_ns.keys())) | |
231 | return line |
|
232 | return line | |
232 |
|
233 | |||
233 | @cell_magic |
|
234 | @cell_magic | |
@@ -239,10 +240,10 b' IPython object:' | |||||
239 | def lcmagic(self, line, cell=None): |
|
240 | def lcmagic(self, line, cell=None): | |
240 | "Magic that works both as %lcmagic and as %%lcmagic" |
|
241 | "Magic that works both as %lcmagic and as %%lcmagic" | |
241 | if cell is None: |
|
242 | if cell is None: | |
242 |
print |
|
243 | print("Called as line magic") | |
243 | return line |
|
244 | return line | |
244 | else: |
|
245 | else: | |
245 |
print |
|
246 | print("Called as cell magic") | |
246 | return line, cell |
|
247 | return line, cell | |
247 |
|
248 | |||
248 |
|
249 | |||
@@ -259,11 +260,11 b' additional state, then you should always call the parent constructor and' | |||||
259 | instantiate the class yourself before registration: |
|
260 | instantiate the class yourself before registration: | |
260 |
|
261 | |||
261 | .. sourcecode:: python |
|
262 | .. sourcecode:: python | |
262 |
|
263 | |||
263 | @magics_class |
|
264 | @magics_class | |
264 | class StatefulMagics(Magics): |
|
265 | class StatefulMagics(Magics): | |
265 | "Magics that hold additional state" |
|
266 | "Magics that hold additional state" | |
266 |
|
267 | |||
267 | def __init__(self, shell, data): |
|
268 | def __init__(self, shell, data): | |
268 | # You must call the parent constructor |
|
269 | # You must call the parent constructor | |
269 | super(StatefulMagics, self).__init__(shell) |
|
270 | super(StatefulMagics, self).__init__(shell) | |
@@ -288,8 +289,8 b' follows:' | |||||
288 | .. sourcecode:: python |
|
289 | .. sourcecode:: python | |
289 |
|
290 | |||
290 | def func(self, line): |
|
291 | def func(self, line): | |
291 |
print |
|
292 | print("Line magic called with line:", line) | |
292 |
|
|
293 | print("IPython object:", self.shell) | |
293 |
|
294 | |||
294 | ip = get_ipython() |
|
295 | ip = get_ipython() | |
295 | # Declare this function as the magic %mycommand |
|
296 | # Declare this function as the magic %mycommand | |
@@ -961,7 +962,7 b' standard Python tutorial::' | |||||
961 | In [3]: ... a, b = 0, 1 |
|
962 | In [3]: ... a, b = 0, 1 | |
962 |
|
963 | |||
963 | In [4]: >>> while b < 10: |
|
964 | In [4]: >>> while b < 10: | |
964 |
...: ... print |
|
965 | ...: ... print(b) | |
965 | ...: ... a, b = b, a+b |
|
966 | ...: ... a, b = b, a+b | |
966 | ...: |
|
967 | ...: | |
967 | 1 |
|
968 | 1 |
@@ -62,7 +62,7 b' mechanism, this is automatically stored::' | |||||
62 |
|
62 | |||
63 | hello - this is a temporary file |
|
63 | hello - this is a temporary file | |
64 |
|
64 | |||
65 |
Out[1]: "print |
|
65 | Out[1]: "print('hello - this is a temporary file')\n" | |
66 |
|
66 | |||
67 | Now, if you call ``%edit -p``, IPython tries to open an editor with the |
|
67 | Now, if you call ``%edit -p``, IPython tries to open an editor with the | |
68 | same data as the last time you used %edit. So if you haven't used %edit |
|
68 | same data as the last time you used %edit. So if you haven't used %edit | |
@@ -82,7 +82,7 b' Continuing with the example above, this should illustrate this idea::' | |||||
82 |
|
82 | |||
83 | hello - now I made some changes |
|
83 | hello - now I made some changes | |
84 |
|
84 | |||
85 |
Out[2]: "print |
|
85 | Out[2]: "print('hello - now I made some changes')\n" | |
86 |
|
86 | |||
87 | In [3]: edit _1 |
|
87 | In [3]: edit _1 | |
88 |
|
88 | |||
@@ -94,7 +94,7 b' Continuing with the example above, this should illustrate this idea::' | |||||
94 |
|
94 | |||
95 | IPython version control at work :) |
|
95 | IPython version control at work :) | |
96 |
|
96 | |||
97 |
Out[3]: "print |
|
97 | Out[3]: "print('hello - this is a temporary file')\nprint('IPython version control at work :)')\n" | |
98 |
|
98 | |||
99 |
|
99 | |||
100 | This section was written after a contribution by Alexander Belchenko on |
|
100 | This section was written after a contribution by Alexander Belchenko on |
General Comments 0
You need to be logged in to leave comments.
Login now