##// END OF EJS Templates
Merge pull request #5650 from titsworth/load_from_namespace...
Jonathan Frederic -
r16388:f3e43272 merge
parent child Browse files
Show More
@@ -0,0 +1,6 b''
1 Adds object inspection to %load magic so that source for objects in user or global namespaces can be loaded. To enable searching the namespace, use the ``-n`` option.
2
3 .. sourcecode:: ipython
4
5 In [1]: %load -n my_module.some_function
6
@@ -3089,7 +3089,7 b' class InteractiveShell(SingletonConfigurable):'
3089 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3089 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3090 return "\n".join(x for _, _, x in lines)
3090 return "\n".join(x for _, _, x in lines)
3091
3091
3092 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True):
3092 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3093 """Get a code string from history, file, url, or a string or macro.
3093 """Get a code string from history, file, url, or a string or macro.
3094
3094
3095 This is mainly used by magic functions.
3095 This is mainly used by magic functions.
@@ -3156,11 +3156,18 b' class InteractiveShell(SingletonConfigurable):'
3156 elif os.path.isdir(os.path.expanduser(tgt)):
3156 elif os.path.isdir(os.path.expanduser(tgt)):
3157 raise ValueError("'%s' is a directory, not a regular file." % target)
3157 raise ValueError("'%s' is a directory, not a regular file." % target)
3158
3158
3159 if search_ns:
3160 # Inspect namespace to load object source
3161 object_info = self.object_inspect(target, detail_level=1)
3162 if object_info['found'] and object_info['source']:
3163 return object_info['source']
3164
3159 try: # User namespace
3165 try: # User namespace
3160 codeobj = eval(target, self.user_ns)
3166 codeobj = eval(target, self.user_ns)
3161 except Exception:
3167 except Exception:
3162 raise ValueError(("'%s' was not found in history, as a file, url, "
3168 raise ValueError(("'%s' was not found in history, as a file, url, "
3163 "nor in the user namespace.") % target)
3169 "nor in the user namespace.") % target)
3170
3164 if isinstance(codeobj, string_types):
3171 if isinstance(codeobj, string_types):
3165 return codeobj
3172 return codeobj
3166 elif isinstance(codeobj, Macro):
3173 elif isinstance(codeobj, Macro):
@@ -272,7 +272,8 b' class CodeMagics(Magics):'
272 Usage:\\
272 Usage:\\
273 %load [options] source
273 %load [options] source
274
274
275 where source can be a filename, URL, input history range or macro
275 where source can be a filename, URL, input history range, macro, or
276 element in the user namespace
276
277
277 Options:
278 Options:
278
279
@@ -285,6 +286,8 b' class CodeMagics(Magics):'
285
286
286 -y : Don't ask confirmation for loading source above 200 000 characters.
287 -y : Don't ask confirmation for loading source above 200 000 characters.
287
288
289 -n : Include the user's namespace when searching for source code.
290
288 This magic command can either take a local filename, a URL, an history
291 This magic command can either take a local filename, a URL, an history
289 range (see %history) or a macro as argument, it will prompt for
292 range (see %history) or a macro as argument, it will prompt for
290 confirmation before loading source with more than 200 000 characters, unless
293 confirmation before loading source with more than 200 000 characters, unless
@@ -297,14 +300,18 b' class CodeMagics(Magics):'
297 %load -r 5-10 myscript.py
300 %load -r 5-10 myscript.py
298 %load -r 10-20,30,40: foo.py
301 %load -r 10-20,30,40: foo.py
299 %load -s MyClass,wonder_function myscript.py
302 %load -s MyClass,wonder_function myscript.py
303 %load -n MyClass
304 %load -n my_module.wonder_function
300 """
305 """
301 opts,args = self.parse_options(arg_s,'ys:r:')
306 opts,args = self.parse_options(arg_s,'yns:r:')
302
307
303 if not args:
308 if not args:
304 raise UsageError('Missing filename, URL, input history range, '
309 raise UsageError('Missing filename, URL, input history range, '
305 'or macro.')
310 'macro, or element in the user namespace.')
311
312 search_ns = 'n' in opts
306
313
307 contents = self.shell.find_user_code(args)
314 contents = self.shell.find_user_code(args, search_ns=search_ns)
308
315
309 if 's' in opts:
316 if 's' in opts:
310 try:
317 try:
@@ -57,6 +57,17 b' for playing with examples from documentation, such as matplotlib.'
57 ...:
57 ...:
58 ...: plt.show()
58 ...: plt.show()
59
59
60 The ``%load`` magic can also load source code from objects in the user or
61 global namespace by invoking the ``-n`` option.
62
63 .. sourcecode:: ipython
64
65 In [1]: import hello_world
66 ...: %load -n hello_world.say_hello
67
68 In [3]: def say_hello() :
69 ...: print("Hello World!")
70
60 Inline Matplotlib
71 Inline Matplotlib
61 =================
72 =================
62
73
General Comments 0
You need to be logged in to leave comments. Login now