Show More
@@ -83,6 +83,8 b' __license__ = Release.license' | |||
|
83 | 83 | |
|
84 | 84 | import re |
|
85 | 85 | |
|
86 | from IPython.iplib import InteractiveShell | |
|
87 | ||
|
86 | 88 | PROMPT_RE = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )') |
|
87 | 89 | |
|
88 | 90 | def prefilter_paste(self,line,continuation): |
@@ -106,13 +108,15 b' def prefilter_paste(self,line,continuation):' | |||
|
106 | 108 | return '' |
|
107 | 109 | else: |
|
108 | 110 | return self._prefilter(line,continuation) |
|
109 | ||
|
110 | # Rebind this to be the new IPython prefilter: | |
|
111 | from IPython.iplib import InteractiveShell | |
|
112 | InteractiveShell.prefilter = prefilter_paste | |
|
113 | 111 | |
|
114 | # Clean up the namespace. | |
|
115 | del InteractiveShell,prefilter_paste | |
|
112 | def activate_prefilter(): | |
|
113 | """Rebind the input-pasting filter to be the new IPython prefilter""" | |
|
114 | InteractiveShell.prefilter = prefilter_paste | |
|
115 | ||
|
116 | def deactivate_prefilter(): | |
|
117 | """Reset the filter.""" | |
|
118 | InteractiveShell.prefilter = InteractiveShell._prefilter | |
|
116 | 119 | |
|
117 | 120 | # Just a heads up at the console |
|
121 | activate_prefilter() | |
|
118 | 122 | print '*** Pasting of code with ">>>" or "..." has been enabled.' |
@@ -42,4 +42,7 b' def main():' | |||
|
42 | 42 | # Use plain exceptions, to also resemble normal pyhton. |
|
43 | 43 | o.xmode = 'plain' |
|
44 | 44 | |
|
45 | # Store the activity flag in the metadata bag from the running shell | |
|
46 | ip.IP.meta.doctest_mode = True | |
|
47 | ||
|
45 | 48 | main() |
@@ -12,14 +12,14 b' def main():' | |||
|
12 | 12 | ip = IPython.ipapi.get() |
|
13 | 13 | |
|
14 | 14 | try: |
|
15 | ip.ex("import scipy") | |
|
16 | 15 | ip.ex("import numpy") |
|
16 | ip.ex("import scipy") | |
|
17 | 17 | |
|
18 | ip.ex("from scipy import *") | |
|
19 | 18 | ip.ex("from numpy import *") |
|
19 | ip.ex("from scipy import *") | |
|
20 | 20 | print "SciPy profile successfully loaded." |
|
21 | 21 | except ImportError: |
|
22 | 22 | print "Unable to start scipy profile, are scipy and numpy installed?" |
|
23 | 23 | |
|
24 | 24 | |
|
25 | main() No newline at end of file | |
|
25 | main() |
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | |
|
4 |
$Id: Magic.py 2 |
|
|
4 | $Id: Magic.py 2601 2007-08-10 07:01:29Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
@@ -2912,4 +2912,90 b' Defaulting color scheme to \'NoColor\'"""' | |||
|
2912 | 2912 | suffix = (sys.platform == 'win32' and '.ini' or '') |
|
2913 | 2913 | (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n') |
|
2914 | 2914 | |
|
2915 | ||
|
2916 | def magic_doctest_mode(self,parameter_s=''): | |
|
2917 | """Toggle doctest mode on and off. | |
|
2918 | ||
|
2919 | This mode allows you to toggle the prompt behavior between normal | |
|
2920 | IPython prompts and ones that are as similar to the default IPython | |
|
2921 | interpreter as possible. | |
|
2922 | ||
|
2923 | It also supports the pasting of code snippets that have leading '>>>' | |
|
2924 | and '...' prompts in them. This means that you can paste doctests from | |
|
2925 | files or docstrings (even if they have leading whitespace), and the | |
|
2926 | code will execute correctly. You can then use '%history -tn' to see | |
|
2927 | the translated history without line numbers; this will give you the | |
|
2928 | input after removal of all the leading prompts and whitespace, which | |
|
2929 | can be pasted back into an editor. | |
|
2930 | ||
|
2931 | With these features, you can switch into this mode easily whenever you | |
|
2932 | need to do testing and changes to doctests, without having to leave | |
|
2933 | your existing IPython session. | |
|
2934 | """ | |
|
2935 | ||
|
2936 | # XXX - Fix this to have cleaner activate/deactivate calls. | |
|
2937 | from IPython.Extensions import InterpreterPasteInput as ipaste | |
|
2938 | from IPython.ipstruct import Struct | |
|
2939 | ||
|
2940 | # Shorthands | |
|
2941 | shell = self.shell | |
|
2942 | oc = shell.outputcache | |
|
2943 | rc = shell.rc | |
|
2944 | meta = shell.meta | |
|
2945 | # dstore is a data store kept in the instance metadata bag to track any | |
|
2946 | # changes we make, so we can undo them later. | |
|
2947 | dstore = meta.setdefault('doctest_mode',Struct()) | |
|
2948 | save_dstore = dstore.setdefault | |
|
2949 | ||
|
2950 | # save a few values we'll need to recover later | |
|
2951 | mode = save_dstore('mode',False) | |
|
2952 | save_dstore('rc_pprint',rc.pprint) | |
|
2953 | save_dstore('xmode',shell.InteractiveTB.mode) | |
|
2954 | save_dstore('rc_separate_in',rc.separate_in) | |
|
2955 | save_dstore('rc_separate_out',rc.separate_out) | |
|
2956 | save_dstore('rc_separate_out2',rc.separate_out2) | |
|
2957 | save_dstore('rc_prompts_pad_left',rc.prompts_pad_left) | |
|
2958 | ||
|
2959 | if mode == False: | |
|
2960 | # turn on | |
|
2961 | ipaste.activate_prefilter() | |
|
2962 | ||
|
2963 | oc.prompt1.p_template = '>>> ' | |
|
2964 | oc.prompt2.p_template = '... ' | |
|
2965 | oc.prompt_out.p_template = '' | |
|
2966 | ||
|
2967 | oc.prompt1.sep = '' | |
|
2968 | oc.prompt_out.output_sep = '' | |
|
2969 | oc.prompt_out.output_sep2 = '\n' | |
|
2970 | ||
|
2971 | oc.prompt1.pad_left = oc.prompt2.pad_left = \ | |
|
2972 | oc.prompt_out.pad_left = False | |
|
2973 | ||
|
2974 | shell.magic_xmode('Plain') | |
|
2975 | ||
|
2976 | rc.pprint = False | |
|
2977 | ||
|
2978 | else: | |
|
2979 | # turn off | |
|
2980 | ipaste.deactivate_prefilter() | |
|
2981 | ||
|
2982 | oc.prompt1.p_template = rc.prompt_in1 | |
|
2983 | oc.prompt2.p_template = rc.prompt_in2 | |
|
2984 | oc.prompt_out.p_template = rc.prompt_out | |
|
2985 | ||
|
2986 | oc.prompt1.sep = dstore.rc_separate_in | |
|
2987 | oc.prompt_out.output_sep = dstore.rc_separate_out | |
|
2988 | oc.prompt_out.output_sep2 = dstore.rc_separate_out2 | |
|
2989 | ||
|
2990 | oc.prompt1.pad_left = oc.prompt2.pad_left = \ | |
|
2991 | oc.prompt_out.pad_left = dstore.rc_prompts_pad_left | |
|
2992 | shell.magic_xmode(dstore.xmode) | |
|
2993 | ||
|
2994 | rc.pprint = dstore.rc_pprint | |
|
2995 | ||
|
2996 | # Store new mode and inform | |
|
2997 | dstore.mode = bool(1-int(mode)) | |
|
2998 | print 'Doctest mode is:', | |
|
2999 | print ['OFF','ON'][dstore.mode] | |
|
3000 | ||
|
2915 | 3001 | # end Magic |
@@ -2,7 +2,7 b'' | |||
|
2 | 2 | """ |
|
3 | 3 | Classes for handling input/output prompts. |
|
4 | 4 | |
|
5 |
$Id: Prompts.py 2 |
|
|
5 | $Id: Prompts.py 2601 2007-08-10 07:01:29Z fperez $""" | |
|
6 | 6 | |
|
7 | 7 | #***************************************************************************** |
|
8 | 8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
@@ -215,8 +215,19 b' def str_safe(arg):' | |||
|
215 | 215 | out = '<ERROR: %s>' % msg |
|
216 | 216 | return out |
|
217 | 217 | |
|
218 | class BasePrompt: | |
|
218 | class BasePrompt(object): | |
|
219 | 219 | """Interactive prompt similar to Mathematica's.""" |
|
220 | ||
|
221 | def _get_p_template(self): | |
|
222 | return self._p_template | |
|
223 | ||
|
224 | def _set_p_template(self,val): | |
|
225 | self._p_template = val | |
|
226 | self.set_p_str() | |
|
227 | ||
|
228 | p_template = property(_get_p_template,_set_p_template, | |
|
229 | doc='Template for prompt string creation') | |
|
230 | ||
|
220 | 231 | def __init__(self,cache,sep,prompt,pad_left=False): |
|
221 | 232 | |
|
222 | 233 | # Hack: we access information about the primary prompt through the |
@@ -232,7 +243,9 b' class BasePrompt:' | |||
|
232 | 243 | # Flag to left-pad prompt strings to match the length of the primary |
|
233 | 244 | # prompt |
|
234 | 245 | self.pad_left = pad_left |
|
235 | # Set template to create each actual prompt (where numbers change) | |
|
246 | ||
|
247 | # Set template to create each actual prompt (where numbers change). | |
|
248 | # Use a property | |
|
236 | 249 | self.p_template = prompt |
|
237 | 250 | self.set_p_str() |
|
238 | 251 |
@@ -251,7 +251,8 b' class IPApi:' | |||
|
251 | 251 | |
|
252 | 252 | Inputs: |
|
253 | 253 | |
|
254 | - vars: string with variable names separated by whitespace | |
|
254 | - vars: string with variable names separated by whitespace, or a | |
|
255 | dict with name/value pairs. | |
|
255 | 256 | |
|
256 | 257 | - interactive: if True (default), the var will be listed with |
|
257 | 258 | %whos et. al. |
@@ -285,33 +286,53 b' class IPApi:' | |||
|
285 | 286 | # if this routine crashes on the next line after: |
|
286 | 287 | ip.to_user_ns('x y') |
|
287 | 288 | ... |
|
289 | ||
|
290 | # To expose *ALL* the local variables from the function, use: | |
|
291 | ip.to_user_ns(locals()) | |
|
292 | ||
|
293 | ... | |
|
288 | 294 | # return |
|
289 | 295 | |
|
290 | If you need to rename variables, just use ip.user_ns with dict | |
|
291 | and update: | |
|
292 | ||
|
293 | # exposes variables 'foo' as 'x' and 'bar' as 'y' in IPython | |
|
294 | # user namespace | |
|
295 |
ip.user_ns |
|
|
296 | ||
|
297 | If you need to rename variables, the dict input makes it easy. For | |
|
298 | example, this call exposes variables 'foo' as 'x' and 'bar' as 'y' | |
|
299 | in IPython user namespace: | |
|
300 | ||
|
301 | ip.to_user_ns(dict(x=foo,y=bar)) | |
|
296 | 302 | """ |
|
297 | 303 | |
|
298 | 304 | # print 'vars given:',vars # dbg |
|
299 | # Get the caller's frame to evaluate the given names in | |
|
300 | cf = sys._getframe(1) | |
|
301 | 305 | |
|
302 | user_ns = self.user_ns | |
|
306 | # We need a dict of name/value pairs to do namespace updates. | |
|
307 | if isinstance(vars,dict): | |
|
308 | # If a dict was given, no need to change anything. | |
|
309 | vdict = vars | |
|
310 | elif isinstance(vars,basestring): | |
|
311 | # If a string with names was given, get the caller's frame to | |
|
312 | # evaluate the given names in | |
|
313 | cf = sys._getframe(1) | |
|
314 | vdict = {} | |
|
315 | for name in vars.split(): | |
|
316 | try: | |
|
317 | vdict[name] = eval(name,cf.f_globals,cf.f_locals) | |
|
318 | except: | |
|
319 | print ('could not get var. %s from %s' % | |
|
320 | (name,cf.f_code.co_name)) | |
|
321 | else: | |
|
322 | raise ValueError('vars must be a string or a dict') | |
|
323 | ||
|
324 | # Propagate variables to user namespace | |
|
325 | self.user_ns.update(vdict) | |
|
326 | ||
|
327 | # And configure interactive visibility | |
|
303 | 328 | config_ns = self.IP.user_config_ns |
|
304 | for name in vars.split(): | |
|
305 | try: | |
|
306 | val = eval(name,cf.f_globals,cf.f_locals) | |
|
307 | user_ns[name] = val | |
|
308 | if not interactive: | |
|
309 |
|
|
|
310 | else: | |
|
311 | config_ns.pop(name,None) | |
|
312 | except: | |
|
313 | print ('could not get var. %s from %s' % | |
|
314 | (name,cf.f_code.co_name)) | |
|
329 | if interactive: | |
|
330 | for name,val in vdict.iteritems(): | |
|
331 | config_ns.pop(name,None) | |
|
332 | else: | |
|
333 | for name,val in vdict.iteritems(): | |
|
334 | config_ns[name] = val | |
|
335 | ||
|
315 | 336 | |
|
316 | 337 | def expand_alias(self,line): |
|
317 | 338 | """ Expand an alias in the command line |
@@ -1,3 +1,14 b'' | |||
|
1 | 2007-8-9 Fernando Perez <fperez@planck.colorado.edu> | |
|
2 | ||
|
3 | * IPython/ipapi.py (to_user_ns): update to accept a dict as well as | |
|
4 | a string with names. | |
|
5 | ||
|
6 | 2007-08-09 Fernando Perez <Fernando.Perez@colorado.edu> | |
|
7 | ||
|
8 | * IPython/Magic.py (magic_doctest_mode): added new %doctest_mode | |
|
9 | magic to toggle on/off the doctest pasting support without having | |
|
10 | to leave a session to switch to a separate profile. | |
|
11 | ||
|
1 | 12 | 2007-08-08 Fernando Perez <Fernando.Perez@colorado.edu> |
|
2 | 13 | |
|
3 | 14 | * IPython/Extensions/ipy_profile_doctest.py (main): fix prompt to |
General Comments 0
You need to be logged in to leave comments.
Login now