Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,434 +1,442 b'' | |||
|
1 | 1 | ''' IPython customization API |
|
2 | 2 | |
|
3 | 3 | Your one-stop module for configuring & extending ipython |
|
4 | 4 | |
|
5 | 5 | The API will probably break when ipython 1.0 is released, but so |
|
6 | 6 | will the other configuration method (rc files). |
|
7 | 7 | |
|
8 | 8 | All names prefixed by underscores are for internal use, not part |
|
9 | 9 | of the public api. |
|
10 | 10 | |
|
11 | 11 | Below is an example that you can just put to a module and import from ipython. |
|
12 | 12 | |
|
13 | 13 | A good practice is to install the config script below as e.g. |
|
14 | 14 | |
|
15 | 15 | ~/.ipython/my_private_conf.py |
|
16 | 16 | |
|
17 | 17 | And do |
|
18 | 18 | |
|
19 | 19 | import_mod my_private_conf |
|
20 | 20 | |
|
21 | 21 | in ~/.ipython/ipythonrc |
|
22 | 22 | |
|
23 | 23 | That way the module is imported at startup and you can have all your |
|
24 | 24 | personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME |
|
25 | 25 | stuff) in there. |
|
26 | 26 | |
|
27 | 27 | ----------------------------------------------- |
|
28 | 28 | import IPython.ipapi |
|
29 | 29 | ip = IPython.ipapi.get() |
|
30 | 30 | |
|
31 | 31 | def ankka_f(self, arg): |
|
32 | 32 | print "Ankka",self,"says uppercase:",arg.upper() |
|
33 | 33 | |
|
34 | 34 | ip.expose_magic("ankka",ankka_f) |
|
35 | 35 | |
|
36 | 36 | ip.magic('alias sayhi echo "Testing, hi ok"') |
|
37 | 37 | ip.magic('alias helloworld echo "Hello world"') |
|
38 | 38 | ip.system('pwd') |
|
39 | 39 | |
|
40 | 40 | ip.ex('import re') |
|
41 | 41 | ip.ex(""" |
|
42 | 42 | def funcci(a,b): |
|
43 | 43 | print a+b |
|
44 | 44 | print funcci(3,4) |
|
45 | 45 | """) |
|
46 | 46 | ip.ex("funcci(348,9)") |
|
47 | 47 | |
|
48 | 48 | def jed_editor(self,filename, linenum=None): |
|
49 | 49 | print "Calling my own editor, jed ... via hook!" |
|
50 | 50 | import os |
|
51 | 51 | if linenum is None: linenum = 0 |
|
52 | 52 | os.system('jed +%d %s' % (linenum, filename)) |
|
53 | 53 | print "exiting jed" |
|
54 | 54 | |
|
55 | 55 | ip.set_hook('editor',jed_editor) |
|
56 | 56 | |
|
57 | 57 | o = ip.options |
|
58 | 58 | o.autocall = 2 # FULL autocall mode |
|
59 | 59 | |
|
60 | 60 | print "done!" |
|
61 | 61 | ''' |
|
62 | 62 | |
|
63 | 63 | # stdlib imports |
|
64 | 64 | import __builtin__ |
|
65 | 65 | import sys |
|
66 | 66 | |
|
67 | 67 | # our own |
|
68 | 68 | #from IPython.genutils import warn,error |
|
69 | 69 | |
|
70 | 70 | class TryNext(Exception): |
|
71 | 71 | """Try next hook exception. |
|
72 | 72 | |
|
73 | 73 | Raise this in your hook function to indicate that the next hook handler |
|
74 | 74 | should be used to handle the operation. If you pass arguments to the |
|
75 | 75 | constructor those arguments will be used by the next hook instead of the |
|
76 | 76 | original ones. |
|
77 | 77 | """ |
|
78 | 78 | |
|
79 | 79 | def __init__(self, *args, **kwargs): |
|
80 | 80 | self.args = args |
|
81 | 81 | self.kwargs = kwargs |
|
82 | 82 | |
|
83 | 83 | class IPyAutocall: |
|
84 | 84 | """ Instances of this class are always autocalled |
|
85 | 85 | |
|
86 | 86 | This happens regardless of 'autocall' variable state. Use this to |
|
87 | 87 | develop macro-like mechanisms. |
|
88 | 88 | """ |
|
89 | 89 | |
|
90 | 90 | def set_ip(self,ip): |
|
91 | 91 | """ Will be used to set _ip point to current ipython instance b/f call |
|
92 | 92 | |
|
93 | 93 | Override this method if you don't want this to happen. |
|
94 | 94 | |
|
95 | 95 | """ |
|
96 | 96 | self._ip = ip |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | # contains the most recently instantiated IPApi |
|
100 | 100 | |
|
101 | 101 | class IPythonNotRunning: |
|
102 | 102 | """Dummy do-nothing class. |
|
103 | 103 | |
|
104 | 104 | Instances of this class return a dummy attribute on all accesses, which |
|
105 | 105 | can be called and warns. This makes it easier to write scripts which use |
|
106 | 106 | the ipapi.get() object for informational purposes to operate both with and |
|
107 | 107 | without ipython. Obviously code which uses the ipython object for |
|
108 | 108 | computations will not work, but this allows a wider range of code to |
|
109 | 109 | transparently work whether ipython is being used or not.""" |
|
110 | 110 | |
|
111 | 111 | def __init__(self,warn=True): |
|
112 | 112 | if warn: |
|
113 | 113 | self.dummy = self._dummy_warn |
|
114 | 114 | else: |
|
115 | 115 | self.dummy = self._dummy_silent |
|
116 | 116 | |
|
117 | 117 | def __str__(self): |
|
118 | 118 | return "<IPythonNotRunning>" |
|
119 | 119 | |
|
120 | 120 | __repr__ = __str__ |
|
121 | 121 | |
|
122 | 122 | def __getattr__(self,name): |
|
123 | 123 | return self.dummy |
|
124 | 124 | |
|
125 | 125 | def _dummy_warn(self,*args,**kw): |
|
126 | 126 | """Dummy function, which doesn't do anything but warn.""" |
|
127 | 127 | |
|
128 | 128 | print ("IPython is not running, this is a dummy no-op function") |
|
129 | 129 | |
|
130 | 130 | def _dummy_silent(self,*args,**kw): |
|
131 | 131 | """Dummy function, which doesn't do anything and emits no warnings.""" |
|
132 | 132 | pass |
|
133 | 133 | |
|
134 | 134 | _recent = None |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | def get(allow_dummy=False,dummy_warn=True): |
|
138 | 138 | """Get an IPApi object. |
|
139 | 139 | |
|
140 | 140 | If allow_dummy is true, returns an instance of IPythonNotRunning |
|
141 | 141 | instead of None if not running under IPython. |
|
142 | 142 | |
|
143 | 143 | If dummy_warn is false, the dummy instance will be completely silent. |
|
144 | 144 | |
|
145 | 145 | Running this should be the first thing you do when writing extensions that |
|
146 | 146 | can be imported as normal modules. You can then direct all the |
|
147 | 147 | configuration operations against the returned object. |
|
148 | 148 | """ |
|
149 | 149 | global _recent |
|
150 | 150 | if allow_dummy and not _recent: |
|
151 | 151 | _recent = IPythonNotRunning(dummy_warn) |
|
152 | 152 | return _recent |
|
153 | 153 | |
|
154 | 154 | class IPApi: |
|
155 | 155 | """ The actual API class for configuring IPython |
|
156 | 156 | |
|
157 | 157 | You should do all of the IPython configuration by getting an IPApi object |
|
158 | 158 | with IPython.ipapi.get() and using the attributes and methods of the |
|
159 | 159 | returned object.""" |
|
160 | 160 | |
|
161 | 161 | def __init__(self,ip): |
|
162 | 162 | |
|
163 | 163 | # All attributes exposed here are considered to be the public API of |
|
164 | 164 | # IPython. As needs dictate, some of these may be wrapped as |
|
165 | 165 | # properties. |
|
166 | 166 | |
|
167 | 167 | self.magic = ip.ipmagic |
|
168 | 168 | |
|
169 | 169 | self.system = ip.system |
|
170 | 170 | |
|
171 | 171 | self.set_hook = ip.set_hook |
|
172 | 172 | |
|
173 | 173 | self.set_custom_exc = ip.set_custom_exc |
|
174 | 174 | |
|
175 | 175 | self.user_ns = ip.user_ns |
|
176 | 176 | |
|
177 | 177 | self.set_crash_handler = ip.set_crash_handler |
|
178 | 178 | |
|
179 | 179 | # Session-specific data store, which can be used to store |
|
180 | 180 | # data that should persist through the ipython session. |
|
181 | 181 | self.meta = ip.meta |
|
182 | 182 | |
|
183 | 183 | # The ipython instance provided |
|
184 | 184 | self.IP = ip |
|
185 | 185 | |
|
186 | 186 | global _recent |
|
187 | 187 | _recent = self |
|
188 | 188 | |
|
189 | 189 | # Use a property for some things which are added to the instance very |
|
190 | 190 | # late. I don't have time right now to disentangle the initialization |
|
191 | 191 | # order issues, so a property lets us delay item extraction while |
|
192 | 192 | # providing a normal attribute API. |
|
193 | 193 | def get_db(self): |
|
194 | 194 | """A handle to persistent dict-like database (a PickleShareDB object)""" |
|
195 | 195 | return self.IP.db |
|
196 | 196 | |
|
197 | 197 | db = property(get_db,None,None,get_db.__doc__) |
|
198 | 198 | |
|
199 | 199 | def get_options(self): |
|
200 | 200 | """All configurable variables.""" |
|
201 | 201 | |
|
202 | 202 | # catch typos by disabling new attribute creation. If new attr creation |
|
203 | 203 | # is in fact wanted (e.g. when exposing new options), do allow_new_attr(True) |
|
204 | 204 | # for the received rc struct. |
|
205 | 205 | |
|
206 | 206 | self.IP.rc.allow_new_attr(False) |
|
207 | 207 | return self.IP.rc |
|
208 | 208 | |
|
209 | 209 | options = property(get_options,None,None,get_options.__doc__) |
|
210 | 210 | |
|
211 | 211 | def expose_magic(self,magicname, func): |
|
212 | 212 | ''' Expose own function as magic function for ipython |
|
213 | 213 | |
|
214 | 214 | def foo_impl(self,parameter_s=''): |
|
215 | 215 | """My very own magic!. (Use docstrings, IPython reads them).""" |
|
216 | 216 | print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>' |
|
217 | 217 | print 'The self object is:',self |
|
218 | 218 | |
|
219 | 219 | ipapi.expose_magic("foo",foo_impl) |
|
220 | 220 | ''' |
|
221 | 221 | |
|
222 | 222 | import new |
|
223 | 223 | im = new.instancemethod(func,self.IP, self.IP.__class__) |
|
224 | 224 | setattr(self.IP, "magic_" + magicname, im) |
|
225 | 225 | |
|
226 | 226 | def ex(self,cmd): |
|
227 | 227 | """ Execute a normal python statement in user namespace """ |
|
228 | 228 | exec cmd in self.user_ns |
|
229 | 229 | |
|
230 | 230 | def ev(self,expr): |
|
231 | 231 | """ Evaluate python expression expr in user namespace |
|
232 | 232 | |
|
233 | 233 | Returns the result of evaluation""" |
|
234 | 234 | return eval(expr,self.user_ns) |
|
235 | 235 | |
|
236 | 236 | def runlines(self,lines): |
|
237 | 237 | """ Run the specified lines in interpreter, honoring ipython directives. |
|
238 | 238 | |
|
239 | 239 | This allows %magic and !shell escape notations. |
|
240 | 240 | |
|
241 | 241 | Takes either all lines in one string or list of lines. |
|
242 | 242 | """ |
|
243 | 243 | if isinstance(lines,basestring): |
|
244 | 244 | self.IP.runlines(lines) |
|
245 | 245 | else: |
|
246 | 246 | self.IP.runlines('\n'.join(lines)) |
|
247 | 247 | |
|
248 | def to_user_ns(self,vars): | |
|
248 | def to_user_ns(self,vars, interactive = True): | |
|
249 | 249 | """Inject a group of variables into the IPython user namespace. |
|
250 | 250 | |
|
251 | 251 | Inputs: |
|
252 | 252 | |
|
253 | 253 | - vars: string with variable names separated by whitespace |
|
254 | 254 | |
|
255 | - interactive: if True (default), the var will be listed with | |
|
256 | %whos et. al. | |
|
257 | ||
|
255 | 258 | This utility routine is meant to ease interactive debugging work, |
|
256 | 259 | where you want to easily propagate some internal variable in your code |
|
257 | 260 | up to the interactive namespace for further exploration. |
|
258 | 261 | |
|
259 | 262 | When you run code via %run, globals in your script become visible at |
|
260 | 263 | the interactive prompt, but this doesn't happen for locals inside your |
|
261 | 264 | own functions and methods. Yet when debugging, it is common to want |
|
262 | 265 | to explore some internal variables further at the interactive propmt. |
|
263 | 266 | |
|
264 | 267 | Examples: |
|
265 | 268 | |
|
266 | 269 | To use this, you first must obtain a handle on the ipython object as |
|
267 | 270 | indicated above, via: |
|
268 | 271 | |
|
269 | 272 | import IPython.ipapi |
|
270 | 273 | ip = IPython.ipapi.get() |
|
271 | 274 | |
|
272 | 275 | Once this is done, inside a routine foo() where you want to expose |
|
273 | 276 | variables x and y, you do the following: |
|
274 | 277 | |
|
275 | 278 | def foo(): |
|
276 | 279 | ... |
|
277 | 280 | x = your_computation() |
|
278 | 281 | y = something_else() |
|
279 | 282 | |
|
280 | 283 | # This pushes x and y to the interactive prompt immediately, even |
|
281 | 284 | # if this routine crashes on the next line after: |
|
282 | 285 | ip.to_user_ns('x y') |
|
283 | 286 | ... |
|
284 | 287 | # return |
|
285 | 288 | |
|
286 | 289 | If you need to rename variables, just use ip.user_ns with dict |
|
287 | 290 | and update: |
|
288 | 291 | |
|
289 | 292 | # exposes variables 'foo' as 'x' and 'bar' as 'y' in IPython |
|
290 | 293 | # user namespace |
|
291 | 294 | ip.user_ns.update(dict(x=foo,y=bar)) |
|
292 | 295 | """ |
|
293 | 296 | |
|
294 | 297 | # print 'vars given:',vars # dbg |
|
295 | 298 | # Get the caller's frame to evaluate the given names in |
|
296 | 299 | cf = sys._getframe(1) |
|
297 | 300 | |
|
298 | 301 | user_ns = self.user_ns |
|
299 | ||
|
302 | config_ns = self.IP.user_config_ns | |
|
300 | 303 | for name in vars.split(): |
|
301 | 304 | try: |
|
302 |
|
|
|
305 | val = eval(name,cf.f_globals,cf.f_locals) | |
|
306 | user_ns[name] = val | |
|
307 | if not interactive: | |
|
308 | config_ns[name] = val | |
|
309 | else: | |
|
310 | config_ns.pop(name,None) | |
|
303 | 311 | except: |
|
304 | 312 | print ('could not get var. %s from %s' % |
|
305 | 313 | (name,cf.f_code.co_name)) |
|
306 | 314 | |
|
307 | 315 | def expand_alias(self,line): |
|
308 | 316 | """ Expand an alias in the command line |
|
309 | 317 | |
|
310 | 318 | Returns the provided command line, possibly with the first word |
|
311 | 319 | (command) translated according to alias expansion rules. |
|
312 | 320 | |
|
313 | 321 | [ipython]|16> _ip.expand_aliases("np myfile.txt") |
|
314 | 322 | <16> 'q:/opt/np/notepad++.exe myfile.txt' |
|
315 | 323 | """ |
|
316 | 324 | |
|
317 | 325 | pre,fn,rest = self.IP.split_user_input(line) |
|
318 | 326 | res = pre + self.IP.expand_aliases(fn,rest) |
|
319 | 327 | return res |
|
320 | 328 | |
|
321 | 329 | def defalias(self, name, cmd): |
|
322 | 330 | """ Define a new alias |
|
323 | 331 | |
|
324 | 332 | _ip.defalias('bb','bldmake bldfiles') |
|
325 | 333 | |
|
326 | 334 | Creates a new alias named 'bb' in ipython user namespace |
|
327 | 335 | """ |
|
328 | 336 | |
|
329 | 337 | |
|
330 | 338 | nargs = cmd.count('%s') |
|
331 | 339 | if nargs>0 and cmd.find('%l')>=0: |
|
332 | 340 | raise Exception('The %s and %l specifiers are mutually exclusive ' |
|
333 | 341 | 'in alias definitions.') |
|
334 | 342 | |
|
335 | 343 | else: # all looks OK |
|
336 | 344 | self.IP.alias_table[name] = (nargs,cmd) |
|
337 | 345 | |
|
338 | 346 | def defmacro(self, *args): |
|
339 | 347 | """ Define a new macro |
|
340 | 348 | |
|
341 | 349 | 2 forms of calling: |
|
342 | 350 | |
|
343 | 351 | mac = _ip.defmacro('print "hello"\nprint "world"') |
|
344 | 352 | |
|
345 | 353 | (doesn't put the created macro on user namespace) |
|
346 | 354 | |
|
347 | 355 | _ip.defmacro('build', 'bldmake bldfiles\nabld build winscw udeb') |
|
348 | 356 | |
|
349 | 357 | (creates a macro named 'build' in user namespace) |
|
350 | 358 | """ |
|
351 | 359 | |
|
352 | 360 | import IPython.macro |
|
353 | 361 | |
|
354 | 362 | if len(args) == 1: |
|
355 | 363 | return IPython.macro.Macro(args[0]) |
|
356 | 364 | elif len(args) == 2: |
|
357 | 365 | self.user_ns[args[0]] = IPython.macro.Macro(args[1]) |
|
358 | 366 | else: |
|
359 | 367 | return Exception("_ip.defmacro must be called with 1 or 2 arguments") |
|
360 | 368 | |
|
361 | 369 | def set_next_input(self, s): |
|
362 | 370 | """ Sets the 'default' input string for the next command line. |
|
363 | 371 | |
|
364 | 372 | Requires readline. |
|
365 | 373 | |
|
366 | 374 | Example: |
|
367 | 375 | |
|
368 | 376 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
369 | 377 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
370 | 378 | """ |
|
371 | 379 | |
|
372 | 380 | self.IP.rl_next_input = s |
|
373 | 381 | |
|
374 | 382 | |
|
375 | 383 | def launch_new_instance(user_ns = None): |
|
376 | 384 | """ Make and start a new ipython instance. |
|
377 | 385 | |
|
378 | 386 | This can be called even without having an already initialized |
|
379 | 387 | ipython session running. |
|
380 | 388 | |
|
381 | 389 | This is also used as the egg entry point for the 'ipython' script. |
|
382 | 390 | |
|
383 | 391 | """ |
|
384 | 392 | ses = make_session(user_ns) |
|
385 | 393 | ses.mainloop() |
|
386 | 394 | |
|
387 | 395 | |
|
388 | 396 | def make_user_ns(user_ns = None): |
|
389 | 397 | """Return a valid user interactive namespace. |
|
390 | 398 | |
|
391 | 399 | This builds a dict with the minimal information needed to operate as a |
|
392 | 400 | valid IPython user namespace, which you can pass to the various embedding |
|
393 | 401 | classes in ipython. |
|
394 | 402 | """ |
|
395 | 403 | |
|
396 | 404 | if user_ns is None: |
|
397 | 405 | # Set __name__ to __main__ to better match the behavior of the |
|
398 | 406 | # normal interpreter. |
|
399 | 407 | user_ns = {'__name__' :'__main__', |
|
400 | 408 | '__builtins__' : __builtin__, |
|
401 | 409 | } |
|
402 | 410 | else: |
|
403 | 411 | user_ns.setdefault('__name__','__main__') |
|
404 | 412 | user_ns.setdefault('__builtins__',__builtin__) |
|
405 | 413 | |
|
406 | 414 | return user_ns |
|
407 | 415 | |
|
408 | 416 | |
|
409 | 417 | def make_user_global_ns(ns = None): |
|
410 | 418 | """Return a valid user global namespace. |
|
411 | 419 | |
|
412 | 420 | Similar to make_user_ns(), but global namespaces are really only needed in |
|
413 | 421 | embedded applications, where there is a distinction between the user's |
|
414 | 422 | interactive namespace and the global one where ipython is running.""" |
|
415 | 423 | |
|
416 | 424 | if ns is None: ns = {} |
|
417 | 425 | return ns |
|
418 | 426 | |
|
419 | 427 | |
|
420 | 428 | def make_session(user_ns = None): |
|
421 | 429 | """Makes, but does not launch an IPython session. |
|
422 | 430 | |
|
423 | 431 | Later on you can call obj.mainloop() on the returned object. |
|
424 | 432 | |
|
425 | 433 | Inputs: |
|
426 | 434 | |
|
427 | 435 | - user_ns(None): a dict to be used as the user's namespace with initial |
|
428 | 436 | data. |
|
429 | 437 | |
|
430 | 438 | WARNING: This should *not* be run when a session exists already.""" |
|
431 | 439 | |
|
432 | 440 | import IPython |
|
433 | 441 | return IPython.Shell.start(user_ns) |
|
434 | 442 |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now