Show More
@@ -1,2741 +1,2741 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Magic functions for InteractiveShell. |
|
2 | """Magic functions for InteractiveShell. | |
3 |
|
3 | |||
4 |
$Id: Magic.py 100 |
|
4 | $Id: Magic.py 1003 2006-01-11 22:18:56Z vivainio $""" | |
5 |
|
5 | |||
6 | #***************************************************************************** |
|
6 | #***************************************************************************** | |
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #***************************************************************************** |
|
12 | #***************************************************************************** | |
13 |
|
13 | |||
14 | #**************************************************************************** |
|
14 | #**************************************************************************** | |
15 | # Modules and globals |
|
15 | # Modules and globals | |
16 |
|
16 | |||
17 | from IPython import Release |
|
17 | from IPython import Release | |
18 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
18 | __author__ = '%s <%s>\n%s <%s>' % \ | |
19 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
19 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) | |
20 | __license__ = Release.license |
|
20 | __license__ = Release.license | |
21 |
|
21 | |||
22 | # Python standard modules |
|
22 | # Python standard modules | |
23 | import __builtin__ |
|
23 | import __builtin__ | |
24 | import bdb |
|
24 | import bdb | |
25 | import inspect |
|
25 | import inspect | |
26 | import os |
|
26 | import os | |
27 | import pdb |
|
27 | import pdb | |
28 | import pydoc |
|
28 | import pydoc | |
29 | import sys |
|
29 | import sys | |
30 | import re |
|
30 | import re | |
31 | import tempfile |
|
31 | import tempfile | |
32 | import time |
|
32 | import time | |
33 | import cPickle as pickle |
|
33 | import cPickle as pickle | |
34 | from cStringIO import StringIO |
|
34 | from cStringIO import StringIO | |
35 | from getopt import getopt |
|
35 | from getopt import getopt | |
36 | from pprint import pprint, pformat |
|
36 | from pprint import pprint, pformat | |
37 |
|
37 | |||
38 | # profile isn't bundled by default in Debian for license reasons |
|
38 | # profile isn't bundled by default in Debian for license reasons | |
39 | try: |
|
39 | try: | |
40 | import profile,pstats |
|
40 | import profile,pstats | |
41 | except ImportError: |
|
41 | except ImportError: | |
42 | profile = pstats = None |
|
42 | profile = pstats = None | |
43 |
|
43 | |||
44 | # Homebrewed |
|
44 | # Homebrewed | |
45 | from IPython import Debugger, OInspect, wildcard |
|
45 | from IPython import Debugger, OInspect, wildcard | |
46 | from IPython.FakeModule import FakeModule |
|
46 | from IPython.FakeModule import FakeModule | |
47 | from IPython.Itpl import Itpl, itpl, printpl,itplns |
|
47 | from IPython.Itpl import Itpl, itpl, printpl,itplns | |
48 | from IPython.PyColorize import Parser |
|
48 | from IPython.PyColorize import Parser | |
49 | from IPython.Struct import Struct |
|
49 | from IPython.Struct import Struct | |
50 | from IPython.macro import Macro |
|
50 | from IPython.macro import Macro | |
51 | from IPython.genutils import * |
|
51 | from IPython.genutils import * | |
52 |
|
52 | |||
53 | #*************************************************************************** |
|
53 | #*************************************************************************** | |
54 | # Utility functions |
|
54 | # Utility functions | |
55 | def on_off(tag): |
|
55 | def on_off(tag): | |
56 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
56 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" | |
57 | return ['OFF','ON'][tag] |
|
57 | return ['OFF','ON'][tag] | |
58 |
|
58 | |||
59 | class Bunch: pass |
|
59 | class Bunch: pass | |
60 |
|
60 | |||
61 | #*************************************************************************** |
|
61 | #*************************************************************************** | |
62 | # Main class implementing Magic functionality |
|
62 | # Main class implementing Magic functionality | |
63 | class Magic: |
|
63 | class Magic: | |
64 | """Magic functions for InteractiveShell. |
|
64 | """Magic functions for InteractiveShell. | |
65 |
|
65 | |||
66 | Shell functions which can be reached as %function_name. All magic |
|
66 | Shell functions which can be reached as %function_name. All magic | |
67 | functions should accept a string, which they can parse for their own |
|
67 | functions should accept a string, which they can parse for their own | |
68 | needs. This can make some functions easier to type, eg `%cd ../` |
|
68 | needs. This can make some functions easier to type, eg `%cd ../` | |
69 | vs. `%cd("../")` |
|
69 | vs. `%cd("../")` | |
70 |
|
70 | |||
71 | ALL definitions MUST begin with the prefix magic_. The user won't need it |
|
71 | ALL definitions MUST begin with the prefix magic_. The user won't need it | |
72 | at the command line, but it is is needed in the definition. """ |
|
72 | at the command line, but it is is needed in the definition. """ | |
73 |
|
73 | |||
74 | # class globals |
|
74 | # class globals | |
75 | auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.', |
|
75 | auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.', | |
76 | 'Automagic is ON, % prefix NOT needed for magic functions.'] |
|
76 | 'Automagic is ON, % prefix NOT needed for magic functions.'] | |
77 |
|
77 | |||
78 | #...................................................................... |
|
78 | #...................................................................... | |
79 | # some utility functions |
|
79 | # some utility functions | |
80 |
|
80 | |||
81 | def __init__(self,shell): |
|
81 | def __init__(self,shell): | |
82 |
|
82 | |||
83 | self.options_table = {} |
|
83 | self.options_table = {} | |
84 | if profile is None: |
|
84 | if profile is None: | |
85 | self.magic_prun = self.profile_missing_notice |
|
85 | self.magic_prun = self.profile_missing_notice | |
86 | self.shell = shell |
|
86 | self.shell = shell | |
87 |
|
87 | |||
88 | # namespace for holding state we may need |
|
88 | # namespace for holding state we may need | |
89 | self._magic_state = Bunch() |
|
89 | self._magic_state = Bunch() | |
90 |
|
90 | |||
91 | def profile_missing_notice(self, *args, **kwargs): |
|
91 | def profile_missing_notice(self, *args, **kwargs): | |
92 | error("""\ |
|
92 | error("""\ | |
93 | The profile module could not be found. If you are a Debian user, |
|
93 | The profile module could not be found. If you are a Debian user, | |
94 | it has been removed from the standard Debian package because of its non-free |
|
94 | it has been removed from the standard Debian package because of its non-free | |
95 | license. To use profiling, please install"python2.3-profiler" from non-free.""") |
|
95 | license. To use profiling, please install"python2.3-profiler" from non-free.""") | |
96 |
|
96 | |||
97 | def default_option(self,fn,optstr): |
|
97 | def default_option(self,fn,optstr): | |
98 | """Make an entry in the options_table for fn, with value optstr""" |
|
98 | """Make an entry in the options_table for fn, with value optstr""" | |
99 |
|
99 | |||
100 | if fn not in self.lsmagic(): |
|
100 | if fn not in self.lsmagic(): | |
101 | error("%s is not a magic function" % fn) |
|
101 | error("%s is not a magic function" % fn) | |
102 | self.options_table[fn] = optstr |
|
102 | self.options_table[fn] = optstr | |
103 |
|
103 | |||
104 | def lsmagic(self): |
|
104 | def lsmagic(self): | |
105 | """Return a list of currently available magic functions. |
|
105 | """Return a list of currently available magic functions. | |
106 |
|
106 | |||
107 | Gives a list of the bare names after mangling (['ls','cd', ...], not |
|
107 | Gives a list of the bare names after mangling (['ls','cd', ...], not | |
108 | ['magic_ls','magic_cd',...]""" |
|
108 | ['magic_ls','magic_cd',...]""" | |
109 |
|
109 | |||
110 | # FIXME. This needs a cleanup, in the way the magics list is built. |
|
110 | # FIXME. This needs a cleanup, in the way the magics list is built. | |
111 |
|
111 | |||
112 | # magics in class definition |
|
112 | # magics in class definition | |
113 | class_magic = lambda fn: fn.startswith('magic_') and \ |
|
113 | class_magic = lambda fn: fn.startswith('magic_') and \ | |
114 | callable(Magic.__dict__[fn]) |
|
114 | callable(Magic.__dict__[fn]) | |
115 | # in instance namespace (run-time user additions) |
|
115 | # in instance namespace (run-time user additions) | |
116 | inst_magic = lambda fn: fn.startswith('magic_') and \ |
|
116 | inst_magic = lambda fn: fn.startswith('magic_') and \ | |
117 | callable(self.__dict__[fn]) |
|
117 | callable(self.__dict__[fn]) | |
118 | # and bound magics by user (so they can access self): |
|
118 | # and bound magics by user (so they can access self): | |
119 | inst_bound_magic = lambda fn: fn.startswith('magic_') and \ |
|
119 | inst_bound_magic = lambda fn: fn.startswith('magic_') and \ | |
120 | callable(self.__class__.__dict__[fn]) |
|
120 | callable(self.__class__.__dict__[fn]) | |
121 | magics = filter(class_magic,Magic.__dict__.keys()) + \ |
|
121 | magics = filter(class_magic,Magic.__dict__.keys()) + \ | |
122 | filter(inst_magic,self.__dict__.keys()) + \ |
|
122 | filter(inst_magic,self.__dict__.keys()) + \ | |
123 | filter(inst_bound_magic,self.__class__.__dict__.keys()) |
|
123 | filter(inst_bound_magic,self.__class__.__dict__.keys()) | |
124 | out = [] |
|
124 | out = [] | |
125 | for fn in magics: |
|
125 | for fn in magics: | |
126 | out.append(fn.replace('magic_','',1)) |
|
126 | out.append(fn.replace('magic_','',1)) | |
127 | out.sort() |
|
127 | out.sort() | |
128 | return out |
|
128 | return out | |
129 |
|
129 | |||
130 | def extract_input_slices(self,slices): |
|
130 | def extract_input_slices(self,slices): | |
131 | """Return as a string a set of input history slices. |
|
131 | """Return as a string a set of input history slices. | |
132 |
|
132 | |||
133 | The set of slices is given as a list of strings (like ['1','4:8','9'], |
|
133 | The set of slices is given as a list of strings (like ['1','4:8','9'], | |
134 | since this function is for use by magic functions which get their |
|
134 | since this function is for use by magic functions which get their | |
135 | arguments as strings. |
|
135 | arguments as strings. | |
136 |
|
136 | |||
137 | Note that slices can be called with two notations: |
|
137 | Note that slices can be called with two notations: | |
138 |
|
138 | |||
139 | N:M -> standard python form, means including items N...(M-1). |
|
139 | N:M -> standard python form, means including items N...(M-1). | |
140 |
|
140 | |||
141 | N-M -> include items N..M (closed endpoint).""" |
|
141 | N-M -> include items N..M (closed endpoint).""" | |
142 |
|
142 | |||
143 | cmds = [] |
|
143 | cmds = [] | |
144 | for chunk in slices: |
|
144 | for chunk in slices: | |
145 | if ':' in chunk: |
|
145 | if ':' in chunk: | |
146 | ini,fin = map(int,chunk.split(':')) |
|
146 | ini,fin = map(int,chunk.split(':')) | |
147 | elif '-' in chunk: |
|
147 | elif '-' in chunk: | |
148 | ini,fin = map(int,chunk.split('-')) |
|
148 | ini,fin = map(int,chunk.split('-')) | |
149 | fin += 1 |
|
149 | fin += 1 | |
150 | else: |
|
150 | else: | |
151 | ini = int(chunk) |
|
151 | ini = int(chunk) | |
152 | fin = ini+1 |
|
152 | fin = ini+1 | |
153 | cmds.append(self.shell.input_hist[ini:fin]) |
|
153 | cmds.append(self.shell.input_hist[ini:fin]) | |
154 | return cmds |
|
154 | return cmds | |
155 |
|
155 | |||
156 | def _ofind(self,oname): |
|
156 | def _ofind(self,oname): | |
157 | """Find an object in the available namespaces. |
|
157 | """Find an object in the available namespaces. | |
158 |
|
158 | |||
159 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
159 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
160 |
|
160 | |||
161 | Has special code to detect magic functions. |
|
161 | Has special code to detect magic functions. | |
162 | """ |
|
162 | """ | |
163 |
|
163 | |||
164 | oname = oname.strip() |
|
164 | oname = oname.strip() | |
165 |
|
165 | |||
166 | # Namespaces to search in: |
|
166 | # Namespaces to search in: | |
167 | user_ns = self.shell.user_ns |
|
167 | user_ns = self.shell.user_ns | |
168 | internal_ns = self.shell.internal_ns |
|
168 | internal_ns = self.shell.internal_ns | |
169 | builtin_ns = __builtin__.__dict__ |
|
169 | builtin_ns = __builtin__.__dict__ | |
170 | alias_ns = self.shell.alias_table |
|
170 | alias_ns = self.shell.alias_table | |
171 |
|
171 | |||
172 | # Put them in a list. The order is important so that we find things in |
|
172 | # Put them in a list. The order is important so that we find things in | |
173 | # the same order that Python finds them. |
|
173 | # the same order that Python finds them. | |
174 | namespaces = [ ('Interactive',user_ns), |
|
174 | namespaces = [ ('Interactive',user_ns), | |
175 | ('IPython internal',internal_ns), |
|
175 | ('IPython internal',internal_ns), | |
176 | ('Python builtin',builtin_ns), |
|
176 | ('Python builtin',builtin_ns), | |
177 | ('Alias',alias_ns), |
|
177 | ('Alias',alias_ns), | |
178 | ] |
|
178 | ] | |
179 |
|
179 | |||
180 | # initialize results to 'null' |
|
180 | # initialize results to 'null' | |
181 | found = 0; obj = None; ospace = None; ds = None; |
|
181 | found = 0; obj = None; ospace = None; ds = None; | |
182 | ismagic = 0; isalias = 0 |
|
182 | ismagic = 0; isalias = 0 | |
183 |
|
183 | |||
184 | # Look for the given name by splitting it in parts. If the head is |
|
184 | # Look for the given name by splitting it in parts. If the head is | |
185 | # found, then we look for all the remaining parts as members, and only |
|
185 | # found, then we look for all the remaining parts as members, and only | |
186 | # declare success if we can find them all. |
|
186 | # declare success if we can find them all. | |
187 | oname_parts = oname.split('.') |
|
187 | oname_parts = oname.split('.') | |
188 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
188 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
189 | for nsname,ns in namespaces: |
|
189 | for nsname,ns in namespaces: | |
190 | try: |
|
190 | try: | |
191 | obj = ns[oname_head] |
|
191 | obj = ns[oname_head] | |
192 | except KeyError: |
|
192 | except KeyError: | |
193 | continue |
|
193 | continue | |
194 | else: |
|
194 | else: | |
195 | for part in oname_rest: |
|
195 | for part in oname_rest: | |
196 | try: |
|
196 | try: | |
197 | obj = getattr(obj,part) |
|
197 | obj = getattr(obj,part) | |
198 | except: |
|
198 | except: | |
199 | # Blanket except b/c some badly implemented objects |
|
199 | # Blanket except b/c some badly implemented objects | |
200 | # allow __getattr__ to raise exceptions other than |
|
200 | # allow __getattr__ to raise exceptions other than | |
201 | # AttributeError, which then crashes IPython. |
|
201 | # AttributeError, which then crashes IPython. | |
202 | break |
|
202 | break | |
203 | else: |
|
203 | else: | |
204 | # If we finish the for loop (no break), we got all members |
|
204 | # If we finish the for loop (no break), we got all members | |
205 | found = 1 |
|
205 | found = 1 | |
206 | ospace = nsname |
|
206 | ospace = nsname | |
207 | if ns == alias_ns: |
|
207 | if ns == alias_ns: | |
208 | isalias = 1 |
|
208 | isalias = 1 | |
209 | break # namespace loop |
|
209 | break # namespace loop | |
210 |
|
210 | |||
211 | # Try to see if it's magic |
|
211 | # Try to see if it's magic | |
212 | if not found: |
|
212 | if not found: | |
213 | if oname.startswith(self.shell.ESC_MAGIC): |
|
213 | if oname.startswith(self.shell.ESC_MAGIC): | |
214 | oname = oname[1:] |
|
214 | oname = oname[1:] | |
215 | obj = getattr(self,'magic_'+oname,None) |
|
215 | obj = getattr(self,'magic_'+oname,None) | |
216 | if obj is not None: |
|
216 | if obj is not None: | |
217 | found = 1 |
|
217 | found = 1 | |
218 | ospace = 'IPython internal' |
|
218 | ospace = 'IPython internal' | |
219 | ismagic = 1 |
|
219 | ismagic = 1 | |
220 |
|
220 | |||
221 | # Last try: special-case some literals like '', [], {}, etc: |
|
221 | # Last try: special-case some literals like '', [], {}, etc: | |
222 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
222 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
223 | obj = eval(oname_head) |
|
223 | obj = eval(oname_head) | |
224 | found = 1 |
|
224 | found = 1 | |
225 | ospace = 'Interactive' |
|
225 | ospace = 'Interactive' | |
226 |
|
226 | |||
227 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
227 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
228 | 'ismagic':ismagic, 'isalias':isalias} |
|
228 | 'ismagic':ismagic, 'isalias':isalias} | |
229 |
|
229 | |||
230 | def arg_err(self,func): |
|
230 | def arg_err(self,func): | |
231 | """Print docstring if incorrect arguments were passed""" |
|
231 | """Print docstring if incorrect arguments were passed""" | |
232 | print 'Error in arguments:' |
|
232 | print 'Error in arguments:' | |
233 | print OInspect.getdoc(func) |
|
233 | print OInspect.getdoc(func) | |
234 |
|
234 | |||
235 | def format_latex(self,strng): |
|
235 | def format_latex(self,strng): | |
236 | """Format a string for latex inclusion.""" |
|
236 | """Format a string for latex inclusion.""" | |
237 |
|
237 | |||
238 | # Characters that need to be escaped for latex: |
|
238 | # Characters that need to be escaped for latex: | |
239 | escape_re = re.compile(r'(%|_|\$|#)',re.MULTILINE) |
|
239 | escape_re = re.compile(r'(%|_|\$|#)',re.MULTILINE) | |
240 | # Magic command names as headers: |
|
240 | # Magic command names as headers: | |
241 | cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC, |
|
241 | cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC, | |
242 | re.MULTILINE) |
|
242 | re.MULTILINE) | |
243 | # Magic commands |
|
243 | # Magic commands | |
244 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC, |
|
244 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC, | |
245 | re.MULTILINE) |
|
245 | re.MULTILINE) | |
246 | # Paragraph continue |
|
246 | # Paragraph continue | |
247 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
247 | par_re = re.compile(r'\\$',re.MULTILINE) | |
248 |
|
248 | |||
249 | # The "\n" symbol |
|
249 | # The "\n" symbol | |
250 | newline_re = re.compile(r'\\n') |
|
250 | newline_re = re.compile(r'\\n') | |
251 |
|
251 | |||
252 | # Now build the string for output: |
|
252 | # Now build the string for output: | |
253 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) |
|
253 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) | |
254 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', |
|
254 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', | |
255 | strng) |
|
255 | strng) | |
256 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) |
|
256 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) | |
257 | strng = par_re.sub(r'\\\\',strng) |
|
257 | strng = par_re.sub(r'\\\\',strng) | |
258 | strng = escape_re.sub(r'\\\1',strng) |
|
258 | strng = escape_re.sub(r'\\\1',strng) | |
259 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
259 | strng = newline_re.sub(r'\\textbackslash{}n',strng) | |
260 | return strng |
|
260 | return strng | |
261 |
|
261 | |||
262 | def format_screen(self,strng): |
|
262 | def format_screen(self,strng): | |
263 | """Format a string for screen printing. |
|
263 | """Format a string for screen printing. | |
264 |
|
264 | |||
265 | This removes some latex-type format codes.""" |
|
265 | This removes some latex-type format codes.""" | |
266 | # Paragraph continue |
|
266 | # Paragraph continue | |
267 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
267 | par_re = re.compile(r'\\$',re.MULTILINE) | |
268 | strng = par_re.sub('',strng) |
|
268 | strng = par_re.sub('',strng) | |
269 | return strng |
|
269 | return strng | |
270 |
|
270 | |||
271 | def parse_options(self,arg_str,opt_str,*long_opts,**kw): |
|
271 | def parse_options(self,arg_str,opt_str,*long_opts,**kw): | |
272 | """Parse options passed to an argument string. |
|
272 | """Parse options passed to an argument string. | |
273 |
|
273 | |||
274 | The interface is similar to that of getopt(), but it returns back a |
|
274 | The interface is similar to that of getopt(), but it returns back a | |
275 | Struct with the options as keys and the stripped argument string still |
|
275 | Struct with the options as keys and the stripped argument string still | |
276 | as a string. |
|
276 | as a string. | |
277 |
|
277 | |||
278 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
278 | arg_str is quoted as a true sys.argv vector by using shlex.split. | |
279 | This allows us to easily expand variables, glob files, quote |
|
279 | This allows us to easily expand variables, glob files, quote | |
280 | arguments, etc. |
|
280 | arguments, etc. | |
281 |
|
281 | |||
282 | Options: |
|
282 | Options: | |
283 | -mode: default 'string'. If given as 'list', the argument string is |
|
283 | -mode: default 'string'. If given as 'list', the argument string is | |
284 | returned as a list (split on whitespace) instead of a string. |
|
284 | returned as a list (split on whitespace) instead of a string. | |
285 |
|
285 | |||
286 | -list_all: put all option values in lists. Normally only options |
|
286 | -list_all: put all option values in lists. Normally only options | |
287 | appearing more than once are put in a list.""" |
|
287 | appearing more than once are put in a list.""" | |
288 |
|
288 | |||
289 | # inject default options at the beginning of the input line |
|
289 | # inject default options at the beginning of the input line | |
290 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') |
|
290 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') | |
291 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
291 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) | |
292 |
|
292 | |||
293 | mode = kw.get('mode','string') |
|
293 | mode = kw.get('mode','string') | |
294 | if mode not in ['string','list']: |
|
294 | if mode not in ['string','list']: | |
295 | raise ValueError,'incorrect mode given: %s' % mode |
|
295 | raise ValueError,'incorrect mode given: %s' % mode | |
296 | # Get options |
|
296 | # Get options | |
297 | list_all = kw.get('list_all',0) |
|
297 | list_all = kw.get('list_all',0) | |
298 |
|
298 | |||
299 | # Check if we have more than one argument to warrant extra processing: |
|
299 | # Check if we have more than one argument to warrant extra processing: | |
300 | odict = {} # Dictionary with options |
|
300 | odict = {} # Dictionary with options | |
301 | args = arg_str.split() |
|
301 | args = arg_str.split() | |
302 | if len(args) >= 1: |
|
302 | if len(args) >= 1: | |
303 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
303 | # If the list of inputs only has 0 or 1 thing in it, there's no | |
304 | # need to look for options |
|
304 | # need to look for options | |
305 | argv = shlex_split(arg_str) |
|
305 | argv = shlex_split(arg_str) | |
306 | # Do regular option processing |
|
306 | # Do regular option processing | |
307 | opts,args = getopt(argv,opt_str,*long_opts) |
|
307 | opts,args = getopt(argv,opt_str,*long_opts) | |
308 | for o,a in opts: |
|
308 | for o,a in opts: | |
309 | if o.startswith('--'): |
|
309 | if o.startswith('--'): | |
310 | o = o[2:] |
|
310 | o = o[2:] | |
311 | else: |
|
311 | else: | |
312 | o = o[1:] |
|
312 | o = o[1:] | |
313 | try: |
|
313 | try: | |
314 | odict[o].append(a) |
|
314 | odict[o].append(a) | |
315 | except AttributeError: |
|
315 | except AttributeError: | |
316 | odict[o] = [odict[o],a] |
|
316 | odict[o] = [odict[o],a] | |
317 | except KeyError: |
|
317 | except KeyError: | |
318 | if list_all: |
|
318 | if list_all: | |
319 | odict[o] = [a] |
|
319 | odict[o] = [a] | |
320 | else: |
|
320 | else: | |
321 | odict[o] = a |
|
321 | odict[o] = a | |
322 |
|
322 | |||
323 | # Prepare opts,args for return |
|
323 | # Prepare opts,args for return | |
324 | opts = Struct(odict) |
|
324 | opts = Struct(odict) | |
325 | if mode == 'string': |
|
325 | if mode == 'string': | |
326 | args = ' '.join(args) |
|
326 | args = ' '.join(args) | |
327 |
|
327 | |||
328 | return opts,args |
|
328 | return opts,args | |
329 |
|
329 | |||
330 | #...................................................................... |
|
330 | #...................................................................... | |
331 | # And now the actual magic functions |
|
331 | # And now the actual magic functions | |
332 |
|
332 | |||
333 | # Functions for IPython shell work (vars,funcs, config, etc) |
|
333 | # Functions for IPython shell work (vars,funcs, config, etc) | |
334 | def magic_lsmagic(self, parameter_s = ''): |
|
334 | def magic_lsmagic(self, parameter_s = ''): | |
335 | """List currently available magic functions.""" |
|
335 | """List currently available magic functions.""" | |
336 | mesc = self.shell.ESC_MAGIC |
|
336 | mesc = self.shell.ESC_MAGIC | |
337 | print 'Available magic functions:\n'+mesc+\ |
|
337 | print 'Available magic functions:\n'+mesc+\ | |
338 | (' '+mesc).join(self.lsmagic()) |
|
338 | (' '+mesc).join(self.lsmagic()) | |
339 | print '\n' + Magic.auto_status[self.shell.rc.automagic] |
|
339 | print '\n' + Magic.auto_status[self.shell.rc.automagic] | |
340 | return None |
|
340 | return None | |
341 |
|
341 | |||
342 | def magic_magic(self, parameter_s = ''): |
|
342 | def magic_magic(self, parameter_s = ''): | |
343 | """Print information about the magic function system.""" |
|
343 | """Print information about the magic function system.""" | |
344 |
|
344 | |||
345 | mode = '' |
|
345 | mode = '' | |
346 | try: |
|
346 | try: | |
347 | if parameter_s.split()[0] == '-latex': |
|
347 | if parameter_s.split()[0] == '-latex': | |
348 | mode = 'latex' |
|
348 | mode = 'latex' | |
349 | except: |
|
349 | except: | |
350 | pass |
|
350 | pass | |
351 |
|
351 | |||
352 | magic_docs = [] |
|
352 | magic_docs = [] | |
353 | for fname in self.lsmagic(): |
|
353 | for fname in self.lsmagic(): | |
354 | mname = 'magic_' + fname |
|
354 | mname = 'magic_' + fname | |
355 | for space in (Magic,self,self.__class__): |
|
355 | for space in (Magic,self,self.__class__): | |
356 | try: |
|
356 | try: | |
357 | fn = space.__dict__[mname] |
|
357 | fn = space.__dict__[mname] | |
358 | except KeyError: |
|
358 | except KeyError: | |
359 | pass |
|
359 | pass | |
360 | else: |
|
360 | else: | |
361 | break |
|
361 | break | |
362 | magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC, |
|
362 | magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC, | |
363 | fname,fn.__doc__)) |
|
363 | fname,fn.__doc__)) | |
364 | magic_docs = ''.join(magic_docs) |
|
364 | magic_docs = ''.join(magic_docs) | |
365 |
|
365 | |||
366 | if mode == 'latex': |
|
366 | if mode == 'latex': | |
367 | print self.format_latex(magic_docs) |
|
367 | print self.format_latex(magic_docs) | |
368 | return |
|
368 | return | |
369 | else: |
|
369 | else: | |
370 | magic_docs = self.format_screen(magic_docs) |
|
370 | magic_docs = self.format_screen(magic_docs) | |
371 |
|
371 | |||
372 | outmsg = """ |
|
372 | outmsg = """ | |
373 | IPython's 'magic' functions |
|
373 | IPython's 'magic' functions | |
374 | =========================== |
|
374 | =========================== | |
375 |
|
375 | |||
376 | The magic function system provides a series of functions which allow you to |
|
376 | The magic function system provides a series of functions which allow you to | |
377 | control the behavior of IPython itself, plus a lot of system-type |
|
377 | control the behavior of IPython itself, plus a lot of system-type | |
378 | features. All these functions are prefixed with a % character, but parameters |
|
378 | features. All these functions are prefixed with a % character, but parameters | |
379 | are given without parentheses or quotes. |
|
379 | are given without parentheses or quotes. | |
380 |
|
380 | |||
381 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
381 | NOTE: If you have 'automagic' enabled (via the command line option or with the | |
382 | %automagic function), you don't need to type in the % explicitly. By default, |
|
382 | %automagic function), you don't need to type in the % explicitly. By default, | |
383 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
383 | IPython ships with automagic on, so you should only rarely need the % escape. | |
384 |
|
384 | |||
385 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
|
385 | Example: typing '%cd mydir' (without the quotes) changes you working directory | |
386 | to 'mydir', if it exists. |
|
386 | to 'mydir', if it exists. | |
387 |
|
387 | |||
388 | You can define your own magic functions to extend the system. See the supplied |
|
388 | You can define your own magic functions to extend the system. See the supplied | |
389 | ipythonrc and example-magic.py files for details (in your ipython |
|
389 | ipythonrc and example-magic.py files for details (in your ipython | |
390 | configuration directory, typically $HOME/.ipython/). |
|
390 | configuration directory, typically $HOME/.ipython/). | |
391 |
|
391 | |||
392 | You can also define your own aliased names for magic functions. In your |
|
392 | You can also define your own aliased names for magic functions. In your | |
393 | ipythonrc file, placing a line like: |
|
393 | ipythonrc file, placing a line like: | |
394 |
|
394 | |||
395 | execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile |
|
395 | execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile | |
396 |
|
396 | |||
397 | will define %pf as a new name for %profile. |
|
397 | will define %pf as a new name for %profile. | |
398 |
|
398 | |||
399 | You can also call magics in code using the ipmagic() function, which IPython |
|
399 | You can also call magics in code using the ipmagic() function, which IPython | |
400 | automatically adds to the builtin namespace. Type 'ipmagic?' for details. |
|
400 | automatically adds to the builtin namespace. Type 'ipmagic?' for details. | |
401 |
|
401 | |||
402 | For a list of the available magic functions, use %lsmagic. For a description |
|
402 | For a list of the available magic functions, use %lsmagic. For a description | |
403 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
403 | of any of them, type %magic_name?, e.g. '%cd?'. | |
404 |
|
404 | |||
405 | Currently the magic system has the following functions:\n""" |
|
405 | Currently the magic system has the following functions:\n""" | |
406 |
|
406 | |||
407 | mesc = self.shell.ESC_MAGIC |
|
407 | mesc = self.shell.ESC_MAGIC | |
408 | outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):" |
|
408 | outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):" | |
409 | "\n\n%s%s\n\n%s" % (outmsg, |
|
409 | "\n\n%s%s\n\n%s" % (outmsg, | |
410 | magic_docs,mesc,mesc, |
|
410 | magic_docs,mesc,mesc, | |
411 | (' '+mesc).join(self.lsmagic()), |
|
411 | (' '+mesc).join(self.lsmagic()), | |
412 | Magic.auto_status[self.shell.rc.automagic] ) ) |
|
412 | Magic.auto_status[self.shell.rc.automagic] ) ) | |
413 |
|
413 | |||
414 | page(outmsg,screen_lines=self.shell.rc.screen_length) |
|
414 | page(outmsg,screen_lines=self.shell.rc.screen_length) | |
415 |
|
415 | |||
416 | def magic_automagic(self, parameter_s = ''): |
|
416 | def magic_automagic(self, parameter_s = ''): | |
417 | """Make magic functions callable without having to type the initial %. |
|
417 | """Make magic functions callable without having to type the initial %. | |
418 |
|
418 | |||
419 | Toggles on/off (when off, you must call it as %automagic, of |
|
419 | Toggles on/off (when off, you must call it as %automagic, of | |
420 | course). Note that magic functions have lowest priority, so if there's |
|
420 | course). Note that magic functions have lowest priority, so if there's | |
421 | a variable whose name collides with that of a magic fn, automagic |
|
421 | a variable whose name collides with that of a magic fn, automagic | |
422 | won't work for that function (you get the variable instead). However, |
|
422 | won't work for that function (you get the variable instead). However, | |
423 | if you delete the variable (del var), the previously shadowed magic |
|
423 | if you delete the variable (del var), the previously shadowed magic | |
424 | function becomes visible to automagic again.""" |
|
424 | function becomes visible to automagic again.""" | |
425 |
|
425 | |||
426 | rc = self.shell.rc |
|
426 | rc = self.shell.rc | |
427 | rc.automagic = not rc.automagic |
|
427 | rc.automagic = not rc.automagic | |
428 | print '\n' + Magic.auto_status[rc.automagic] |
|
428 | print '\n' + Magic.auto_status[rc.automagic] | |
429 |
|
429 | |||
430 | def magic_autocall(self, parameter_s = ''): |
|
430 | def magic_autocall(self, parameter_s = ''): | |
431 | """Make functions callable without having to type parentheses. |
|
431 | """Make functions callable without having to type parentheses. | |
432 |
|
432 | |||
433 | Usage: |
|
433 | Usage: | |
434 |
|
434 | |||
435 | %autocall [mode] |
|
435 | %autocall [mode] | |
436 |
|
436 | |||
437 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the |
|
437 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the | |
438 | value is toggled on and off (remembering the previous state).""" |
|
438 | value is toggled on and off (remembering the previous state).""" | |
439 |
|
439 | |||
440 | rc = self.shell.rc |
|
440 | rc = self.shell.rc | |
441 |
|
441 | |||
442 | if parameter_s: |
|
442 | if parameter_s: | |
443 | arg = int(parameter_s) |
|
443 | arg = int(parameter_s) | |
444 | else: |
|
444 | else: | |
445 | arg = 'toggle' |
|
445 | arg = 'toggle' | |
446 |
|
446 | |||
447 | if not arg in (0,1,2,'toggle'): |
|
447 | if not arg in (0,1,2,'toggle'): | |
448 | error('Valid modes: (0->Off, 1->Smart, 2->Full') |
|
448 | error('Valid modes: (0->Off, 1->Smart, 2->Full') | |
449 | return |
|
449 | return | |
450 |
|
450 | |||
451 | if arg in (0,1,2): |
|
451 | if arg in (0,1,2): | |
452 | rc.autocall = arg |
|
452 | rc.autocall = arg | |
453 | else: # toggle |
|
453 | else: # toggle | |
454 | if rc.autocall: |
|
454 | if rc.autocall: | |
455 | self._magic_state.autocall_save = rc.autocall |
|
455 | self._magic_state.autocall_save = rc.autocall | |
456 | rc.autocall = 0 |
|
456 | rc.autocall = 0 | |
457 | else: |
|
457 | else: | |
458 | try: |
|
458 | try: | |
459 | rc.autocall = self._magic_state.autocall_save |
|
459 | rc.autocall = self._magic_state.autocall_save | |
460 | except AttributeError: |
|
460 | except AttributeError: | |
461 | rc.autocall = self._magic_state.autocall_save = 1 |
|
461 | rc.autocall = self._magic_state.autocall_save = 1 | |
462 |
|
462 | |||
463 | print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall] |
|
463 | print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall] | |
464 |
|
464 | |||
465 | def magic_autoindent(self, parameter_s = ''): |
|
465 | def magic_autoindent(self, parameter_s = ''): | |
466 | """Toggle autoindent on/off (if available).""" |
|
466 | """Toggle autoindent on/off (if available).""" | |
467 |
|
467 | |||
468 | self.shell.set_autoindent() |
|
468 | self.shell.set_autoindent() | |
469 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] |
|
469 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] | |
470 |
|
470 | |||
471 | def magic_system_verbose(self, parameter_s = ''): |
|
471 | def magic_system_verbose(self, parameter_s = ''): | |
472 | """Toggle verbose printing of system calls on/off.""" |
|
472 | """Toggle verbose printing of system calls on/off.""" | |
473 |
|
473 | |||
474 | self.shell.rc_set_toggle('system_verbose') |
|
474 | self.shell.rc_set_toggle('system_verbose') | |
475 | print "System verbose printing is:",\ |
|
475 | print "System verbose printing is:",\ | |
476 | ['OFF','ON'][self.shell.rc.system_verbose] |
|
476 | ['OFF','ON'][self.shell.rc.system_verbose] | |
477 |
|
477 | |||
478 | def magic_history(self, parameter_s = ''): |
|
478 | def magic_history(self, parameter_s = ''): | |
479 | """Print input history (_i<n> variables), with most recent last. |
|
479 | """Print input history (_i<n> variables), with most recent last. | |
480 |
|
480 | |||
481 | %history [-n] -> print at most 40 inputs (some may be multi-line)\\ |
|
481 | %history [-n] -> print at most 40 inputs (some may be multi-line)\\ | |
482 | %history [-n] n -> print at most n inputs\\ |
|
482 | %history [-n] n -> print at most n inputs\\ | |
483 | %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ |
|
483 | %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ | |
484 |
|
484 | |||
485 | Each input's number <n> is shown, and is accessible as the |
|
485 | Each input's number <n> is shown, and is accessible as the | |
486 | automatically generated variable _i<n>. Multi-line statements are |
|
486 | automatically generated variable _i<n>. Multi-line statements are | |
487 | printed starting at a new line for easy copy/paste. |
|
487 | printed starting at a new line for easy copy/paste. | |
488 |
|
488 | |||
489 | If option -n is used, input numbers are not printed. This is useful if |
|
489 | If option -n is used, input numbers are not printed. This is useful if | |
490 | you want to get a printout of many lines which can be directly pasted |
|
490 | you want to get a printout of many lines which can be directly pasted | |
491 | into a text editor. |
|
491 | into a text editor. | |
492 |
|
492 | |||
493 | This feature is only available if numbered prompts are in use.""" |
|
493 | This feature is only available if numbered prompts are in use.""" | |
494 |
|
494 | |||
495 | shell = self.shell |
|
495 | shell = self.shell | |
496 | if not shell.outputcache.do_full_cache: |
|
496 | if not shell.outputcache.do_full_cache: | |
497 | print 'This feature is only available if numbered prompts are in use.' |
|
497 | print 'This feature is only available if numbered prompts are in use.' | |
498 | return |
|
498 | return | |
499 | opts,args = self.parse_options(parameter_s,'n',mode='list') |
|
499 | opts,args = self.parse_options(parameter_s,'n',mode='list') | |
500 |
|
500 | |||
501 | input_hist = shell.input_hist |
|
501 | input_hist = shell.input_hist | |
502 | default_length = 40 |
|
502 | default_length = 40 | |
503 | if len(args) == 0: |
|
503 | if len(args) == 0: | |
504 | final = len(input_hist) |
|
504 | final = len(input_hist) | |
505 | init = max(1,final-default_length) |
|
505 | init = max(1,final-default_length) | |
506 | elif len(args) == 1: |
|
506 | elif len(args) == 1: | |
507 | final = len(input_hist) |
|
507 | final = len(input_hist) | |
508 | init = max(1,final-int(args[0])) |
|
508 | init = max(1,final-int(args[0])) | |
509 | elif len(args) == 2: |
|
509 | elif len(args) == 2: | |
510 | init,final = map(int,args) |
|
510 | init,final = map(int,args) | |
511 | else: |
|
511 | else: | |
512 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') |
|
512 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') | |
513 | print self.magic_hist.__doc__ |
|
513 | print self.magic_hist.__doc__ | |
514 | return |
|
514 | return | |
515 | width = len(str(final)) |
|
515 | width = len(str(final)) | |
516 | line_sep = ['','\n'] |
|
516 | line_sep = ['','\n'] | |
517 | print_nums = not opts.has_key('n') |
|
517 | print_nums = not opts.has_key('n') | |
518 | for in_num in range(init,final): |
|
518 | for in_num in range(init,final): | |
519 | inline = input_hist[in_num] |
|
519 | inline = input_hist[in_num] | |
520 | multiline = int(inline.count('\n') > 1) |
|
520 | multiline = int(inline.count('\n') > 1) | |
521 | if print_nums: |
|
521 | if print_nums: | |
522 | print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]), |
|
522 | print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]), | |
523 | print inline, |
|
523 | print inline, | |
524 |
|
524 | |||
525 | def magic_hist(self, parameter_s=''): |
|
525 | def magic_hist(self, parameter_s=''): | |
526 | """Alternate name for %history.""" |
|
526 | """Alternate name for %history.""" | |
527 | return self.magic_history(parameter_s) |
|
527 | return self.magic_history(parameter_s) | |
528 |
|
528 | |||
529 | def magic_p(self, parameter_s=''): |
|
529 | def magic_p(self, parameter_s=''): | |
530 | """Just a short alias for Python's 'print'.""" |
|
530 | """Just a short alias for Python's 'print'.""" | |
531 | exec 'print ' + parameter_s in self.shell.user_ns |
|
531 | exec 'print ' + parameter_s in self.shell.user_ns | |
532 |
|
532 | |||
533 | def magic_r(self, parameter_s=''): |
|
533 | def magic_r(self, parameter_s=''): | |
534 | """Repeat previous input. |
|
534 | """Repeat previous input. | |
535 |
|
535 | |||
536 | If given an argument, repeats the previous command which starts with |
|
536 | If given an argument, repeats the previous command which starts with | |
537 | the same string, otherwise it just repeats the previous input. |
|
537 | the same string, otherwise it just repeats the previous input. | |
538 |
|
538 | |||
539 | Shell escaped commands (with ! as first character) are not recognized |
|
539 | Shell escaped commands (with ! as first character) are not recognized | |
540 | by this system, only pure python code and magic commands. |
|
540 | by this system, only pure python code and magic commands. | |
541 | """ |
|
541 | """ | |
542 |
|
542 | |||
543 | start = parameter_s.strip() |
|
543 | start = parameter_s.strip() | |
544 | esc_magic = self.shell.ESC_MAGIC |
|
544 | esc_magic = self.shell.ESC_MAGIC | |
545 | # Identify magic commands even if automagic is on (which means |
|
545 | # Identify magic commands even if automagic is on (which means | |
546 | # the in-memory version is different from that typed by the user). |
|
546 | # the in-memory version is different from that typed by the user). | |
547 | if self.shell.rc.automagic: |
|
547 | if self.shell.rc.automagic: | |
548 | start_magic = esc_magic+start |
|
548 | start_magic = esc_magic+start | |
549 | else: |
|
549 | else: | |
550 | start_magic = start |
|
550 | start_magic = start | |
551 | # Look through the input history in reverse |
|
551 | # Look through the input history in reverse | |
552 | for n in range(len(self.shell.input_hist)-2,0,-1): |
|
552 | for n in range(len(self.shell.input_hist)-2,0,-1): | |
553 | input = self.shell.input_hist[n] |
|
553 | input = self.shell.input_hist[n] | |
554 | # skip plain 'r' lines so we don't recurse to infinity |
|
554 | # skip plain 'r' lines so we don't recurse to infinity | |
555 | if input != 'ipmagic("r")\n' and \ |
|
555 | if input != 'ipmagic("r")\n' and \ | |
556 | (input.startswith(start) or input.startswith(start_magic)): |
|
556 | (input.startswith(start) or input.startswith(start_magic)): | |
557 | #print 'match',`input` # dbg |
|
557 | #print 'match',`input` # dbg | |
558 | print 'Executing:',input, |
|
558 | print 'Executing:',input, | |
559 | self.shell.runlines(input) |
|
559 | self.shell.runlines(input) | |
560 | return |
|
560 | return | |
561 | print 'No previous input matching `%s` found.' % start |
|
561 | print 'No previous input matching `%s` found.' % start | |
562 |
|
562 | |||
563 | def magic_page(self, parameter_s=''): |
|
563 | def magic_page(self, parameter_s=''): | |
564 | """Pretty print the object and display it through a pager. |
|
564 | """Pretty print the object and display it through a pager. | |
565 |
|
565 | |||
566 | If no parameter is given, use _ (last output).""" |
|
566 | If no parameter is given, use _ (last output).""" | |
567 | # After a function contributed by Olivier Aubert, slightly modified. |
|
567 | # After a function contributed by Olivier Aubert, slightly modified. | |
568 |
|
568 | |||
569 | oname = parameter_s and parameter_s or '_' |
|
569 | oname = parameter_s and parameter_s or '_' | |
570 | info = self._ofind(oname) |
|
570 | info = self._ofind(oname) | |
571 | if info['found']: |
|
571 | if info['found']: | |
572 | page(pformat(info['obj'])) |
|
572 | page(pformat(info['obj'])) | |
573 | else: |
|
573 | else: | |
574 | print 'Object `%s` not found' % oname |
|
574 | print 'Object `%s` not found' % oname | |
575 |
|
575 | |||
576 | def magic_profile(self, parameter_s=''): |
|
576 | def magic_profile(self, parameter_s=''): | |
577 | """Print your currently active IPyhton profile.""" |
|
577 | """Print your currently active IPyhton profile.""" | |
578 | if self.shell.rc.profile: |
|
578 | if self.shell.rc.profile: | |
579 | printpl('Current IPython profile: $self.shell.rc.profile.') |
|
579 | printpl('Current IPython profile: $self.shell.rc.profile.') | |
580 | else: |
|
580 | else: | |
581 | print 'No profile active.' |
|
581 | print 'No profile active.' | |
582 |
|
582 | |||
583 | def _inspect(self,meth,oname,**kw): |
|
583 | def _inspect(self,meth,oname,**kw): | |
584 | """Generic interface to the inspector system. |
|
584 | """Generic interface to the inspector system. | |
585 |
|
585 | |||
586 | This function is meant to be called by pdef, pdoc & friends.""" |
|
586 | This function is meant to be called by pdef, pdoc & friends.""" | |
587 |
|
587 | |||
588 | oname = oname.strip() |
|
588 | oname = oname.strip() | |
589 | info = Struct(self._ofind(oname)) |
|
589 | info = Struct(self._ofind(oname)) | |
590 | if info.found: |
|
590 | if info.found: | |
591 | pmethod = getattr(self.shell.inspector,meth) |
|
591 | pmethod = getattr(self.shell.inspector,meth) | |
592 | formatter = info.ismagic and self.format_screen or None |
|
592 | formatter = info.ismagic and self.format_screen or None | |
593 | if meth == 'pdoc': |
|
593 | if meth == 'pdoc': | |
594 | pmethod(info.obj,oname,formatter) |
|
594 | pmethod(info.obj,oname,formatter) | |
595 | elif meth == 'pinfo': |
|
595 | elif meth == 'pinfo': | |
596 | pmethod(info.obj,oname,formatter,info,**kw) |
|
596 | pmethod(info.obj,oname,formatter,info,**kw) | |
597 | else: |
|
597 | else: | |
598 | pmethod(info.obj,oname) |
|
598 | pmethod(info.obj,oname) | |
599 | else: |
|
599 | else: | |
600 | print 'Object `%s` not found.' % oname |
|
600 | print 'Object `%s` not found.' % oname | |
601 | return 'not found' # so callers can take other action |
|
601 | return 'not found' # so callers can take other action | |
602 |
|
602 | |||
603 | def magic_pdef(self, parameter_s=''): |
|
603 | def magic_pdef(self, parameter_s=''): | |
604 | """Print the definition header for any callable object. |
|
604 | """Print the definition header for any callable object. | |
605 |
|
605 | |||
606 | If the object is a class, print the constructor information.""" |
|
606 | If the object is a class, print the constructor information.""" | |
607 | self._inspect('pdef',parameter_s) |
|
607 | self._inspect('pdef',parameter_s) | |
608 |
|
608 | |||
609 | def magic_pdoc(self, parameter_s=''): |
|
609 | def magic_pdoc(self, parameter_s=''): | |
610 | """Print the docstring for an object. |
|
610 | """Print the docstring for an object. | |
611 |
|
611 | |||
612 | If the given object is a class, it will print both the class and the |
|
612 | If the given object is a class, it will print both the class and the | |
613 | constructor docstrings.""" |
|
613 | constructor docstrings.""" | |
614 | self._inspect('pdoc',parameter_s) |
|
614 | self._inspect('pdoc',parameter_s) | |
615 |
|
615 | |||
616 | def magic_psource(self, parameter_s=''): |
|
616 | def magic_psource(self, parameter_s=''): | |
617 | """Print (or run through pager) the source code for an object.""" |
|
617 | """Print (or run through pager) the source code for an object.""" | |
618 | self._inspect('psource',parameter_s) |
|
618 | self._inspect('psource',parameter_s) | |
619 |
|
619 | |||
620 | def magic_pfile(self, parameter_s=''): |
|
620 | def magic_pfile(self, parameter_s=''): | |
621 | """Print (or run through pager) the file where an object is defined. |
|
621 | """Print (or run through pager) the file where an object is defined. | |
622 |
|
622 | |||
623 | The file opens at the line where the object definition begins. IPython |
|
623 | The file opens at the line where the object definition begins. IPython | |
624 | will honor the environment variable PAGER if set, and otherwise will |
|
624 | will honor the environment variable PAGER if set, and otherwise will | |
625 | do its best to print the file in a convenient form. |
|
625 | do its best to print the file in a convenient form. | |
626 |
|
626 | |||
627 | If the given argument is not an object currently defined, IPython will |
|
627 | If the given argument is not an object currently defined, IPython will | |
628 | try to interpret it as a filename (automatically adding a .py extension |
|
628 | try to interpret it as a filename (automatically adding a .py extension | |
629 | if needed). You can thus use %pfile as a syntax highlighting code |
|
629 | if needed). You can thus use %pfile as a syntax highlighting code | |
630 | viewer.""" |
|
630 | viewer.""" | |
631 |
|
631 | |||
632 | # first interpret argument as an object name |
|
632 | # first interpret argument as an object name | |
633 | out = self._inspect('pfile',parameter_s) |
|
633 | out = self._inspect('pfile',parameter_s) | |
634 | # if not, try the input as a filename |
|
634 | # if not, try the input as a filename | |
635 | if out == 'not found': |
|
635 | if out == 'not found': | |
636 | try: |
|
636 | try: | |
637 | filename = get_py_filename(parameter_s) |
|
637 | filename = get_py_filename(parameter_s) | |
638 | except IOError,msg: |
|
638 | except IOError,msg: | |
639 | print msg |
|
639 | print msg | |
640 | return |
|
640 | return | |
641 | page(self.shell.inspector.format(file(filename).read())) |
|
641 | page(self.shell.inspector.format(file(filename).read())) | |
642 |
|
642 | |||
643 | def magic_pinfo(self, parameter_s=''): |
|
643 | def magic_pinfo(self, parameter_s=''): | |
644 | """Provide detailed information about an object. |
|
644 | """Provide detailed information about an object. | |
645 |
|
645 | |||
646 | '%pinfo object' is just a synonym for object? or ?object.""" |
|
646 | '%pinfo object' is just a synonym for object? or ?object.""" | |
647 |
|
647 | |||
648 | #print 'pinfo par: <%s>' % parameter_s # dbg |
|
648 | #print 'pinfo par: <%s>' % parameter_s # dbg | |
649 |
|
649 | |||
650 | # detail_level: 0 -> obj? , 1 -> obj?? |
|
650 | # detail_level: 0 -> obj? , 1 -> obj?? | |
651 | detail_level = 0 |
|
651 | detail_level = 0 | |
652 | # We need to detect if we got called as 'pinfo pinfo foo', which can |
|
652 | # We need to detect if we got called as 'pinfo pinfo foo', which can | |
653 | # happen if the user types 'pinfo foo?' at the cmd line. |
|
653 | # happen if the user types 'pinfo foo?' at the cmd line. | |
654 | pinfo,qmark1,oname,qmark2 = \ |
|
654 | pinfo,qmark1,oname,qmark2 = \ | |
655 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() |
|
655 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() | |
656 | if pinfo or qmark1 or qmark2: |
|
656 | if pinfo or qmark1 or qmark2: | |
657 | detail_level = 1 |
|
657 | detail_level = 1 | |
658 | if "*" in oname: |
|
658 | if "*" in oname: | |
659 | self.magic_psearch(oname) |
|
659 | self.magic_psearch(oname) | |
660 | else: |
|
660 | else: | |
661 | self._inspect('pinfo',oname,detail_level=detail_level) |
|
661 | self._inspect('pinfo',oname,detail_level=detail_level) | |
662 |
|
662 | |||
663 | def magic_psearch(self, parameter_s=''): |
|
663 | def magic_psearch(self, parameter_s=''): | |
664 | """Search for object in namespaces by wildcard. |
|
664 | """Search for object in namespaces by wildcard. | |
665 |
|
665 | |||
666 | %psearch [options] PATTERN [OBJECT TYPE] |
|
666 | %psearch [options] PATTERN [OBJECT TYPE] | |
667 |
|
667 | |||
668 | Note: ? can be used as a synonym for %psearch, at the beginning or at |
|
668 | Note: ? can be used as a synonym for %psearch, at the beginning or at | |
669 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the |
|
669 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the | |
670 | rest of the command line must be unchanged (options come first), so |
|
670 | rest of the command line must be unchanged (options come first), so | |
671 | for example the following forms are equivalent |
|
671 | for example the following forms are equivalent | |
672 |
|
672 | |||
673 | %psearch -i a* function |
|
673 | %psearch -i a* function | |
674 | -i a* function? |
|
674 | -i a* function? | |
675 | ?-i a* function |
|
675 | ?-i a* function | |
676 |
|
676 | |||
677 | Arguments: |
|
677 | Arguments: | |
678 |
|
678 | |||
679 | PATTERN |
|
679 | PATTERN | |
680 |
|
680 | |||
681 | where PATTERN is a string containing * as a wildcard similar to its |
|
681 | where PATTERN is a string containing * as a wildcard similar to its | |
682 | use in a shell. The pattern is matched in all namespaces on the |
|
682 | use in a shell. The pattern is matched in all namespaces on the | |
683 | search path. By default objects starting with a single _ are not |
|
683 | search path. By default objects starting with a single _ are not | |
684 | matched, many IPython generated objects have a single |
|
684 | matched, many IPython generated objects have a single | |
685 | underscore. The default is case insensitive matching. Matching is |
|
685 | underscore. The default is case insensitive matching. Matching is | |
686 | also done on the attributes of objects and not only on the objects |
|
686 | also done on the attributes of objects and not only on the objects | |
687 | in a module. |
|
687 | in a module. | |
688 |
|
688 | |||
689 | [OBJECT TYPE] |
|
689 | [OBJECT TYPE] | |
690 |
|
690 | |||
691 | Is the name of a python type from the types module. The name is |
|
691 | Is the name of a python type from the types module. The name is | |
692 | given in lowercase without the ending type, ex. StringType is |
|
692 | given in lowercase without the ending type, ex. StringType is | |
693 | written string. By adding a type here only objects matching the |
|
693 | written string. By adding a type here only objects matching the | |
694 | given type are matched. Using all here makes the pattern match all |
|
694 | given type are matched. Using all here makes the pattern match all | |
695 | types (this is the default). |
|
695 | types (this is the default). | |
696 |
|
696 | |||
697 | Options: |
|
697 | Options: | |
698 |
|
698 | |||
699 | -a: makes the pattern match even objects whose names start with a |
|
699 | -a: makes the pattern match even objects whose names start with a | |
700 | single underscore. These names are normally ommitted from the |
|
700 | single underscore. These names are normally ommitted from the | |
701 | search. |
|
701 | search. | |
702 |
|
702 | |||
703 | -i/-c: make the pattern case insensitive/sensitive. If neither of |
|
703 | -i/-c: make the pattern case insensitive/sensitive. If neither of | |
704 | these options is given, the default is read from your ipythonrc |
|
704 | these options is given, the default is read from your ipythonrc | |
705 | file. The option name which sets this value is |
|
705 | file. The option name which sets this value is | |
706 | 'wildcards_case_sensitive'. If this option is not specified in your |
|
706 | 'wildcards_case_sensitive'. If this option is not specified in your | |
707 | ipythonrc file, IPython's internal default is to do a case sensitive |
|
707 | ipythonrc file, IPython's internal default is to do a case sensitive | |
708 | search. |
|
708 | search. | |
709 |
|
709 | |||
710 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you |
|
710 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you | |
711 | specifiy can be searched in any of the following namespaces: |
|
711 | specifiy can be searched in any of the following namespaces: | |
712 | 'builtin', 'user', 'user_global','internal', 'alias', where |
|
712 | 'builtin', 'user', 'user_global','internal', 'alias', where | |
713 | 'builtin' and 'user' are the search defaults. Note that you should |
|
713 | 'builtin' and 'user' are the search defaults. Note that you should | |
714 | not use quotes when specifying namespaces. |
|
714 | not use quotes when specifying namespaces. | |
715 |
|
715 | |||
716 | 'Builtin' contains the python module builtin, 'user' contains all |
|
716 | 'Builtin' contains the python module builtin, 'user' contains all | |
717 | user data, 'alias' only contain the shell aliases and no python |
|
717 | user data, 'alias' only contain the shell aliases and no python | |
718 | objects, 'internal' contains objects used by IPython. The |
|
718 | objects, 'internal' contains objects used by IPython. The | |
719 | 'user_global' namespace is only used by embedded IPython instances, |
|
719 | 'user_global' namespace is only used by embedded IPython instances, | |
720 | and it contains module-level globals. You can add namespaces to the |
|
720 | and it contains module-level globals. You can add namespaces to the | |
721 | search with -s or exclude them with -e (these options can be given |
|
721 | search with -s or exclude them with -e (these options can be given | |
722 | more than once). |
|
722 | more than once). | |
723 |
|
723 | |||
724 | Examples: |
|
724 | Examples: | |
725 |
|
725 | |||
726 | %psearch a* -> objects beginning with an a |
|
726 | %psearch a* -> objects beginning with an a | |
727 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a |
|
727 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a | |
728 | %psearch a* function -> all functions beginning with an a |
|
728 | %psearch a* function -> all functions beginning with an a | |
729 | %psearch re.e* -> objects beginning with an e in module re |
|
729 | %psearch re.e* -> objects beginning with an e in module re | |
730 | %psearch r*.e* -> objects that start with e in modules starting in r |
|
730 | %psearch r*.e* -> objects that start with e in modules starting in r | |
731 | %psearch r*.* string -> all strings in modules beginning with r |
|
731 | %psearch r*.* string -> all strings in modules beginning with r | |
732 |
|
732 | |||
733 | Case sensitve search: |
|
733 | Case sensitve search: | |
734 |
|
734 | |||
735 | %psearch -c a* list all object beginning with lower case a |
|
735 | %psearch -c a* list all object beginning with lower case a | |
736 |
|
736 | |||
737 | Show objects beginning with a single _: |
|
737 | Show objects beginning with a single _: | |
738 |
|
738 | |||
739 | %psearch -a _* list objects beginning with a single underscore""" |
|
739 | %psearch -a _* list objects beginning with a single underscore""" | |
740 |
|
740 | |||
741 | # default namespaces to be searched |
|
741 | # default namespaces to be searched | |
742 | def_search = ['user','builtin'] |
|
742 | def_search = ['user','builtin'] | |
743 |
|
743 | |||
744 | # Process options/args |
|
744 | # Process options/args | |
745 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) |
|
745 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) | |
746 | opt = opts.get |
|
746 | opt = opts.get | |
747 | shell = self.shell |
|
747 | shell = self.shell | |
748 | psearch = shell.inspector.psearch |
|
748 | psearch = shell.inspector.psearch | |
749 |
|
749 | |||
750 | # select case options |
|
750 | # select case options | |
751 | if opts.has_key('i'): |
|
751 | if opts.has_key('i'): | |
752 | ignore_case = True |
|
752 | ignore_case = True | |
753 | elif opts.has_key('c'): |
|
753 | elif opts.has_key('c'): | |
754 | ignore_case = False |
|
754 | ignore_case = False | |
755 | else: |
|
755 | else: | |
756 | ignore_case = not shell.rc.wildcards_case_sensitive |
|
756 | ignore_case = not shell.rc.wildcards_case_sensitive | |
757 |
|
757 | |||
758 | # Build list of namespaces to search from user options |
|
758 | # Build list of namespaces to search from user options | |
759 | def_search.extend(opt('s',[])) |
|
759 | def_search.extend(opt('s',[])) | |
760 | ns_exclude = ns_exclude=opt('e',[]) |
|
760 | ns_exclude = ns_exclude=opt('e',[]) | |
761 | ns_search = [nm for nm in def_search if nm not in ns_exclude] |
|
761 | ns_search = [nm for nm in def_search if nm not in ns_exclude] | |
762 |
|
762 | |||
763 | # Call the actual search |
|
763 | # Call the actual search | |
764 | try: |
|
764 | try: | |
765 | psearch(args,shell.ns_table,ns_search, |
|
765 | psearch(args,shell.ns_table,ns_search, | |
766 | show_all=opt('a'),ignore_case=ignore_case) |
|
766 | show_all=opt('a'),ignore_case=ignore_case) | |
767 | except: |
|
767 | except: | |
768 | shell.showtraceback() |
|
768 | shell.showtraceback() | |
769 |
|
769 | |||
770 | def magic_who_ls(self, parameter_s=''): |
|
770 | def magic_who_ls(self, parameter_s=''): | |
771 | """Return a sorted list of all interactive variables. |
|
771 | """Return a sorted list of all interactive variables. | |
772 |
|
772 | |||
773 | If arguments are given, only variables of types matching these |
|
773 | If arguments are given, only variables of types matching these | |
774 | arguments are returned.""" |
|
774 | arguments are returned.""" | |
775 |
|
775 | |||
776 | user_ns = self.shell.user_ns |
|
776 | user_ns = self.shell.user_ns | |
777 | internal_ns = self.shell.internal_ns |
|
777 | internal_ns = self.shell.internal_ns | |
778 | user_config_ns = self.shell.user_config_ns |
|
778 | user_config_ns = self.shell.user_config_ns | |
779 | out = [] |
|
779 | out = [] | |
780 | typelist = parameter_s.split() |
|
780 | typelist = parameter_s.split() | |
781 |
|
781 | |||
782 | for i in user_ns: |
|
782 | for i in user_ns: | |
783 | if not (i.startswith('_') or i.startswith('_i')) \ |
|
783 | if not (i.startswith('_') or i.startswith('_i')) \ | |
784 | and not (i in internal_ns or i in user_config_ns): |
|
784 | and not (i in internal_ns or i in user_config_ns): | |
785 | if typelist: |
|
785 | if typelist: | |
786 | if type(user_ns[i]).__name__ in typelist: |
|
786 | if type(user_ns[i]).__name__ in typelist: | |
787 | out.append(i) |
|
787 | out.append(i) | |
788 | else: |
|
788 | else: | |
789 | out.append(i) |
|
789 | out.append(i) | |
790 | out.sort() |
|
790 | out.sort() | |
791 | return out |
|
791 | return out | |
792 |
|
792 | |||
793 | def magic_who(self, parameter_s=''): |
|
793 | def magic_who(self, parameter_s=''): | |
794 | """Print all interactive variables, with some minimal formatting. |
|
794 | """Print all interactive variables, with some minimal formatting. | |
795 |
|
795 | |||
796 | If any arguments are given, only variables whose type matches one of |
|
796 | If any arguments are given, only variables whose type matches one of | |
797 | these are printed. For example: |
|
797 | these are printed. For example: | |
798 |
|
798 | |||
799 | %who function str |
|
799 | %who function str | |
800 |
|
800 | |||
801 | will only list functions and strings, excluding all other types of |
|
801 | will only list functions and strings, excluding all other types of | |
802 | variables. To find the proper type names, simply use type(var) at a |
|
802 | variables. To find the proper type names, simply use type(var) at a | |
803 | command line to see how python prints type names. For example: |
|
803 | command line to see how python prints type names. For example: | |
804 |
|
804 | |||
805 | In [1]: type('hello')\\ |
|
805 | In [1]: type('hello')\\ | |
806 | Out[1]: <type 'str'> |
|
806 | Out[1]: <type 'str'> | |
807 |
|
807 | |||
808 | indicates that the type name for strings is 'str'. |
|
808 | indicates that the type name for strings is 'str'. | |
809 |
|
809 | |||
810 | %who always excludes executed names loaded through your configuration |
|
810 | %who always excludes executed names loaded through your configuration | |
811 | file and things which are internal to IPython. |
|
811 | file and things which are internal to IPython. | |
812 |
|
812 | |||
813 | This is deliberate, as typically you may load many modules and the |
|
813 | This is deliberate, as typically you may load many modules and the | |
814 | purpose of %who is to show you only what you've manually defined.""" |
|
814 | purpose of %who is to show you only what you've manually defined.""" | |
815 |
|
815 | |||
816 | varlist = self.magic_who_ls(parameter_s) |
|
816 | varlist = self.magic_who_ls(parameter_s) | |
817 | if not varlist: |
|
817 | if not varlist: | |
818 | print 'Interactive namespace is empty.' |
|
818 | print 'Interactive namespace is empty.' | |
819 | return |
|
819 | return | |
820 |
|
820 | |||
821 | # if we have variables, move on... |
|
821 | # if we have variables, move on... | |
822 |
|
822 | |||
823 | # stupid flushing problem: when prompts have no separators, stdout is |
|
823 | # stupid flushing problem: when prompts have no separators, stdout is | |
824 | # getting lost. I'm starting to think this is a python bug. I'm having |
|
824 | # getting lost. I'm starting to think this is a python bug. I'm having | |
825 | # to force a flush with a print because even a sys.stdout.flush |
|
825 | # to force a flush with a print because even a sys.stdout.flush | |
826 | # doesn't seem to do anything! |
|
826 | # doesn't seem to do anything! | |
827 |
|
827 | |||
828 | count = 0 |
|
828 | count = 0 | |
829 | for i in varlist: |
|
829 | for i in varlist: | |
830 | print i+'\t', |
|
830 | print i+'\t', | |
831 | count += 1 |
|
831 | count += 1 | |
832 | if count > 8: |
|
832 | if count > 8: | |
833 | count = 0 |
|
833 | count = 0 | |
834 |
|
834 | |||
835 | sys.stdout.flush() # FIXME. Why the hell isn't this flushing??? |
|
835 | sys.stdout.flush() # FIXME. Why the hell isn't this flushing??? | |
836 |
|
836 | |||
837 | print # well, this does force a flush at the expense of an extra \n |
|
837 | print # well, this does force a flush at the expense of an extra \n | |
838 |
|
838 | |||
839 | def magic_whos(self, parameter_s=''): |
|
839 | def magic_whos(self, parameter_s=''): | |
840 | """Like %who, but gives some extra information about each variable. |
|
840 | """Like %who, but gives some extra information about each variable. | |
841 |
|
841 | |||
842 | The same type filtering of %who can be applied here. |
|
842 | The same type filtering of %who can be applied here. | |
843 |
|
843 | |||
844 | For all variables, the type is printed. Additionally it prints: |
|
844 | For all variables, the type is printed. Additionally it prints: | |
845 |
|
845 | |||
846 | - For {},[],(): their length. |
|
846 | - For {},[],(): their length. | |
847 |
|
847 | |||
848 | - For Numeric arrays, a summary with shape, number of elements, |
|
848 | - For Numeric arrays, a summary with shape, number of elements, | |
849 | typecode and size in memory. |
|
849 | typecode and size in memory. | |
850 |
|
850 | |||
851 | - Everything else: a string representation, snipping their middle if |
|
851 | - Everything else: a string representation, snipping their middle if | |
852 | too long.""" |
|
852 | too long.""" | |
853 |
|
853 | |||
854 | varnames = self.magic_who_ls(parameter_s) |
|
854 | varnames = self.magic_who_ls(parameter_s) | |
855 | if not varnames: |
|
855 | if not varnames: | |
856 | print 'Interactive namespace is empty.' |
|
856 | print 'Interactive namespace is empty.' | |
857 | return |
|
857 | return | |
858 |
|
858 | |||
859 | # if we have variables, move on... |
|
859 | # if we have variables, move on... | |
860 |
|
860 | |||
861 | # for these types, show len() instead of data: |
|
861 | # for these types, show len() instead of data: | |
862 | seq_types = [types.DictType,types.ListType,types.TupleType] |
|
862 | seq_types = [types.DictType,types.ListType,types.TupleType] | |
863 |
|
863 | |||
864 | # for Numeric arrays, display summary info |
|
864 | # for Numeric arrays, display summary info | |
865 | try: |
|
865 | try: | |
866 | import Numeric |
|
866 | import Numeric | |
867 | except ImportError: |
|
867 | except ImportError: | |
868 | array_type = None |
|
868 | array_type = None | |
869 | else: |
|
869 | else: | |
870 | array_type = Numeric.ArrayType.__name__ |
|
870 | array_type = Numeric.ArrayType.__name__ | |
871 |
|
871 | |||
872 | # Find all variable names and types so we can figure out column sizes |
|
872 | # Find all variable names and types so we can figure out column sizes | |
873 | get_vars = lambda i: self.shell.user_ns[i] |
|
873 | get_vars = lambda i: self.shell.user_ns[i] | |
874 | type_name = lambda v: type(v).__name__ |
|
874 | type_name = lambda v: type(v).__name__ | |
875 | varlist = map(get_vars,varnames) |
|
875 | varlist = map(get_vars,varnames) | |
876 |
|
876 | |||
877 | typelist = [] |
|
877 | typelist = [] | |
878 | for vv in varlist: |
|
878 | for vv in varlist: | |
879 | tt = type_name(vv) |
|
879 | tt = type_name(vv) | |
880 | if tt=='instance': |
|
880 | if tt=='instance': | |
881 | typelist.append(str(vv.__class__)) |
|
881 | typelist.append(str(vv.__class__)) | |
882 | else: |
|
882 | else: | |
883 | typelist.append(tt) |
|
883 | typelist.append(tt) | |
884 |
|
884 | |||
885 | # column labels and # of spaces as separator |
|
885 | # column labels and # of spaces as separator | |
886 | varlabel = 'Variable' |
|
886 | varlabel = 'Variable' | |
887 | typelabel = 'Type' |
|
887 | typelabel = 'Type' | |
888 | datalabel = 'Data/Info' |
|
888 | datalabel = 'Data/Info' | |
889 | colsep = 3 |
|
889 | colsep = 3 | |
890 | # variable format strings |
|
890 | # variable format strings | |
891 | vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)" |
|
891 | vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)" | |
892 | vfmt_short = '$vstr[:25]<...>$vstr[-25:]' |
|
892 | vfmt_short = '$vstr[:25]<...>$vstr[-25:]' | |
893 | aformat = "%s: %s elems, type `%s`, %s bytes" |
|
893 | aformat = "%s: %s elems, type `%s`, %s bytes" | |
894 | # find the size of the columns to format the output nicely |
|
894 | # find the size of the columns to format the output nicely | |
895 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep |
|
895 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep | |
896 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep |
|
896 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep | |
897 | # table header |
|
897 | # table header | |
898 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ |
|
898 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ | |
899 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) |
|
899 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) | |
900 | # and the table itself |
|
900 | # and the table itself | |
901 | kb = 1024 |
|
901 | kb = 1024 | |
902 | Mb = 1048576 # kb**2 |
|
902 | Mb = 1048576 # kb**2 | |
903 | for vname,var,vtype in zip(varnames,varlist,typelist): |
|
903 | for vname,var,vtype in zip(varnames,varlist,typelist): | |
904 | print itpl(vformat), |
|
904 | print itpl(vformat), | |
905 | if vtype in seq_types: |
|
905 | if vtype in seq_types: | |
906 | print len(var) |
|
906 | print len(var) | |
907 | elif vtype==array_type: |
|
907 | elif vtype==array_type: | |
908 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] |
|
908 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] | |
909 | vsize = Numeric.size(var) |
|
909 | vsize = Numeric.size(var) | |
910 | vbytes = vsize*var.itemsize() |
|
910 | vbytes = vsize*var.itemsize() | |
911 | if vbytes < 100000: |
|
911 | if vbytes < 100000: | |
912 | print aformat % (vshape,vsize,var.typecode(),vbytes) |
|
912 | print aformat % (vshape,vsize,var.typecode(),vbytes) | |
913 | else: |
|
913 | else: | |
914 | print aformat % (vshape,vsize,var.typecode(),vbytes), |
|
914 | print aformat % (vshape,vsize,var.typecode(),vbytes), | |
915 | if vbytes < Mb: |
|
915 | if vbytes < Mb: | |
916 | print '(%s kb)' % (vbytes/kb,) |
|
916 | print '(%s kb)' % (vbytes/kb,) | |
917 | else: |
|
917 | else: | |
918 | print '(%s Mb)' % (vbytes/Mb,) |
|
918 | print '(%s Mb)' % (vbytes/Mb,) | |
919 | else: |
|
919 | else: | |
920 | vstr = str(var).replace('\n','\\n') |
|
920 | vstr = str(var).replace('\n','\\n') | |
921 | if len(vstr) < 50: |
|
921 | if len(vstr) < 50: | |
922 | print vstr |
|
922 | print vstr | |
923 | else: |
|
923 | else: | |
924 | printpl(vfmt_short) |
|
924 | printpl(vfmt_short) | |
925 |
|
925 | |||
926 | def magic_reset(self, parameter_s=''): |
|
926 | def magic_reset(self, parameter_s=''): | |
927 | """Resets the namespace by removing all names defined by the user. |
|
927 | """Resets the namespace by removing all names defined by the user. | |
928 |
|
928 | |||
929 | Input/Output history are left around in case you need them.""" |
|
929 | Input/Output history are left around in case you need them.""" | |
930 |
|
930 | |||
931 | ans = raw_input( |
|
931 | ans = raw_input( | |
932 | "Once deleted, variables cannot be recovered. Proceed (y/n)? ") |
|
932 | "Once deleted, variables cannot be recovered. Proceed (y/n)? ") | |
933 | if not ans.lower() == 'y': |
|
933 | if not ans.lower() == 'y': | |
934 | print 'Nothing done.' |
|
934 | print 'Nothing done.' | |
935 | return |
|
935 | return | |
936 | user_ns = self.shell.user_ns |
|
936 | user_ns = self.shell.user_ns | |
937 | for i in self.magic_who_ls(): |
|
937 | for i in self.magic_who_ls(): | |
938 | del(user_ns[i]) |
|
938 | del(user_ns[i]) | |
939 |
|
939 | |||
940 | def magic_config(self,parameter_s=''): |
|
940 | def magic_config(self,parameter_s=''): | |
941 | """Show IPython's internal configuration.""" |
|
941 | """Show IPython's internal configuration.""" | |
942 |
|
942 | |||
943 | page('Current configuration structure:\n'+ |
|
943 | page('Current configuration structure:\n'+ | |
944 | pformat(self.shell.rc.dict())) |
|
944 | pformat(self.shell.rc.dict())) | |
945 |
|
945 | |||
946 | def magic_logstart(self,parameter_s=''): |
|
946 | def magic_logstart(self,parameter_s=''): | |
947 | """Start logging anywhere in a session. |
|
947 | """Start logging anywhere in a session. | |
948 |
|
948 | |||
949 | %logstart [-o|-t] [log_name [log_mode]] |
|
949 | %logstart [-o|-t] [log_name [log_mode]] | |
950 |
|
950 | |||
951 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
951 | If no name is given, it defaults to a file named 'ipython_log.py' in your | |
952 | current directory, in 'rotate' mode (see below). |
|
952 | current directory, in 'rotate' mode (see below). | |
953 |
|
953 | |||
954 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
954 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your | |
955 | history up to that point and then continues logging. |
|
955 | history up to that point and then continues logging. | |
956 |
|
956 | |||
957 | %logstart takes a second optional parameter: logging mode. This can be one |
|
957 | %logstart takes a second optional parameter: logging mode. This can be one | |
958 | of (note that the modes are given unquoted):\\ |
|
958 | of (note that the modes are given unquoted):\\ | |
959 | append: well, that says it.\\ |
|
959 | append: well, that says it.\\ | |
960 | backup: rename (if exists) to name~ and start name.\\ |
|
960 | backup: rename (if exists) to name~ and start name.\\ | |
961 | global: single logfile in your home dir, appended to.\\ |
|
961 | global: single logfile in your home dir, appended to.\\ | |
962 | over : overwrite existing log.\\ |
|
962 | over : overwrite existing log.\\ | |
963 | rotate: create rotating logs name.1~, name.2~, etc. |
|
963 | rotate: create rotating logs name.1~, name.2~, etc. | |
964 |
|
964 | |||
965 | Options: |
|
965 | Options: | |
966 |
|
966 | |||
967 | -o: log also IPython's output. In this mode, all commands which |
|
967 | -o: log also IPython's output. In this mode, all commands which | |
968 | generate an Out[NN] prompt are recorded to the logfile, right after |
|
968 | generate an Out[NN] prompt are recorded to the logfile, right after | |
969 | their corresponding input line. The output lines are always |
|
969 | their corresponding input line. The output lines are always | |
970 | prepended with a '#[Out]# ' marker, so that the log remains valid |
|
970 | prepended with a '#[Out]# ' marker, so that the log remains valid | |
971 | Python code. |
|
971 | Python code. | |
972 |
|
972 | |||
973 | Since this marker is always the same, filtering only the output from |
|
973 | Since this marker is always the same, filtering only the output from | |
974 | a log is very easy, using for example a simple awk call: |
|
974 | a log is very easy, using for example a simple awk call: | |
975 |
|
975 | |||
976 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py |
|
976 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py | |
977 |
|
977 | |||
978 | -t: put timestamps before each input line logged (these are put in |
|
978 | -t: put timestamps before each input line logged (these are put in | |
979 | comments).""" |
|
979 | comments).""" | |
980 |
|
980 | |||
981 | opts,par = self.parse_options(parameter_s,'ot') |
|
981 | opts,par = self.parse_options(parameter_s,'ot') | |
982 | log_output = 'o' in opts |
|
982 | log_output = 'o' in opts | |
983 | timestamp = 't' in opts |
|
983 | timestamp = 't' in opts | |
984 |
|
984 | |||
985 | rc = self.shell.rc |
|
985 | rc = self.shell.rc | |
986 | logger = self.shell.logger |
|
986 | logger = self.shell.logger | |
987 |
|
987 | |||
988 | # if no args are given, the defaults set in the logger constructor by |
|
988 | # if no args are given, the defaults set in the logger constructor by | |
989 | # ipytohn remain valid |
|
989 | # ipytohn remain valid | |
990 | if par: |
|
990 | if par: | |
991 | try: |
|
991 | try: | |
992 | logfname,logmode = par.split() |
|
992 | logfname,logmode = par.split() | |
993 | except: |
|
993 | except: | |
994 | logfname = par |
|
994 | logfname = par | |
995 | logmode = 'backup' |
|
995 | logmode = 'backup' | |
996 | else: |
|
996 | else: | |
997 | logfname = logger.logfname |
|
997 | logfname = logger.logfname | |
998 | logmode = logger.logmode |
|
998 | logmode = logger.logmode | |
999 | # put logfname into rc struct as if it had been called on the command |
|
999 | # put logfname into rc struct as if it had been called on the command | |
1000 | # line, so it ends up saved in the log header Save it in case we need |
|
1000 | # line, so it ends up saved in the log header Save it in case we need | |
1001 | # to restore it... |
|
1001 | # to restore it... | |
1002 | old_logfile = rc.opts.get('logfile','') |
|
1002 | old_logfile = rc.opts.get('logfile','') | |
1003 | if logfname: |
|
1003 | if logfname: | |
1004 | logfname = os.path.expanduser(logfname) |
|
1004 | logfname = os.path.expanduser(logfname) | |
1005 | rc.opts.logfile = logfname |
|
1005 | rc.opts.logfile = logfname | |
1006 | loghead = self.shell.loghead_tpl % (rc.opts,rc.args) |
|
1006 | loghead = self.shell.loghead_tpl % (rc.opts,rc.args) | |
1007 | try: |
|
1007 | try: | |
1008 | started = logger.logstart(logfname,loghead,logmode, |
|
1008 | started = logger.logstart(logfname,loghead,logmode, | |
1009 | log_output,timestamp) |
|
1009 | log_output,timestamp) | |
1010 | except: |
|
1010 | except: | |
1011 | rc.opts.logfile = old_logfile |
|
1011 | rc.opts.logfile = old_logfile | |
1012 | warn("Couldn't start log: %s" % sys.exc_info()[1]) |
|
1012 | warn("Couldn't start log: %s" % sys.exc_info()[1]) | |
1013 | else: |
|
1013 | else: | |
1014 | # log input history up to this point, optionally interleaving |
|
1014 | # log input history up to this point, optionally interleaving | |
1015 | # output if requested |
|
1015 | # output if requested | |
1016 |
|
1016 | |||
1017 | if timestamp: |
|
1017 | if timestamp: | |
1018 | # disable timestamping for the previous history, since we've |
|
1018 | # disable timestamping for the previous history, since we've | |
1019 | # lost those already (no time machine here). |
|
1019 | # lost those already (no time machine here). | |
1020 | logger.timestamp = False |
|
1020 | logger.timestamp = False | |
1021 | if log_output: |
|
1021 | if log_output: | |
1022 | log_write = logger.log_write |
|
1022 | log_write = logger.log_write | |
1023 | input_hist = self.shell.input_hist |
|
1023 | input_hist = self.shell.input_hist | |
1024 | output_hist = self.shell.output_hist |
|
1024 | output_hist = self.shell.output_hist | |
1025 | for n in range(1,len(input_hist)-1): |
|
1025 | for n in range(1,len(input_hist)-1): | |
1026 | log_write(input_hist[n].rstrip()) |
|
1026 | log_write(input_hist[n].rstrip()) | |
1027 | if n in output_hist: |
|
1027 | if n in output_hist: | |
1028 | log_write(repr(output_hist[n]),'output') |
|
1028 | log_write(repr(output_hist[n]),'output') | |
1029 | else: |
|
1029 | else: | |
1030 | logger.log_write(self.shell.input_hist[1:]) |
|
1030 | logger.log_write(self.shell.input_hist[1:]) | |
1031 | if timestamp: |
|
1031 | if timestamp: | |
1032 | # re-enable timestamping |
|
1032 | # re-enable timestamping | |
1033 | logger.timestamp = True |
|
1033 | logger.timestamp = True | |
1034 |
|
1034 | |||
1035 | print ('Activating auto-logging. ' |
|
1035 | print ('Activating auto-logging. ' | |
1036 | 'Current session state plus future input saved.') |
|
1036 | 'Current session state plus future input saved.') | |
1037 | logger.logstate() |
|
1037 | logger.logstate() | |
1038 |
|
1038 | |||
1039 | def magic_logoff(self,parameter_s=''): |
|
1039 | def magic_logoff(self,parameter_s=''): | |
1040 | """Temporarily stop logging. |
|
1040 | """Temporarily stop logging. | |
1041 |
|
1041 | |||
1042 | You must have previously started logging.""" |
|
1042 | You must have previously started logging.""" | |
1043 | self.shell.logger.switch_log(0) |
|
1043 | self.shell.logger.switch_log(0) | |
1044 |
|
1044 | |||
1045 | def magic_logon(self,parameter_s=''): |
|
1045 | def magic_logon(self,parameter_s=''): | |
1046 | """Restart logging. |
|
1046 | """Restart logging. | |
1047 |
|
1047 | |||
1048 | This function is for restarting logging which you've temporarily |
|
1048 | This function is for restarting logging which you've temporarily | |
1049 | stopped with %logoff. For starting logging for the first time, you |
|
1049 | stopped with %logoff. For starting logging for the first time, you | |
1050 | must use the %logstart function, which allows you to specify an |
|
1050 | must use the %logstart function, which allows you to specify an | |
1051 | optional log filename.""" |
|
1051 | optional log filename.""" | |
1052 |
|
1052 | |||
1053 | self.shell.logger.switch_log(1) |
|
1053 | self.shell.logger.switch_log(1) | |
1054 |
|
1054 | |||
1055 | def magic_logstate(self,parameter_s=''): |
|
1055 | def magic_logstate(self,parameter_s=''): | |
1056 | """Print the status of the logging system.""" |
|
1056 | """Print the status of the logging system.""" | |
1057 |
|
1057 | |||
1058 | self.shell.logger.logstate() |
|
1058 | self.shell.logger.logstate() | |
1059 |
|
1059 | |||
1060 | def magic_pdb(self, parameter_s=''): |
|
1060 | def magic_pdb(self, parameter_s=''): | |
1061 | """Control the calling of the pdb interactive debugger. |
|
1061 | """Control the calling of the pdb interactive debugger. | |
1062 |
|
1062 | |||
1063 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
1063 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without | |
1064 | argument it works as a toggle. |
|
1064 | argument it works as a toggle. | |
1065 |
|
1065 | |||
1066 | When an exception is triggered, IPython can optionally call the |
|
1066 | When an exception is triggered, IPython can optionally call the | |
1067 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
1067 | interactive pdb debugger after the traceback printout. %pdb toggles | |
1068 | this feature on and off.""" |
|
1068 | this feature on and off.""" | |
1069 |
|
1069 | |||
1070 | par = parameter_s.strip().lower() |
|
1070 | par = parameter_s.strip().lower() | |
1071 |
|
1071 | |||
1072 | if par: |
|
1072 | if par: | |
1073 | try: |
|
1073 | try: | |
1074 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
1074 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] | |
1075 | except KeyError: |
|
1075 | except KeyError: | |
1076 | print ('Incorrect argument. Use on/1, off/0, ' |
|
1076 | print ('Incorrect argument. Use on/1, off/0, ' | |
1077 | 'or nothing for a toggle.') |
|
1077 | 'or nothing for a toggle.') | |
1078 | return |
|
1078 | return | |
1079 | else: |
|
1079 | else: | |
1080 | # toggle |
|
1080 | # toggle | |
1081 | new_pdb = not self.shell.InteractiveTB.call_pdb |
|
1081 | new_pdb = not self.shell.InteractiveTB.call_pdb | |
1082 |
|
1082 | |||
1083 | # set on the shell |
|
1083 | # set on the shell | |
1084 | self.shell.call_pdb = new_pdb |
|
1084 | self.shell.call_pdb = new_pdb | |
1085 | print 'Automatic pdb calling has been turned',on_off(new_pdb) |
|
1085 | print 'Automatic pdb calling has been turned',on_off(new_pdb) | |
1086 |
|
1086 | |||
1087 | def magic_prun(self, parameter_s ='',user_mode=1, |
|
1087 | def magic_prun(self, parameter_s ='',user_mode=1, | |
1088 | opts=None,arg_lst=None,prog_ns=None): |
|
1088 | opts=None,arg_lst=None,prog_ns=None): | |
1089 |
|
1089 | |||
1090 | """Run a statement through the python code profiler. |
|
1090 | """Run a statement through the python code profiler. | |
1091 |
|
1091 | |||
1092 | Usage:\\ |
|
1092 | Usage:\\ | |
1093 | %prun [options] statement |
|
1093 | %prun [options] statement | |
1094 |
|
1094 | |||
1095 | The given statement (which doesn't require quote marks) is run via the |
|
1095 | The given statement (which doesn't require quote marks) is run via the | |
1096 | python profiler in a manner similar to the profile.run() function. |
|
1096 | python profiler in a manner similar to the profile.run() function. | |
1097 | Namespaces are internally managed to work correctly; profile.run |
|
1097 | Namespaces are internally managed to work correctly; profile.run | |
1098 | cannot be used in IPython because it makes certain assumptions about |
|
1098 | cannot be used in IPython because it makes certain assumptions about | |
1099 | namespaces which do not hold under IPython. |
|
1099 | namespaces which do not hold under IPython. | |
1100 |
|
1100 | |||
1101 | Options: |
|
1101 | Options: | |
1102 |
|
1102 | |||
1103 | -l <limit>: you can place restrictions on what or how much of the |
|
1103 | -l <limit>: you can place restrictions on what or how much of the | |
1104 | profile gets printed. The limit value can be: |
|
1104 | profile gets printed. The limit value can be: | |
1105 |
|
1105 | |||
1106 | * A string: only information for function names containing this string |
|
1106 | * A string: only information for function names containing this string | |
1107 | is printed. |
|
1107 | is printed. | |
1108 |
|
1108 | |||
1109 | * An integer: only these many lines are printed. |
|
1109 | * An integer: only these many lines are printed. | |
1110 |
|
1110 | |||
1111 | * A float (between 0 and 1): this fraction of the report is printed |
|
1111 | * A float (between 0 and 1): this fraction of the report is printed | |
1112 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
1112 | (for example, use a limit of 0.4 to see the topmost 40% only). | |
1113 |
|
1113 | |||
1114 | You can combine several limits with repeated use of the option. For |
|
1114 | You can combine several limits with repeated use of the option. For | |
1115 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
1115 | example, '-l __init__ -l 5' will print only the topmost 5 lines of | |
1116 | information about class constructors. |
|
1116 | information about class constructors. | |
1117 |
|
1117 | |||
1118 | -r: return the pstats.Stats object generated by the profiling. This |
|
1118 | -r: return the pstats.Stats object generated by the profiling. This | |
1119 | object has all the information about the profile in it, and you can |
|
1119 | object has all the information about the profile in it, and you can | |
1120 | later use it for further analysis or in other functions. |
|
1120 | later use it for further analysis or in other functions. | |
1121 |
|
1121 | |||
1122 | Since magic functions have a particular form of calling which prevents |
|
1122 | Since magic functions have a particular form of calling which prevents | |
1123 | you from writing something like:\\ |
|
1123 | you from writing something like:\\ | |
1124 | In [1]: p = %prun -r print 4 # invalid!\\ |
|
1124 | In [1]: p = %prun -r print 4 # invalid!\\ | |
1125 | you must instead use IPython's automatic variables to assign this:\\ |
|
1125 | you must instead use IPython's automatic variables to assign this:\\ | |
1126 | In [1]: %prun -r print 4 \\ |
|
1126 | In [1]: %prun -r print 4 \\ | |
1127 | Out[1]: <pstats.Stats instance at 0x8222cec>\\ |
|
1127 | Out[1]: <pstats.Stats instance at 0x8222cec>\\ | |
1128 | In [2]: stats = _ |
|
1128 | In [2]: stats = _ | |
1129 |
|
1129 | |||
1130 | If you really need to assign this value via an explicit function call, |
|
1130 | If you really need to assign this value via an explicit function call, | |
1131 | you can always tap directly into the true name of the magic function |
|
1131 | you can always tap directly into the true name of the magic function | |
1132 | by using the ipmagic function (which IPython automatically adds to the |
|
1132 | by using the ipmagic function (which IPython automatically adds to the | |
1133 | builtins):\\ |
|
1133 | builtins):\\ | |
1134 | In [3]: stats = ipmagic('prun','-r print 4') |
|
1134 | In [3]: stats = ipmagic('prun','-r print 4') | |
1135 |
|
1135 | |||
1136 | You can type ipmagic? for more details on ipmagic. |
|
1136 | You can type ipmagic? for more details on ipmagic. | |
1137 |
|
1137 | |||
1138 | -s <key>: sort profile by given key. You can provide more than one key |
|
1138 | -s <key>: sort profile by given key. You can provide more than one key | |
1139 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
1139 | by using the option several times: '-s key1 -s key2 -s key3...'. The | |
1140 | default sorting key is 'time'. |
|
1140 | default sorting key is 'time'. | |
1141 |
|
1141 | |||
1142 | The following is copied verbatim from the profile documentation |
|
1142 | The following is copied verbatim from the profile documentation | |
1143 | referenced below: |
|
1143 | referenced below: | |
1144 |
|
1144 | |||
1145 | When more than one key is provided, additional keys are used as |
|
1145 | When more than one key is provided, additional keys are used as | |
1146 | secondary criteria when the there is equality in all keys selected |
|
1146 | secondary criteria when the there is equality in all keys selected | |
1147 | before them. |
|
1147 | before them. | |
1148 |
|
1148 | |||
1149 | Abbreviations can be used for any key names, as long as the |
|
1149 | Abbreviations can be used for any key names, as long as the | |
1150 | abbreviation is unambiguous. The following are the keys currently |
|
1150 | abbreviation is unambiguous. The following are the keys currently | |
1151 | defined: |
|
1151 | defined: | |
1152 |
|
1152 | |||
1153 | Valid Arg Meaning\\ |
|
1153 | Valid Arg Meaning\\ | |
1154 | "calls" call count\\ |
|
1154 | "calls" call count\\ | |
1155 | "cumulative" cumulative time\\ |
|
1155 | "cumulative" cumulative time\\ | |
1156 | "file" file name\\ |
|
1156 | "file" file name\\ | |
1157 | "module" file name\\ |
|
1157 | "module" file name\\ | |
1158 | "pcalls" primitive call count\\ |
|
1158 | "pcalls" primitive call count\\ | |
1159 | "line" line number\\ |
|
1159 | "line" line number\\ | |
1160 | "name" function name\\ |
|
1160 | "name" function name\\ | |
1161 | "nfl" name/file/line\\ |
|
1161 | "nfl" name/file/line\\ | |
1162 | "stdname" standard name\\ |
|
1162 | "stdname" standard name\\ | |
1163 | "time" internal time |
|
1163 | "time" internal time | |
1164 |
|
1164 | |||
1165 | Note that all sorts on statistics are in descending order (placing |
|
1165 | Note that all sorts on statistics are in descending order (placing | |
1166 | most time consuming items first), where as name, file, and line number |
|
1166 | most time consuming items first), where as name, file, and line number | |
1167 | searches are in ascending order (i.e., alphabetical). The subtle |
|
1167 | searches are in ascending order (i.e., alphabetical). The subtle | |
1168 | distinction between "nfl" and "stdname" is that the standard name is a |
|
1168 | distinction between "nfl" and "stdname" is that the standard name is a | |
1169 | sort of the name as printed, which means that the embedded line |
|
1169 | sort of the name as printed, which means that the embedded line | |
1170 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
1170 | numbers get compared in an odd way. For example, lines 3, 20, and 40 | |
1171 | would (if the file names were the same) appear in the string order |
|
1171 | would (if the file names were the same) appear in the string order | |
1172 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
1172 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the | |
1173 | line numbers. In fact, sort_stats("nfl") is the same as |
|
1173 | line numbers. In fact, sort_stats("nfl") is the same as | |
1174 | sort_stats("name", "file", "line"). |
|
1174 | sort_stats("name", "file", "line"). | |
1175 |
|
1175 | |||
1176 | -T <filename>: save profile results as shown on screen to a text |
|
1176 | -T <filename>: save profile results as shown on screen to a text | |
1177 | file. The profile is still shown on screen. |
|
1177 | file. The profile is still shown on screen. | |
1178 |
|
1178 | |||
1179 | -D <filename>: save (via dump_stats) profile statistics to given |
|
1179 | -D <filename>: save (via dump_stats) profile statistics to given | |
1180 | filename. This data is in a format understod by the pstats module, and |
|
1180 | filename. This data is in a format understod by the pstats module, and | |
1181 | is generated by a call to the dump_stats() method of profile |
|
1181 | is generated by a call to the dump_stats() method of profile | |
1182 | objects. The profile is still shown on screen. |
|
1182 | objects. The profile is still shown on screen. | |
1183 |
|
1183 | |||
1184 | If you want to run complete programs under the profiler's control, use |
|
1184 | If you want to run complete programs under the profiler's control, use | |
1185 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
1185 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts | |
1186 | contains profiler specific options as described here. |
|
1186 | contains profiler specific options as described here. | |
1187 |
|
1187 | |||
1188 | You can read the complete documentation for the profile module with:\\ |
|
1188 | You can read the complete documentation for the profile module with:\\ | |
1189 | In [1]: import profile; profile.help() """ |
|
1189 | In [1]: import profile; profile.help() """ | |
1190 |
|
1190 | |||
1191 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) |
|
1191 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) | |
1192 | # protect user quote marks |
|
1192 | # protect user quote marks | |
1193 | parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'") |
|
1193 | parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'") | |
1194 |
|
1194 | |||
1195 | if user_mode: # regular user call |
|
1195 | if user_mode: # regular user call | |
1196 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:', |
|
1196 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:', | |
1197 | list_all=1) |
|
1197 | list_all=1) | |
1198 | namespace = self.shell.user_ns |
|
1198 | namespace = self.shell.user_ns | |
1199 | else: # called to run a program by %run -p |
|
1199 | else: # called to run a program by %run -p | |
1200 | try: |
|
1200 | try: | |
1201 | filename = get_py_filename(arg_lst[0]) |
|
1201 | filename = get_py_filename(arg_lst[0]) | |
1202 | except IOError,msg: |
|
1202 | except IOError,msg: | |
1203 | error(msg) |
|
1203 | error(msg) | |
1204 | return |
|
1204 | return | |
1205 |
|
1205 | |||
1206 | arg_str = 'execfile(filename,prog_ns)' |
|
1206 | arg_str = 'execfile(filename,prog_ns)' | |
1207 | namespace = locals() |
|
1207 | namespace = locals() | |
1208 |
|
1208 | |||
1209 | opts.merge(opts_def) |
|
1209 | opts.merge(opts_def) | |
1210 |
|
1210 | |||
1211 | prof = profile.Profile() |
|
1211 | prof = profile.Profile() | |
1212 | try: |
|
1212 | try: | |
1213 | prof = prof.runctx(arg_str,namespace,namespace) |
|
1213 | prof = prof.runctx(arg_str,namespace,namespace) | |
1214 | sys_exit = '' |
|
1214 | sys_exit = '' | |
1215 | except SystemExit: |
|
1215 | except SystemExit: | |
1216 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
1216 | sys_exit = """*** SystemExit exception caught in code being profiled.""" | |
1217 |
|
1217 | |||
1218 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
1218 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) | |
1219 |
|
1219 | |||
1220 | lims = opts.l |
|
1220 | lims = opts.l | |
1221 | if lims: |
|
1221 | if lims: | |
1222 | lims = [] # rebuild lims with ints/floats/strings |
|
1222 | lims = [] # rebuild lims with ints/floats/strings | |
1223 | for lim in opts.l: |
|
1223 | for lim in opts.l: | |
1224 | try: |
|
1224 | try: | |
1225 | lims.append(int(lim)) |
|
1225 | lims.append(int(lim)) | |
1226 | except ValueError: |
|
1226 | except ValueError: | |
1227 | try: |
|
1227 | try: | |
1228 | lims.append(float(lim)) |
|
1228 | lims.append(float(lim)) | |
1229 | except ValueError: |
|
1229 | except ValueError: | |
1230 | lims.append(lim) |
|
1230 | lims.append(lim) | |
1231 |
|
1231 | |||
1232 | # trap output |
|
1232 | # trap output | |
1233 | sys_stdout = sys.stdout |
|
1233 | sys_stdout = sys.stdout | |
1234 | stdout_trap = StringIO() |
|
1234 | stdout_trap = StringIO() | |
1235 | try: |
|
1235 | try: | |
1236 | sys.stdout = stdout_trap |
|
1236 | sys.stdout = stdout_trap | |
1237 | stats.print_stats(*lims) |
|
1237 | stats.print_stats(*lims) | |
1238 | finally: |
|
1238 | finally: | |
1239 | sys.stdout = sys_stdout |
|
1239 | sys.stdout = sys_stdout | |
1240 | output = stdout_trap.getvalue() |
|
1240 | output = stdout_trap.getvalue() | |
1241 | output = output.rstrip() |
|
1241 | output = output.rstrip() | |
1242 |
|
1242 | |||
1243 | page(output,screen_lines=self.shell.rc.screen_length) |
|
1243 | page(output,screen_lines=self.shell.rc.screen_length) | |
1244 | print sys_exit, |
|
1244 | print sys_exit, | |
1245 |
|
1245 | |||
1246 | dump_file = opts.D[0] |
|
1246 | dump_file = opts.D[0] | |
1247 | text_file = opts.T[0] |
|
1247 | text_file = opts.T[0] | |
1248 | if dump_file: |
|
1248 | if dump_file: | |
1249 | prof.dump_stats(dump_file) |
|
1249 | prof.dump_stats(dump_file) | |
1250 | print '\n*** Profile stats marshalled to file',\ |
|
1250 | print '\n*** Profile stats marshalled to file',\ | |
1251 | `dump_file`+'.',sys_exit |
|
1251 | `dump_file`+'.',sys_exit | |
1252 | if text_file: |
|
1252 | if text_file: | |
1253 | file(text_file,'w').write(output) |
|
1253 | file(text_file,'w').write(output) | |
1254 | print '\n*** Profile printout saved to text file',\ |
|
1254 | print '\n*** Profile printout saved to text file',\ | |
1255 | `text_file`+'.',sys_exit |
|
1255 | `text_file`+'.',sys_exit | |
1256 |
|
1256 | |||
1257 | if opts.has_key('r'): |
|
1257 | if opts.has_key('r'): | |
1258 | return stats |
|
1258 | return stats | |
1259 | else: |
|
1259 | else: | |
1260 | return None |
|
1260 | return None | |
1261 |
|
1261 | |||
1262 | def magic_run(self, parameter_s ='',runner=None): |
|
1262 | def magic_run(self, parameter_s ='',runner=None): | |
1263 | """Run the named file inside IPython as a program. |
|
1263 | """Run the named file inside IPython as a program. | |
1264 |
|
1264 | |||
1265 | Usage:\\ |
|
1265 | Usage:\\ | |
1266 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] |
|
1266 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] | |
1267 |
|
1267 | |||
1268 | Parameters after the filename are passed as command-line arguments to |
|
1268 | Parameters after the filename are passed as command-line arguments to | |
1269 | the program (put in sys.argv). Then, control returns to IPython's |
|
1269 | the program (put in sys.argv). Then, control returns to IPython's | |
1270 | prompt. |
|
1270 | prompt. | |
1271 |
|
1271 | |||
1272 | This is similar to running at a system prompt:\\ |
|
1272 | This is similar to running at a system prompt:\\ | |
1273 | $ python file args\\ |
|
1273 | $ python file args\\ | |
1274 | but with the advantage of giving you IPython's tracebacks, and of |
|
1274 | but with the advantage of giving you IPython's tracebacks, and of | |
1275 | loading all variables into your interactive namespace for further use |
|
1275 | loading all variables into your interactive namespace for further use | |
1276 | (unless -p is used, see below). |
|
1276 | (unless -p is used, see below). | |
1277 |
|
1277 | |||
1278 | The file is executed in a namespace initially consisting only of |
|
1278 | The file is executed in a namespace initially consisting only of | |
1279 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
1279 | __name__=='__main__' and sys.argv constructed as indicated. It thus | |
1280 | sees its environment as if it were being run as a stand-alone |
|
1280 | sees its environment as if it were being run as a stand-alone | |
1281 | program. But after execution, the IPython interactive namespace gets |
|
1281 | program. But after execution, the IPython interactive namespace gets | |
1282 | updated with all variables defined in the program (except for __name__ |
|
1282 | updated with all variables defined in the program (except for __name__ | |
1283 | and sys.argv). This allows for very convenient loading of code for |
|
1283 | and sys.argv). This allows for very convenient loading of code for | |
1284 | interactive work, while giving each program a 'clean sheet' to run in. |
|
1284 | interactive work, while giving each program a 'clean sheet' to run in. | |
1285 |
|
1285 | |||
1286 | Options: |
|
1286 | Options: | |
1287 |
|
1287 | |||
1288 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
1288 | -n: __name__ is NOT set to '__main__', but to the running file's name | |
1289 | without extension (as python does under import). This allows running |
|
1289 | without extension (as python does under import). This allows running | |
1290 | scripts and reloading the definitions in them without calling code |
|
1290 | scripts and reloading the definitions in them without calling code | |
1291 | protected by an ' if __name__ == "__main__" ' clause. |
|
1291 | protected by an ' if __name__ == "__main__" ' clause. | |
1292 |
|
1292 | |||
1293 | -i: run the file in IPython's namespace instead of an empty one. This |
|
1293 | -i: run the file in IPython's namespace instead of an empty one. This | |
1294 | is useful if you are experimenting with code written in a text editor |
|
1294 | is useful if you are experimenting with code written in a text editor | |
1295 | which depends on variables defined interactively. |
|
1295 | which depends on variables defined interactively. | |
1296 |
|
1296 | |||
1297 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
1297 | -e: ignore sys.exit() calls or SystemExit exceptions in the script | |
1298 | being run. This is particularly useful if IPython is being used to |
|
1298 | being run. This is particularly useful if IPython is being used to | |
1299 | run unittests, which always exit with a sys.exit() call. In such |
|
1299 | run unittests, which always exit with a sys.exit() call. In such | |
1300 | cases you are interested in the output of the test results, not in |
|
1300 | cases you are interested in the output of the test results, not in | |
1301 | seeing a traceback of the unittest module. |
|
1301 | seeing a traceback of the unittest module. | |
1302 |
|
1302 | |||
1303 | -t: print timing information at the end of the run. IPython will give |
|
1303 | -t: print timing information at the end of the run. IPython will give | |
1304 | you an estimated CPU time consumption for your script, which under |
|
1304 | you an estimated CPU time consumption for your script, which under | |
1305 | Unix uses the resource module to avoid the wraparound problems of |
|
1305 | Unix uses the resource module to avoid the wraparound problems of | |
1306 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
1306 | time.clock(). Under Unix, an estimate of time spent on system tasks | |
1307 | is also given (for Windows platforms this is reported as 0.0). |
|
1307 | is also given (for Windows platforms this is reported as 0.0). | |
1308 |
|
1308 | |||
1309 | If -t is given, an additional -N<N> option can be given, where <N> |
|
1309 | If -t is given, an additional -N<N> option can be given, where <N> | |
1310 | must be an integer indicating how many times you want the script to |
|
1310 | must be an integer indicating how many times you want the script to | |
1311 | run. The final timing report will include total and per run results. |
|
1311 | run. The final timing report will include total and per run results. | |
1312 |
|
1312 | |||
1313 | For example (testing the script uniq_stable.py): |
|
1313 | For example (testing the script uniq_stable.py): | |
1314 |
|
1314 | |||
1315 | In [1]: run -t uniq_stable |
|
1315 | In [1]: run -t uniq_stable | |
1316 |
|
1316 | |||
1317 | IPython CPU timings (estimated):\\ |
|
1317 | IPython CPU timings (estimated):\\ | |
1318 | User : 0.19597 s.\\ |
|
1318 | User : 0.19597 s.\\ | |
1319 | System: 0.0 s.\\ |
|
1319 | System: 0.0 s.\\ | |
1320 |
|
1320 | |||
1321 | In [2]: run -t -N5 uniq_stable |
|
1321 | In [2]: run -t -N5 uniq_stable | |
1322 |
|
1322 | |||
1323 | IPython CPU timings (estimated):\\ |
|
1323 | IPython CPU timings (estimated):\\ | |
1324 | Total runs performed: 5\\ |
|
1324 | Total runs performed: 5\\ | |
1325 | Times : Total Per run\\ |
|
1325 | Times : Total Per run\\ | |
1326 | User : 0.910862 s, 0.1821724 s.\\ |
|
1326 | User : 0.910862 s, 0.1821724 s.\\ | |
1327 | System: 0.0 s, 0.0 s. |
|
1327 | System: 0.0 s, 0.0 s. | |
1328 |
|
1328 | |||
1329 | -d: run your program under the control of pdb, the Python debugger. |
|
1329 | -d: run your program under the control of pdb, the Python debugger. | |
1330 | This allows you to execute your program step by step, watch variables, |
|
1330 | This allows you to execute your program step by step, watch variables, | |
1331 | etc. Internally, what IPython does is similar to calling: |
|
1331 | etc. Internally, what IPython does is similar to calling: | |
1332 |
|
1332 | |||
1333 | pdb.run('execfile("YOURFILENAME")') |
|
1333 | pdb.run('execfile("YOURFILENAME")') | |
1334 |
|
1334 | |||
1335 | with a breakpoint set on line 1 of your file. You can change the line |
|
1335 | with a breakpoint set on line 1 of your file. You can change the line | |
1336 | number for this automatic breakpoint to be <N> by using the -bN option |
|
1336 | number for this automatic breakpoint to be <N> by using the -bN option | |
1337 | (where N must be an integer). For example: |
|
1337 | (where N must be an integer). For example: | |
1338 |
|
1338 | |||
1339 | %run -d -b40 myscript |
|
1339 | %run -d -b40 myscript | |
1340 |
|
1340 | |||
1341 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
1341 | will set the first breakpoint at line 40 in myscript.py. Note that | |
1342 | the first breakpoint must be set on a line which actually does |
|
1342 | the first breakpoint must be set on a line which actually does | |
1343 | something (not a comment or docstring) for it to stop execution. |
|
1343 | something (not a comment or docstring) for it to stop execution. | |
1344 |
|
1344 | |||
1345 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
1345 | When the pdb debugger starts, you will see a (Pdb) prompt. You must | |
1346 | first enter 'c' (without qoutes) to start execution up to the first |
|
1346 | first enter 'c' (without qoutes) to start execution up to the first | |
1347 | breakpoint. |
|
1347 | breakpoint. | |
1348 |
|
1348 | |||
1349 | Entering 'help' gives information about the use of the debugger. You |
|
1349 | Entering 'help' gives information about the use of the debugger. You | |
1350 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
1350 | can easily see pdb's full documentation with "import pdb;pdb.help()" | |
1351 | at a prompt. |
|
1351 | at a prompt. | |
1352 |
|
1352 | |||
1353 | -p: run program under the control of the Python profiler module (which |
|
1353 | -p: run program under the control of the Python profiler module (which | |
1354 | prints a detailed report of execution times, function calls, etc). |
|
1354 | prints a detailed report of execution times, function calls, etc). | |
1355 |
|
1355 | |||
1356 | You can pass other options after -p which affect the behavior of the |
|
1356 | You can pass other options after -p which affect the behavior of the | |
1357 | profiler itself. See the docs for %prun for details. |
|
1357 | profiler itself. See the docs for %prun for details. | |
1358 |
|
1358 | |||
1359 | In this mode, the program's variables do NOT propagate back to the |
|
1359 | In this mode, the program's variables do NOT propagate back to the | |
1360 | IPython interactive namespace (because they remain in the namespace |
|
1360 | IPython interactive namespace (because they remain in the namespace | |
1361 | where the profiler executes them). |
|
1361 | where the profiler executes them). | |
1362 |
|
1362 | |||
1363 | Internally this triggers a call to %prun, see its documentation for |
|
1363 | Internally this triggers a call to %prun, see its documentation for | |
1364 | details on the options available specifically for profiling.""" |
|
1364 | details on the options available specifically for profiling.""" | |
1365 |
|
1365 | |||
1366 | # get arguments and set sys.argv for program to be run. |
|
1366 | # get arguments and set sys.argv for program to be run. | |
1367 | opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e', |
|
1367 | opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e', | |
1368 | mode='list',list_all=1) |
|
1368 | mode='list',list_all=1) | |
1369 |
|
1369 | |||
1370 | try: |
|
1370 | try: | |
1371 | filename = get_py_filename(arg_lst[0]) |
|
1371 | filename = get_py_filename(arg_lst[0]) | |
1372 | except IndexError: |
|
1372 | except IndexError: | |
1373 | warn('you must provide at least a filename.') |
|
1373 | warn('you must provide at least a filename.') | |
1374 | print '\n%run:\n',OInspect.getdoc(self.magic_run) |
|
1374 | print '\n%run:\n',OInspect.getdoc(self.magic_run) | |
1375 | return |
|
1375 | return | |
1376 | except IOError,msg: |
|
1376 | except IOError,msg: | |
1377 | error(msg) |
|
1377 | error(msg) | |
1378 | return |
|
1378 | return | |
1379 |
|
1379 | |||
1380 | # Control the response to exit() calls made by the script being run |
|
1380 | # Control the response to exit() calls made by the script being run | |
1381 | exit_ignore = opts.has_key('e') |
|
1381 | exit_ignore = opts.has_key('e') | |
1382 |
|
1382 | |||
1383 | # Make sure that the running script gets a proper sys.argv as if it |
|
1383 | # Make sure that the running script gets a proper sys.argv as if it | |
1384 | # were run from a system shell. |
|
1384 | # were run from a system shell. | |
1385 | save_argv = sys.argv # save it for later restoring |
|
1385 | save_argv = sys.argv # save it for later restoring | |
1386 | sys.argv = [filename]+ arg_lst[1:] # put in the proper filename |
|
1386 | sys.argv = [filename]+ arg_lst[1:] # put in the proper filename | |
1387 |
|
1387 | |||
1388 | if opts.has_key('i'): |
|
1388 | if opts.has_key('i'): | |
1389 | prog_ns = self.shell.user_ns |
|
1389 | prog_ns = self.shell.user_ns | |
1390 | __name__save = self.shell.user_ns['__name__'] |
|
1390 | __name__save = self.shell.user_ns['__name__'] | |
1391 | prog_ns['__name__'] = '__main__' |
|
1391 | prog_ns['__name__'] = '__main__' | |
1392 | else: |
|
1392 | else: | |
1393 | if opts.has_key('n'): |
|
1393 | if opts.has_key('n'): | |
1394 | name = os.path.splitext(os.path.basename(filename))[0] |
|
1394 | name = os.path.splitext(os.path.basename(filename))[0] | |
1395 | else: |
|
1395 | else: | |
1396 | name = '__main__' |
|
1396 | name = '__main__' | |
1397 | prog_ns = {'__name__':name} |
|
1397 | prog_ns = {'__name__':name} | |
1398 |
|
1398 | |||
1399 | # pickle fix. See iplib for an explanation. But we need to make sure |
|
1399 | # pickle fix. See iplib for an explanation. But we need to make sure | |
1400 | # that, if we overwrite __main__, we replace it at the end |
|
1400 | # that, if we overwrite __main__, we replace it at the end | |
1401 | if prog_ns['__name__'] == '__main__': |
|
1401 | if prog_ns['__name__'] == '__main__': | |
1402 | restore_main = sys.modules['__main__'] |
|
1402 | restore_main = sys.modules['__main__'] | |
1403 | else: |
|
1403 | else: | |
1404 | restore_main = False |
|
1404 | restore_main = False | |
1405 |
|
1405 | |||
1406 | sys.modules[prog_ns['__name__']] = FakeModule(prog_ns) |
|
1406 | sys.modules[prog_ns['__name__']] = FakeModule(prog_ns) | |
1407 |
|
1407 | |||
1408 | stats = None |
|
1408 | stats = None | |
1409 | try: |
|
1409 | try: | |
1410 | if opts.has_key('p'): |
|
1410 | if opts.has_key('p'): | |
1411 | stats = self.magic_prun('',0,opts,arg_lst,prog_ns) |
|
1411 | stats = self.magic_prun('',0,opts,arg_lst,prog_ns) | |
1412 | else: |
|
1412 | else: | |
1413 | if opts.has_key('d'): |
|
1413 | if opts.has_key('d'): | |
1414 | deb = Debugger.Pdb(self.shell.rc.colors) |
|
1414 | deb = Debugger.Pdb(self.shell.rc.colors) | |
1415 | # reset Breakpoint state, which is moronically kept |
|
1415 | # reset Breakpoint state, which is moronically kept | |
1416 | # in a class |
|
1416 | # in a class | |
1417 | bdb.Breakpoint.next = 1 |
|
1417 | bdb.Breakpoint.next = 1 | |
1418 | bdb.Breakpoint.bplist = {} |
|
1418 | bdb.Breakpoint.bplist = {} | |
1419 | bdb.Breakpoint.bpbynumber = [None] |
|
1419 | bdb.Breakpoint.bpbynumber = [None] | |
1420 | # Set an initial breakpoint to stop execution |
|
1420 | # Set an initial breakpoint to stop execution | |
1421 | maxtries = 10 |
|
1421 | maxtries = 10 | |
1422 | bp = int(opts.get('b',[1])[0]) |
|
1422 | bp = int(opts.get('b',[1])[0]) | |
1423 | checkline = deb.checkline(filename,bp) |
|
1423 | checkline = deb.checkline(filename,bp) | |
1424 | if not checkline: |
|
1424 | if not checkline: | |
1425 | for bp in range(bp+1,bp+maxtries+1): |
|
1425 | for bp in range(bp+1,bp+maxtries+1): | |
1426 | if deb.checkline(filename,bp): |
|
1426 | if deb.checkline(filename,bp): | |
1427 | break |
|
1427 | break | |
1428 | else: |
|
1428 | else: | |
1429 | msg = ("\nI failed to find a valid line to set " |
|
1429 | msg = ("\nI failed to find a valid line to set " | |
1430 | "a breakpoint\n" |
|
1430 | "a breakpoint\n" | |
1431 | "after trying up to line: %s.\n" |
|
1431 | "after trying up to line: %s.\n" | |
1432 | "Please set a valid breakpoint manually " |
|
1432 | "Please set a valid breakpoint manually " | |
1433 | "with the -b option." % bp) |
|
1433 | "with the -b option." % bp) | |
1434 | error(msg) |
|
1434 | error(msg) | |
1435 | return |
|
1435 | return | |
1436 | # if we find a good linenumber, set the breakpoint |
|
1436 | # if we find a good linenumber, set the breakpoint | |
1437 | deb.do_break('%s:%s' % (filename,bp)) |
|
1437 | deb.do_break('%s:%s' % (filename,bp)) | |
1438 | # Start file run |
|
1438 | # Start file run | |
1439 | print "NOTE: Enter 'c' at the", |
|
1439 | print "NOTE: Enter 'c' at the", | |
1440 | print "ipdb> prompt to start your script." |
|
1440 | print "ipdb> prompt to start your script." | |
1441 | try: |
|
1441 | try: | |
1442 | deb.run('execfile("%s")' % filename,prog_ns) |
|
1442 | deb.run('execfile("%s")' % filename,prog_ns) | |
1443 | except: |
|
1443 | except: | |
1444 | etype, value, tb = sys.exc_info() |
|
1444 | etype, value, tb = sys.exc_info() | |
1445 | # Skip three frames in the traceback: the %run one, |
|
1445 | # Skip three frames in the traceback: the %run one, | |
1446 | # one inside bdb.py, and the command-line typed by the |
|
1446 | # one inside bdb.py, and the command-line typed by the | |
1447 | # user (run by exec in pdb itself). |
|
1447 | # user (run by exec in pdb itself). | |
1448 | self.shell.InteractiveTB(etype,value,tb,tb_offset=3) |
|
1448 | self.shell.InteractiveTB(etype,value,tb,tb_offset=3) | |
1449 | else: |
|
1449 | else: | |
1450 | if runner is None: |
|
1450 | if runner is None: | |
1451 | runner = self.shell.safe_execfile |
|
1451 | runner = self.shell.safe_execfile | |
1452 | if opts.has_key('t'): |
|
1452 | if opts.has_key('t'): | |
1453 | try: |
|
1453 | try: | |
1454 | nruns = int(opts['N'][0]) |
|
1454 | nruns = int(opts['N'][0]) | |
1455 | if nruns < 1: |
|
1455 | if nruns < 1: | |
1456 | error('Number of runs must be >=1') |
|
1456 | error('Number of runs must be >=1') | |
1457 | return |
|
1457 | return | |
1458 | except (KeyError): |
|
1458 | except (KeyError): | |
1459 | nruns = 1 |
|
1459 | nruns = 1 | |
1460 | if nruns == 1: |
|
1460 | if nruns == 1: | |
1461 | t0 = clock2() |
|
1461 | t0 = clock2() | |
1462 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1462 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) | |
1463 | t1 = clock2() |
|
1463 | t1 = clock2() | |
1464 | t_usr = t1[0]-t0[0] |
|
1464 | t_usr = t1[0]-t0[0] | |
1465 | t_sys = t1[1]-t1[1] |
|
1465 | t_sys = t1[1]-t1[1] | |
1466 | print "\nIPython CPU timings (estimated):" |
|
1466 | print "\nIPython CPU timings (estimated):" | |
1467 | print " User : %10s s." % t_usr |
|
1467 | print " User : %10s s." % t_usr | |
1468 | print " System: %10s s." % t_sys |
|
1468 | print " System: %10s s." % t_sys | |
1469 | else: |
|
1469 | else: | |
1470 | runs = range(nruns) |
|
1470 | runs = range(nruns) | |
1471 | t0 = clock2() |
|
1471 | t0 = clock2() | |
1472 | for nr in runs: |
|
1472 | for nr in runs: | |
1473 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1473 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) | |
1474 | t1 = clock2() |
|
1474 | t1 = clock2() | |
1475 | t_usr = t1[0]-t0[0] |
|
1475 | t_usr = t1[0]-t0[0] | |
1476 | t_sys = t1[1]-t1[1] |
|
1476 | t_sys = t1[1]-t1[1] | |
1477 | print "\nIPython CPU timings (estimated):" |
|
1477 | print "\nIPython CPU timings (estimated):" | |
1478 | print "Total runs performed:",nruns |
|
1478 | print "Total runs performed:",nruns | |
1479 | print " Times : %10s %10s" % ('Total','Per run') |
|
1479 | print " Times : %10s %10s" % ('Total','Per run') | |
1480 | print " User : %10s s, %10s s." % (t_usr,t_usr/nruns) |
|
1480 | print " User : %10s s, %10s s." % (t_usr,t_usr/nruns) | |
1481 | print " System: %10s s, %10s s." % (t_sys,t_sys/nruns) |
|
1481 | print " System: %10s s, %10s s." % (t_sys,t_sys/nruns) | |
1482 |
|
1482 | |||
1483 | else: |
|
1483 | else: | |
1484 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1484 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) | |
1485 | if opts.has_key('i'): |
|
1485 | if opts.has_key('i'): | |
1486 | self.shell.user_ns['__name__'] = __name__save |
|
1486 | self.shell.user_ns['__name__'] = __name__save | |
1487 | else: |
|
1487 | else: | |
1488 | # update IPython interactive namespace |
|
1488 | # update IPython interactive namespace | |
1489 | del prog_ns['__name__'] |
|
1489 | del prog_ns['__name__'] | |
1490 | self.shell.user_ns.update(prog_ns) |
|
1490 | self.shell.user_ns.update(prog_ns) | |
1491 | finally: |
|
1491 | finally: | |
1492 | sys.argv = save_argv |
|
1492 | sys.argv = save_argv | |
1493 | if restore_main: |
|
1493 | if restore_main: | |
1494 | sys.modules['__main__'] = restore_main |
|
1494 | sys.modules['__main__'] = restore_main | |
1495 | return stats |
|
1495 | return stats | |
1496 |
|
1496 | |||
1497 | def magic_runlog(self, parameter_s =''): |
|
1497 | def magic_runlog(self, parameter_s =''): | |
1498 | """Run files as logs. |
|
1498 | """Run files as logs. | |
1499 |
|
1499 | |||
1500 | Usage:\\ |
|
1500 | Usage:\\ | |
1501 | %runlog file1 file2 ... |
|
1501 | %runlog file1 file2 ... | |
1502 |
|
1502 | |||
1503 | Run the named files (treating them as log files) in sequence inside |
|
1503 | Run the named files (treating them as log files) in sequence inside | |
1504 | the interpreter, and return to the prompt. This is much slower than |
|
1504 | the interpreter, and return to the prompt. This is much slower than | |
1505 | %run because each line is executed in a try/except block, but it |
|
1505 | %run because each line is executed in a try/except block, but it | |
1506 | allows running files with syntax errors in them. |
|
1506 | allows running files with syntax errors in them. | |
1507 |
|
1507 | |||
1508 | Normally IPython will guess when a file is one of its own logfiles, so |
|
1508 | Normally IPython will guess when a file is one of its own logfiles, so | |
1509 | you can typically use %run even for logs. This shorthand allows you to |
|
1509 | you can typically use %run even for logs. This shorthand allows you to | |
1510 | force any file to be treated as a log file.""" |
|
1510 | force any file to be treated as a log file.""" | |
1511 |
|
1511 | |||
1512 | for f in parameter_s.split(): |
|
1512 | for f in parameter_s.split(): | |
1513 | self.shell.safe_execfile(f,self.shell.user_ns, |
|
1513 | self.shell.safe_execfile(f,self.shell.user_ns, | |
1514 | self.shell.user_ns,islog=1) |
|
1514 | self.shell.user_ns,islog=1) | |
1515 |
|
1515 | |||
1516 | def magic_time(self,parameter_s = ''): |
|
1516 | def magic_time(self,parameter_s = ''): | |
1517 | """Time execution of a Python statement or expression. |
|
1517 | """Time execution of a Python statement or expression. | |
1518 |
|
1518 | |||
1519 | The CPU and wall clock times are printed, and the value of the |
|
1519 | The CPU and wall clock times are printed, and the value of the | |
1520 | expression (if any) is returned. Note that under Win32, system time |
|
1520 | expression (if any) is returned. Note that under Win32, system time | |
1521 | is always reported as 0, since it can not be measured. |
|
1521 | is always reported as 0, since it can not be measured. | |
1522 |
|
1522 | |||
1523 | This function provides very basic timing functionality. In Python |
|
1523 | This function provides very basic timing functionality. In Python | |
1524 | 2.3, the timeit module offers more control and sophistication, but for |
|
1524 | 2.3, the timeit module offers more control and sophistication, but for | |
1525 | now IPython supports Python 2.2, so we can not rely on timeit being |
|
1525 | now IPython supports Python 2.2, so we can not rely on timeit being | |
1526 | present. |
|
1526 | present. | |
1527 |
|
1527 | |||
1528 | Some examples: |
|
1528 | Some examples: | |
1529 |
|
1529 | |||
1530 | In [1]: time 2**128 |
|
1530 | In [1]: time 2**128 | |
1531 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1531 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1532 | Wall time: 0.00 |
|
1532 | Wall time: 0.00 | |
1533 | Out[1]: 340282366920938463463374607431768211456L |
|
1533 | Out[1]: 340282366920938463463374607431768211456L | |
1534 |
|
1534 | |||
1535 | In [2]: n = 1000000 |
|
1535 | In [2]: n = 1000000 | |
1536 |
|
1536 | |||
1537 | In [3]: time sum(range(n)) |
|
1537 | In [3]: time sum(range(n)) | |
1538 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
1538 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s | |
1539 | Wall time: 1.37 |
|
1539 | Wall time: 1.37 | |
1540 | Out[3]: 499999500000L |
|
1540 | Out[3]: 499999500000L | |
1541 |
|
1541 | |||
1542 | In [4]: time print 'hello world' |
|
1542 | In [4]: time print 'hello world' | |
1543 | hello world |
|
1543 | hello world | |
1544 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1544 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1545 | Wall time: 0.00 |
|
1545 | Wall time: 0.00 | |
1546 | """ |
|
1546 | """ | |
1547 |
|
1547 | |||
1548 | # fail immediately if the given expression can't be compiled |
|
1548 | # fail immediately if the given expression can't be compiled | |
1549 | try: |
|
1549 | try: | |
1550 | mode = 'eval' |
|
1550 | mode = 'eval' | |
1551 | code = compile(parameter_s,'<timed eval>',mode) |
|
1551 | code = compile(parameter_s,'<timed eval>',mode) | |
1552 | except SyntaxError: |
|
1552 | except SyntaxError: | |
1553 | mode = 'exec' |
|
1553 | mode = 'exec' | |
1554 | code = compile(parameter_s,'<timed exec>',mode) |
|
1554 | code = compile(parameter_s,'<timed exec>',mode) | |
1555 | # skew measurement as little as possible |
|
1555 | # skew measurement as little as possible | |
1556 | glob = self.shell.user_ns |
|
1556 | glob = self.shell.user_ns | |
1557 | clk = clock2 |
|
1557 | clk = clock2 | |
1558 | wtime = time.time |
|
1558 | wtime = time.time | |
1559 | # time execution |
|
1559 | # time execution | |
1560 | wall_st = wtime() |
|
1560 | wall_st = wtime() | |
1561 | if mode=='eval': |
|
1561 | if mode=='eval': | |
1562 | st = clk() |
|
1562 | st = clk() | |
1563 | out = eval(code,glob) |
|
1563 | out = eval(code,glob) | |
1564 | end = clk() |
|
1564 | end = clk() | |
1565 | else: |
|
1565 | else: | |
1566 | st = clk() |
|
1566 | st = clk() | |
1567 | exec code in glob |
|
1567 | exec code in glob | |
1568 | end = clk() |
|
1568 | end = clk() | |
1569 | out = None |
|
1569 | out = None | |
1570 | wall_end = wtime() |
|
1570 | wall_end = wtime() | |
1571 | # Compute actual times and report |
|
1571 | # Compute actual times and report | |
1572 | wall_time = wall_end-wall_st |
|
1572 | wall_time = wall_end-wall_st | |
1573 | cpu_user = end[0]-st[0] |
|
1573 | cpu_user = end[0]-st[0] | |
1574 | cpu_sys = end[1]-st[1] |
|
1574 | cpu_sys = end[1]-st[1] | |
1575 | cpu_tot = cpu_user+cpu_sys |
|
1575 | cpu_tot = cpu_user+cpu_sys | |
1576 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ |
|
1576 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ | |
1577 | (cpu_user,cpu_sys,cpu_tot) |
|
1577 | (cpu_user,cpu_sys,cpu_tot) | |
1578 | print "Wall time: %.2f" % wall_time |
|
1578 | print "Wall time: %.2f" % wall_time | |
1579 | return out |
|
1579 | return out | |
1580 |
|
1580 | |||
1581 | def magic_macro(self,parameter_s = ''): |
|
1581 | def magic_macro(self,parameter_s = ''): | |
1582 | """Define a set of input lines as a macro for future re-execution. |
|
1582 | """Define a set of input lines as a macro for future re-execution. | |
1583 |
|
1583 | |||
1584 | Usage:\\ |
|
1584 | Usage:\\ | |
1585 | %macro name n1-n2 n3-n4 ... n5 .. n6 ... |
|
1585 | %macro name n1-n2 n3-n4 ... n5 .. n6 ... | |
1586 |
|
1586 | |||
1587 | This will define a global variable called `name` which is a string |
|
1587 | This will define a global variable called `name` which is a string | |
1588 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1588 | made of joining the slices and lines you specify (n1,n2,... numbers | |
1589 | above) from your input history into a single string. This variable |
|
1589 | above) from your input history into a single string. This variable | |
1590 | acts like an automatic function which re-executes those lines as if |
|
1590 | acts like an automatic function which re-executes those lines as if | |
1591 | you had typed them. You just type 'name' at the prompt and the code |
|
1591 | you had typed them. You just type 'name' at the prompt and the code | |
1592 | executes. |
|
1592 | executes. | |
1593 |
|
1593 | |||
1594 | The notation for indicating number ranges is: n1-n2 means 'use line |
|
1594 | The notation for indicating number ranges is: n1-n2 means 'use line | |
1595 | numbers n1,...n2' (the endpoint is included). That is, '5-7' means |
|
1595 | numbers n1,...n2' (the endpoint is included). That is, '5-7' means | |
1596 | using the lines numbered 5,6 and 7. |
|
1596 | using the lines numbered 5,6 and 7. | |
1597 |
|
1597 | |||
1598 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
1598 | Note: as a 'hidden' feature, you can also use traditional python slice | |
1599 | notation, where N:M means numbers N through M-1. |
|
1599 | notation, where N:M means numbers N through M-1. | |
1600 |
|
1600 | |||
1601 | For example, if your history contains (%hist prints it): |
|
1601 | For example, if your history contains (%hist prints it): | |
1602 |
|
1602 | |||
1603 | 44: x=1\\ |
|
1603 | 44: x=1\\ | |
1604 | 45: y=3\\ |
|
1604 | 45: y=3\\ | |
1605 | 46: z=x+y\\ |
|
1605 | 46: z=x+y\\ | |
1606 | 47: print x\\ |
|
1606 | 47: print x\\ | |
1607 | 48: a=5\\ |
|
1607 | 48: a=5\\ | |
1608 | 49: print 'x',x,'y',y\\ |
|
1608 | 49: print 'x',x,'y',y\\ | |
1609 |
|
1609 | |||
1610 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1610 | you can create a macro with lines 44 through 47 (included) and line 49 | |
1611 | called my_macro with: |
|
1611 | called my_macro with: | |
1612 |
|
1612 | |||
1613 | In [51]: %macro my_macro 44-47 49 |
|
1613 | In [51]: %macro my_macro 44-47 49 | |
1614 |
|
1614 | |||
1615 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1615 | Now, typing `my_macro` (without quotes) will re-execute all this code | |
1616 | in one pass. |
|
1616 | in one pass. | |
1617 |
|
1617 | |||
1618 | You don't need to give the line-numbers in order, and any given line |
|
1618 | You don't need to give the line-numbers in order, and any given line | |
1619 | number can appear multiple times. You can assemble macros with any |
|
1619 | number can appear multiple times. You can assemble macros with any | |
1620 | lines from your input history in any order. |
|
1620 | lines from your input history in any order. | |
1621 |
|
1621 | |||
1622 | The macro is a simple object which holds its value in an attribute, |
|
1622 | The macro is a simple object which holds its value in an attribute, | |
1623 | but IPython's display system checks for macros and executes them as |
|
1623 | but IPython's display system checks for macros and executes them as | |
1624 | code instead of printing them when you type their name. |
|
1624 | code instead of printing them when you type their name. | |
1625 |
|
1625 | |||
1626 | You can view a macro's contents by explicitly printing it with: |
|
1626 | You can view a macro's contents by explicitly printing it with: | |
1627 |
|
1627 | |||
1628 | 'print macro_name'. |
|
1628 | 'print macro_name'. | |
1629 |
|
1629 | |||
1630 | For one-off cases which DON'T contain magic function calls in them you |
|
1630 | For one-off cases which DON'T contain magic function calls in them you | |
1631 | can obtain similar results by explicitly executing slices from your |
|
1631 | can obtain similar results by explicitly executing slices from your | |
1632 | input history with: |
|
1632 | input history with: | |
1633 |
|
1633 | |||
1634 | In [60]: exec In[44:48]+In[49]""" |
|
1634 | In [60]: exec In[44:48]+In[49]""" | |
1635 |
|
1635 | |||
1636 | args = parameter_s.split() |
|
1636 | args = parameter_s.split() | |
1637 | name,ranges = args[0], args[1:] |
|
1637 | name,ranges = args[0], args[1:] | |
1638 | #print 'rng',ranges # dbg |
|
1638 | #print 'rng',ranges # dbg | |
1639 | lines = self.extract_input_slices(ranges) |
|
1639 | lines = self.extract_input_slices(ranges) | |
1640 | macro = Macro(lines) |
|
1640 | macro = Macro(lines) | |
1641 | self.shell.user_ns.update({name:macro}) |
|
1641 | self.shell.user_ns.update({name:macro}) | |
1642 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
1642 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name | |
1643 | print 'Macro contents:' |
|
1643 | print 'Macro contents:' | |
1644 | print macro, |
|
1644 | print macro, | |
1645 |
|
1645 | |||
1646 | def magic_save(self,parameter_s = ''): |
|
1646 | def magic_save(self,parameter_s = ''): | |
1647 | """Save a set of lines to a given filename. |
|
1647 | """Save a set of lines to a given filename. | |
1648 |
|
1648 | |||
1649 | Usage:\\ |
|
1649 | Usage:\\ | |
1650 | %save filename n1-n2 n3-n4 ... n5 .. n6 ... |
|
1650 | %save filename n1-n2 n3-n4 ... n5 .. n6 ... | |
1651 |
|
1651 | |||
1652 | This function uses the same syntax as %macro for line extraction, but |
|
1652 | This function uses the same syntax as %macro for line extraction, but | |
1653 | instead of creating a macro it saves the resulting string to the |
|
1653 | instead of creating a macro it saves the resulting string to the | |
1654 | filename you specify. |
|
1654 | filename you specify. | |
1655 |
|
1655 | |||
1656 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
1656 | It adds a '.py' extension to the file if you don't do so yourself, and | |
1657 | it asks for confirmation before overwriting existing files.""" |
|
1657 | it asks for confirmation before overwriting existing files.""" | |
1658 |
|
1658 | |||
1659 | args = parameter_s.split() |
|
1659 | args = parameter_s.split() | |
1660 | fname,ranges = args[0], args[1:] |
|
1660 | fname,ranges = args[0], args[1:] | |
1661 | if not fname.endswith('.py'): |
|
1661 | if not fname.endswith('.py'): | |
1662 | fname += '.py' |
|
1662 | fname += '.py' | |
1663 | if os.path.isfile(fname): |
|
1663 | if os.path.isfile(fname): | |
1664 | ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) |
|
1664 | ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) | |
1665 | if ans.lower() not in ['y','yes']: |
|
1665 | if ans.lower() not in ['y','yes']: | |
1666 | print 'Operation cancelled.' |
|
1666 | print 'Operation cancelled.' | |
1667 | return |
|
1667 | return | |
1668 | cmds = ''.join(self.extract_input_slices(ranges)) |
|
1668 | cmds = ''.join(self.extract_input_slices(ranges)) | |
1669 | f = file(fname,'w') |
|
1669 | f = file(fname,'w') | |
1670 | f.write(cmds) |
|
1670 | f.write(cmds) | |
1671 | f.close() |
|
1671 | f.close() | |
1672 | print 'The following commands were written to file `%s`:' % fname |
|
1672 | print 'The following commands were written to file `%s`:' % fname | |
1673 | print cmds |
|
1673 | print cmds | |
1674 |
|
1674 | |||
1675 | def _edit_macro(self,mname,macro): |
|
1675 | def _edit_macro(self,mname,macro): | |
1676 | """open an editor with the macro data in a file""" |
|
1676 | """open an editor with the macro data in a file""" | |
1677 | filename = self.shell.mktempfile(macro.value) |
|
1677 | filename = self.shell.mktempfile(macro.value) | |
1678 | self.shell.hooks.editor(filename) |
|
1678 | self.shell.hooks.editor(filename) | |
1679 |
|
1679 | |||
1680 | # and make a new macro object, to replace the old one |
|
1680 | # and make a new macro object, to replace the old one | |
1681 | mfile = open(filename) |
|
1681 | mfile = open(filename) | |
1682 | mvalue = mfile.read() |
|
1682 | mvalue = mfile.read() | |
1683 | mfile.close() |
|
1683 | mfile.close() | |
1684 | self.shell.user_ns[mname] = Macro(mvalue) |
|
1684 | self.shell.user_ns[mname] = Macro(mvalue) | |
1685 |
|
1685 | |||
1686 | def magic_ed(self,parameter_s=''): |
|
1686 | def magic_ed(self,parameter_s=''): | |
1687 | """Alias to %edit.""" |
|
1687 | """Alias to %edit.""" | |
1688 | return self.magic_edit(parameter_s) |
|
1688 | return self.magic_edit(parameter_s) | |
1689 |
|
1689 | |||
1690 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
1690 | def magic_edit(self,parameter_s='',last_call=['','']): | |
1691 | """Bring up an editor and execute the resulting code. |
|
1691 | """Bring up an editor and execute the resulting code. | |
1692 |
|
1692 | |||
1693 | Usage: |
|
1693 | Usage: | |
1694 | %edit [options] [args] |
|
1694 | %edit [options] [args] | |
1695 |
|
1695 | |||
1696 | %edit runs IPython's editor hook. The default version of this hook is |
|
1696 | %edit runs IPython's editor hook. The default version of this hook is | |
1697 | set to call the __IPYTHON__.rc.editor command. This is read from your |
|
1697 | set to call the __IPYTHON__.rc.editor command. This is read from your | |
1698 | environment variable $EDITOR. If this isn't found, it will default to |
|
1698 | environment variable $EDITOR. If this isn't found, it will default to | |
1699 | vi under Linux/Unix and to notepad under Windows. See the end of this |
|
1699 | vi under Linux/Unix and to notepad under Windows. See the end of this | |
1700 | docstring for how to change the editor hook. |
|
1700 | docstring for how to change the editor hook. | |
1701 |
|
1701 | |||
1702 | You can also set the value of this editor via the command line option |
|
1702 | You can also set the value of this editor via the command line option | |
1703 | '-editor' or in your ipythonrc file. This is useful if you wish to use |
|
1703 | '-editor' or in your ipythonrc file. This is useful if you wish to use | |
1704 | specifically for IPython an editor different from your typical default |
|
1704 | specifically for IPython an editor different from your typical default | |
1705 | (and for Windows users who typically don't set environment variables). |
|
1705 | (and for Windows users who typically don't set environment variables). | |
1706 |
|
1706 | |||
1707 | This command allows you to conveniently edit multi-line code right in |
|
1707 | This command allows you to conveniently edit multi-line code right in | |
1708 | your IPython session. |
|
1708 | your IPython session. | |
1709 |
|
1709 | |||
1710 | If called without arguments, %edit opens up an empty editor with a |
|
1710 | If called without arguments, %edit opens up an empty editor with a | |
1711 | temporary file and will execute the contents of this file when you |
|
1711 | temporary file and will execute the contents of this file when you | |
1712 | close it (don't forget to save it!). |
|
1712 | close it (don't forget to save it!). | |
1713 |
|
1713 | |||
1714 |
|
1714 | |||
1715 | Options: |
|
1715 | Options: | |
1716 |
|
1716 | |||
1717 | -p: this will call the editor with the same data as the previous time |
|
1717 | -p: this will call the editor with the same data as the previous time | |
1718 | it was used, regardless of how long ago (in your current session) it |
|
1718 | it was used, regardless of how long ago (in your current session) it | |
1719 | was. |
|
1719 | was. | |
1720 |
|
1720 | |||
1721 | -x: do not execute the edited code immediately upon exit. This is |
|
1721 | -x: do not execute the edited code immediately upon exit. This is | |
1722 | mainly useful if you are editing programs which need to be called with |
|
1722 | mainly useful if you are editing programs which need to be called with | |
1723 | command line arguments, which you can then do using %run. |
|
1723 | command line arguments, which you can then do using %run. | |
1724 |
|
1724 | |||
1725 |
|
1725 | |||
1726 | Arguments: |
|
1726 | Arguments: | |
1727 |
|
1727 | |||
1728 | If arguments are given, the following possibilites exist: |
|
1728 | If arguments are given, the following possibilites exist: | |
1729 |
|
1729 | |||
1730 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
1730 | - The arguments are numbers or pairs of colon-separated numbers (like | |
1731 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
1731 | 1 4:8 9). These are interpreted as lines of previous input to be | |
1732 | loaded into the editor. The syntax is the same of the %macro command. |
|
1732 | loaded into the editor. The syntax is the same of the %macro command. | |
1733 |
|
1733 | |||
1734 | - If the argument doesn't start with a number, it is evaluated as a |
|
1734 | - If the argument doesn't start with a number, it is evaluated as a | |
1735 | variable and its contents loaded into the editor. You can thus edit |
|
1735 | variable and its contents loaded into the editor. You can thus edit | |
1736 | any string which contains python code (including the result of |
|
1736 | any string which contains python code (including the result of | |
1737 | previous edits). |
|
1737 | previous edits). | |
1738 |
|
1738 | |||
1739 | - If the argument is the name of an object (other than a string), |
|
1739 | - If the argument is the name of an object (other than a string), | |
1740 | IPython will try to locate the file where it was defined and open the |
|
1740 | IPython will try to locate the file where it was defined and open the | |
1741 | editor at the point where it is defined. You can use `%edit function` |
|
1741 | editor at the point where it is defined. You can use `%edit function` | |
1742 | to load an editor exactly at the point where 'function' is defined, |
|
1742 | to load an editor exactly at the point where 'function' is defined, | |
1743 | edit it and have the file be executed automatically. |
|
1743 | edit it and have the file be executed automatically. | |
1744 |
|
1744 | |||
1745 | If the object is a macro (see %macro for details), this opens up your |
|
1745 | If the object is a macro (see %macro for details), this opens up your | |
1746 | specified editor with a temporary file containing the macro's data. |
|
1746 | specified editor with a temporary file containing the macro's data. | |
1747 | Upon exit, the macro is reloaded with the contents of the file. |
|
1747 | Upon exit, the macro is reloaded with the contents of the file. | |
1748 |
|
1748 | |||
1749 | Note: opening at an exact line is only supported under Unix, and some |
|
1749 | Note: opening at an exact line is only supported under Unix, and some | |
1750 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
1750 | editors (like kedit and gedit up to Gnome 2.8) do not understand the | |
1751 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
1751 | '+NUMBER' parameter necessary for this feature. Good editors like | |
1752 | (X)Emacs, vi, jed, pico and joe all do. |
|
1752 | (X)Emacs, vi, jed, pico and joe all do. | |
1753 |
|
1753 | |||
1754 | - If the argument is not found as a variable, IPython will look for a |
|
1754 | - If the argument is not found as a variable, IPython will look for a | |
1755 | file with that name (adding .py if necessary) and load it into the |
|
1755 | file with that name (adding .py if necessary) and load it into the | |
1756 | editor. It will execute its contents with execfile() when you exit, |
|
1756 | editor. It will execute its contents with execfile() when you exit, | |
1757 | loading any code in the file into your interactive namespace. |
|
1757 | loading any code in the file into your interactive namespace. | |
1758 |
|
1758 | |||
1759 | After executing your code, %edit will return as output the code you |
|
1759 | After executing your code, %edit will return as output the code you | |
1760 | typed in the editor (except when it was an existing file). This way |
|
1760 | typed in the editor (except when it was an existing file). This way | |
1761 | you can reload the code in further invocations of %edit as a variable, |
|
1761 | you can reload the code in further invocations of %edit as a variable, | |
1762 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
1762 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of | |
1763 | the output. |
|
1763 | the output. | |
1764 |
|
1764 | |||
1765 | Note that %edit is also available through the alias %ed. |
|
1765 | Note that %edit is also available through the alias %ed. | |
1766 |
|
1766 | |||
1767 | This is an example of creating a simple function inside the editor and |
|
1767 | This is an example of creating a simple function inside the editor and | |
1768 | then modifying it. First, start up the editor: |
|
1768 | then modifying it. First, start up the editor: | |
1769 |
|
1769 | |||
1770 | In [1]: ed\\ |
|
1770 | In [1]: ed\\ | |
1771 | Editing... done. Executing edited code...\\ |
|
1771 | Editing... done. Executing edited code...\\ | |
1772 | Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n' |
|
1772 | Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n' | |
1773 |
|
1773 | |||
1774 | We can then call the function foo(): |
|
1774 | We can then call the function foo(): | |
1775 |
|
1775 | |||
1776 | In [2]: foo()\\ |
|
1776 | In [2]: foo()\\ | |
1777 | foo() was defined in an editing session |
|
1777 | foo() was defined in an editing session | |
1778 |
|
1778 | |||
1779 | Now we edit foo. IPython automatically loads the editor with the |
|
1779 | Now we edit foo. IPython automatically loads the editor with the | |
1780 | (temporary) file where foo() was previously defined: |
|
1780 | (temporary) file where foo() was previously defined: | |
1781 |
|
1781 | |||
1782 | In [3]: ed foo\\ |
|
1782 | In [3]: ed foo\\ | |
1783 | Editing... done. Executing edited code... |
|
1783 | Editing... done. Executing edited code... | |
1784 |
|
1784 | |||
1785 | And if we call foo() again we get the modified version: |
|
1785 | And if we call foo() again we get the modified version: | |
1786 |
|
1786 | |||
1787 | In [4]: foo()\\ |
|
1787 | In [4]: foo()\\ | |
1788 | foo() has now been changed! |
|
1788 | foo() has now been changed! | |
1789 |
|
1789 | |||
1790 | Here is an example of how to edit a code snippet successive |
|
1790 | Here is an example of how to edit a code snippet successive | |
1791 | times. First we call the editor: |
|
1791 | times. First we call the editor: | |
1792 |
|
1792 | |||
1793 | In [8]: ed\\ |
|
1793 | In [8]: ed\\ | |
1794 | Editing... done. Executing edited code...\\ |
|
1794 | Editing... done. Executing edited code...\\ | |
1795 | hello\\ |
|
1795 | hello\\ | |
1796 | Out[8]: "print 'hello'\\n" |
|
1796 | Out[8]: "print 'hello'\\n" | |
1797 |
|
1797 | |||
1798 | Now we call it again with the previous output (stored in _): |
|
1798 | Now we call it again with the previous output (stored in _): | |
1799 |
|
1799 | |||
1800 | In [9]: ed _\\ |
|
1800 | In [9]: ed _\\ | |
1801 | Editing... done. Executing edited code...\\ |
|
1801 | Editing... done. Executing edited code...\\ | |
1802 | hello world\\ |
|
1802 | hello world\\ | |
1803 | Out[9]: "print 'hello world'\\n" |
|
1803 | Out[9]: "print 'hello world'\\n" | |
1804 |
|
1804 | |||
1805 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
1805 | Now we call it with the output #8 (stored in _8, also as Out[8]): | |
1806 |
|
1806 | |||
1807 | In [10]: ed _8\\ |
|
1807 | In [10]: ed _8\\ | |
1808 | Editing... done. Executing edited code...\\ |
|
1808 | Editing... done. Executing edited code...\\ | |
1809 | hello again\\ |
|
1809 | hello again\\ | |
1810 | Out[10]: "print 'hello again'\\n" |
|
1810 | Out[10]: "print 'hello again'\\n" | |
1811 |
|
1811 | |||
1812 |
|
1812 | |||
1813 | Changing the default editor hook: |
|
1813 | Changing the default editor hook: | |
1814 |
|
1814 | |||
1815 | If you wish to write your own editor hook, you can put it in a |
|
1815 | If you wish to write your own editor hook, you can put it in a | |
1816 | configuration file which you load at startup time. The default hook |
|
1816 | configuration file which you load at startup time. The default hook | |
1817 | is defined in the IPython.hooks module, and you can use that as a |
|
1817 | is defined in the IPython.hooks module, and you can use that as a | |
1818 | starting example for further modifications. That file also has |
|
1818 | starting example for further modifications. That file also has | |
1819 | general instructions on how to set a new hook for use once you've |
|
1819 | general instructions on how to set a new hook for use once you've | |
1820 | defined it.""" |
|
1820 | defined it.""" | |
1821 |
|
1821 | |||
1822 | # FIXME: This function has become a convoluted mess. It needs a |
|
1822 | # FIXME: This function has become a convoluted mess. It needs a | |
1823 | # ground-up rewrite with clean, simple logic. |
|
1823 | # ground-up rewrite with clean, simple logic. | |
1824 |
|
1824 | |||
1825 | def make_filename(arg): |
|
1825 | def make_filename(arg): | |
1826 | "Make a filename from the given args" |
|
1826 | "Make a filename from the given args" | |
1827 | try: |
|
1827 | try: | |
1828 | filename = get_py_filename(arg) |
|
1828 | filename = get_py_filename(arg) | |
1829 | except IOError: |
|
1829 | except IOError: | |
1830 | if args.endswith('.py'): |
|
1830 | if args.endswith('.py'): | |
1831 | filename = arg |
|
1831 | filename = arg | |
1832 | else: |
|
1832 | else: | |
1833 | filename = None |
|
1833 | filename = None | |
1834 | return filename |
|
1834 | return filename | |
1835 |
|
1835 | |||
1836 | # custom exceptions |
|
1836 | # custom exceptions | |
1837 | class DataIsObject(Exception): pass |
|
1837 | class DataIsObject(Exception): pass | |
1838 |
|
1838 | |||
1839 | opts,args = self.parse_options(parameter_s,'px') |
|
1839 | opts,args = self.parse_options(parameter_s,'px') | |
1840 |
|
1840 | |||
1841 | # Default line number value |
|
1841 | # Default line number value | |
1842 | lineno = None |
|
1842 | lineno = None | |
1843 | if opts.has_key('p'): |
|
1843 | if opts.has_key('p'): | |
1844 | args = '_%s' % last_call[0] |
|
1844 | args = '_%s' % last_call[0] | |
1845 | if not self.shell.user_ns.has_key(args): |
|
1845 | if not self.shell.user_ns.has_key(args): | |
1846 | args = last_call[1] |
|
1846 | args = last_call[1] | |
1847 |
|
1847 | |||
1848 | # use last_call to remember the state of the previous call, but don't |
|
1848 | # use last_call to remember the state of the previous call, but don't | |
1849 | # let it be clobbered by successive '-p' calls. |
|
1849 | # let it be clobbered by successive '-p' calls. | |
1850 | try: |
|
1850 | try: | |
1851 | last_call[0] = self.shell.outputcache.prompt_count |
|
1851 | last_call[0] = self.shell.outputcache.prompt_count | |
1852 | if not opts.has_key('p'): |
|
1852 | if not opts.has_key('p'): | |
1853 | last_call[1] = parameter_s |
|
1853 | last_call[1] = parameter_s | |
1854 | except: |
|
1854 | except: | |
1855 | pass |
|
1855 | pass | |
1856 |
|
1856 | |||
1857 | # by default this is done with temp files, except when the given |
|
1857 | # by default this is done with temp files, except when the given | |
1858 | # arg is a filename |
|
1858 | # arg is a filename | |
1859 | use_temp = 1 |
|
1859 | use_temp = 1 | |
1860 |
|
1860 | |||
1861 | if re.match(r'\d',args): |
|
1861 | if re.match(r'\d',args): | |
1862 | # Mode where user specifies ranges of lines, like in %macro. |
|
1862 | # Mode where user specifies ranges of lines, like in %macro. | |
1863 | # This means that you can't edit files whose names begin with |
|
1863 | # This means that you can't edit files whose names begin with | |
1864 | # numbers this way. Tough. |
|
1864 | # numbers this way. Tough. | |
1865 | ranges = args.split() |
|
1865 | ranges = args.split() | |
1866 | data = ''.join(self.extract_input_slices(ranges)) |
|
1866 | data = ''.join(self.extract_input_slices(ranges)) | |
1867 | elif args.endswith('.py'): |
|
1867 | elif args.endswith('.py'): | |
1868 | filename = make_filename(args) |
|
1868 | filename = make_filename(args) | |
1869 | data = '' |
|
1869 | data = '' | |
1870 | use_temp = 0 |
|
1870 | use_temp = 0 | |
1871 | elif args: |
|
1871 | elif args: | |
1872 | try: |
|
1872 | try: | |
1873 | # Load the parameter given as a variable. If not a string, |
|
1873 | # Load the parameter given as a variable. If not a string, | |
1874 | # process it as an object instead (below) |
|
1874 | # process it as an object instead (below) | |
1875 |
|
1875 | |||
1876 | #print '*** args',args,'type',type(args) # dbg |
|
1876 | #print '*** args',args,'type',type(args) # dbg | |
1877 | data = eval(args,self.shell.user_ns) |
|
1877 | data = eval(args,self.shell.user_ns) | |
1878 | if not type(data) in StringTypes: |
|
1878 | if not type(data) in StringTypes: | |
1879 | raise DataIsObject |
|
1879 | raise DataIsObject | |
1880 |
|
1880 | |||
1881 | except (NameError,SyntaxError): |
|
1881 | except (NameError,SyntaxError): | |
1882 | # given argument is not a variable, try as a filename |
|
1882 | # given argument is not a variable, try as a filename | |
1883 | filename = make_filename(args) |
|
1883 | filename = make_filename(args) | |
1884 | if filename is None: |
|
1884 | if filename is None: | |
1885 | warn("Argument given (%s) can't be found as a variable " |
|
1885 | warn("Argument given (%s) can't be found as a variable " | |
1886 | "or as a filename." % args) |
|
1886 | "or as a filename." % args) | |
1887 | return |
|
1887 | return | |
1888 |
|
1888 | |||
1889 | data = '' |
|
1889 | data = '' | |
1890 | use_temp = 0 |
|
1890 | use_temp = 0 | |
1891 | except DataIsObject: |
|
1891 | except DataIsObject: | |
1892 |
|
1892 | |||
1893 | # macros have a special edit function |
|
1893 | # macros have a special edit function | |
1894 | if isinstance(data,Macro): |
|
1894 | if isinstance(data,Macro): | |
1895 | self._edit_macro(args,data) |
|
1895 | self._edit_macro(args,data) | |
1896 | return |
|
1896 | return | |
1897 |
|
1897 | |||
1898 | # For objects, try to edit the file where they are defined |
|
1898 | # For objects, try to edit the file where they are defined | |
1899 | try: |
|
1899 | try: | |
1900 | filename = inspect.getabsfile(data) |
|
1900 | filename = inspect.getabsfile(data) | |
1901 | datafile = 1 |
|
1901 | datafile = 1 | |
1902 | except TypeError: |
|
1902 | except TypeError: | |
1903 | filename = make_filename(args) |
|
1903 | filename = make_filename(args) | |
1904 | datafile = 1 |
|
1904 | datafile = 1 | |
1905 | warn('Could not find file where `%s` is defined.\n' |
|
1905 | warn('Could not find file where `%s` is defined.\n' | |
1906 | 'Opening a file named `%s`' % (args,filename)) |
|
1906 | 'Opening a file named `%s`' % (args,filename)) | |
1907 | # Now, make sure we can actually read the source (if it was in |
|
1907 | # Now, make sure we can actually read the source (if it was in | |
1908 | # a temp file it's gone by now). |
|
1908 | # a temp file it's gone by now). | |
1909 | if datafile: |
|
1909 | if datafile: | |
1910 | try: |
|
1910 | try: | |
1911 | lineno = inspect.getsourcelines(data)[1] |
|
1911 | lineno = inspect.getsourcelines(data)[1] | |
1912 | except IOError: |
|
1912 | except IOError: | |
1913 | filename = make_filename(args) |
|
1913 | filename = make_filename(args) | |
1914 | if filename is None: |
|
1914 | if filename is None: | |
1915 | warn('The file `%s` where `%s` was defined cannot ' |
|
1915 | warn('The file `%s` where `%s` was defined cannot ' | |
1916 | 'be read.' % (filename,data)) |
|
1916 | 'be read.' % (filename,data)) | |
1917 | return |
|
1917 | return | |
1918 | use_temp = 0 |
|
1918 | use_temp = 0 | |
1919 | else: |
|
1919 | else: | |
1920 | data = '' |
|
1920 | data = '' | |
1921 |
|
1921 | |||
1922 | if use_temp: |
|
1922 | if use_temp: | |
1923 | filename = self.shell.mktempfile(data) |
|
1923 | filename = self.shell.mktempfile(data) | |
1924 | print 'IPython will make a temporary file named:',filename |
|
1924 | print 'IPython will make a temporary file named:',filename | |
1925 |
|
1925 | |||
1926 | # do actual editing here |
|
1926 | # do actual editing here | |
1927 | print 'Editing...', |
|
1927 | print 'Editing...', | |
1928 | sys.stdout.flush() |
|
1928 | sys.stdout.flush() | |
1929 | self.shell.hooks.editor(filename,lineno) |
|
1929 | self.shell.hooks.editor(filename,lineno) | |
1930 | if opts.has_key('x'): # -x prevents actual execution |
|
1930 | if opts.has_key('x'): # -x prevents actual execution | |
1931 |
|
1931 | |||
1932 | else: |
|
1932 | else: | |
1933 | print 'done. Executing edited code...' |
|
1933 | print 'done. Executing edited code...' | |
1934 | try: |
|
1934 | try: | |
1935 | self.shell.safe_execfile(filename,self.shell.user_ns) |
|
1935 | self.shell.safe_execfile(filename,self.shell.user_ns) | |
1936 | except IOError,msg: |
|
1936 | except IOError,msg: | |
1937 | if msg.filename == filename: |
|
1937 | if msg.filename == filename: | |
1938 | warn('File not found. Did you forget to save?') |
|
1938 | warn('File not found. Did you forget to save?') | |
1939 | return |
|
1939 | return | |
1940 | else: |
|
1940 | else: | |
1941 | self.shell.showtraceback() |
|
1941 | self.shell.showtraceback() | |
1942 | except: |
|
1942 | except: | |
1943 | self.shell.showtraceback() |
|
1943 | self.shell.showtraceback() | |
1944 |
|
1944 | |||
1945 | def magic_xmode(self,parameter_s = ''): |
|
1945 | def magic_xmode(self,parameter_s = ''): | |
1946 | """Switch modes for the exception handlers. |
|
1946 | """Switch modes for the exception handlers. | |
1947 |
|
1947 | |||
1948 | Valid modes: Plain, Context and Verbose. |
|
1948 | Valid modes: Plain, Context and Verbose. | |
1949 |
|
1949 | |||
1950 | If called without arguments, acts as a toggle.""" |
|
1950 | If called without arguments, acts as a toggle.""" | |
1951 |
|
1951 | |||
1952 | def xmode_switch_err(name): |
|
1952 | def xmode_switch_err(name): | |
1953 | warn('Error changing %s exception modes.\n%s' % |
|
1953 | warn('Error changing %s exception modes.\n%s' % | |
1954 | (name,sys.exc_info()[1])) |
|
1954 | (name,sys.exc_info()[1])) | |
1955 |
|
1955 | |||
1956 | shell = self.shell |
|
1956 | shell = self.shell | |
1957 | new_mode = parameter_s.strip().capitalize() |
|
1957 | new_mode = parameter_s.strip().capitalize() | |
1958 | try: |
|
1958 | try: | |
1959 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
1959 | shell.InteractiveTB.set_mode(mode=new_mode) | |
1960 | print 'Exception reporting mode:',shell.InteractiveTB.mode |
|
1960 | print 'Exception reporting mode:',shell.InteractiveTB.mode | |
1961 | except: |
|
1961 | except: | |
1962 | xmode_switch_err('user') |
|
1962 | xmode_switch_err('user') | |
1963 |
|
1963 | |||
1964 | # threaded shells use a special handler in sys.excepthook |
|
1964 | # threaded shells use a special handler in sys.excepthook | |
1965 | if shell.isthreaded: |
|
1965 | if shell.isthreaded: | |
1966 | try: |
|
1966 | try: | |
1967 | shell.sys_excepthook.set_mode(mode=new_mode) |
|
1967 | shell.sys_excepthook.set_mode(mode=new_mode) | |
1968 | except: |
|
1968 | except: | |
1969 | xmode_switch_err('threaded') |
|
1969 | xmode_switch_err('threaded') | |
1970 |
|
1970 | |||
1971 | def magic_colors(self,parameter_s = ''): |
|
1971 | def magic_colors(self,parameter_s = ''): | |
1972 | """Switch color scheme for prompts, info system and exception handlers. |
|
1972 | """Switch color scheme for prompts, info system and exception handlers. | |
1973 |
|
1973 | |||
1974 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
1974 | Currently implemented schemes: NoColor, Linux, LightBG. | |
1975 |
|
1975 | |||
1976 | Color scheme names are not case-sensitive.""" |
|
1976 | Color scheme names are not case-sensitive.""" | |
1977 |
|
1977 | |||
1978 | def color_switch_err(name): |
|
1978 | def color_switch_err(name): | |
1979 | warn('Error changing %s color schemes.\n%s' % |
|
1979 | warn('Error changing %s color schemes.\n%s' % | |
1980 | (name,sys.exc_info()[1])) |
|
1980 | (name,sys.exc_info()[1])) | |
1981 |
|
1981 | |||
1982 |
|
1982 | |||
1983 | new_scheme = parameter_s.strip() |
|
1983 | new_scheme = parameter_s.strip() | |
1984 | if not new_scheme: |
|
1984 | if not new_scheme: | |
1985 | print 'You must specify a color scheme.' |
|
1985 | print 'You must specify a color scheme.' | |
1986 | return |
|
1986 | return | |
1987 | # Under Windows, check for Gary Bishop's readline, which is necessary |
|
1987 | # Under Windows, check for Gary Bishop's readline, which is necessary | |
1988 | # for ANSI coloring |
|
1988 | # for ANSI coloring | |
1989 | if os.name in ['nt','dos']: |
|
1989 | if os.name in ['nt','dos']: | |
1990 | try: |
|
1990 | try: | |
1991 | import readline |
|
1991 | import readline | |
1992 | except ImportError: |
|
1992 | except ImportError: | |
1993 | has_readline = 0 |
|
1993 | has_readline = 0 | |
1994 | else: |
|
1994 | else: | |
1995 | try: |
|
1995 | try: | |
1996 | readline.GetOutputFile() |
|
1996 | readline.GetOutputFile() | |
1997 | except AttributeError: |
|
1997 | except AttributeError: | |
1998 | has_readline = 0 |
|
1998 | has_readline = 0 | |
1999 | else: |
|
1999 | else: | |
2000 | has_readline = 1 |
|
2000 | has_readline = 1 | |
2001 | if not has_readline: |
|
2001 | if not has_readline: | |
2002 | msg = """\ |
|
2002 | msg = """\ | |
2003 | Proper color support under MS Windows requires Gary Bishop's readline library. |
|
2003 | Proper color support under MS Windows requires Gary Bishop's readline library. | |
2004 | You can find it at: |
|
2004 | You can find it at: | |
2005 | http://sourceforge.net/projects/uncpythontools |
|
2005 | http://sourceforge.net/projects/uncpythontools | |
2006 | Gary's readline needs the ctypes module, from: |
|
2006 | Gary's readline needs the ctypes module, from: | |
2007 | http://starship.python.net/crew/theller/ctypes |
|
2007 | http://starship.python.net/crew/theller/ctypes | |
2008 |
|
2008 | |||
2009 | Defaulting color scheme to 'NoColor'""" |
|
2009 | Defaulting color scheme to 'NoColor'""" | |
2010 | new_scheme = 'NoColor' |
|
2010 | new_scheme = 'NoColor' | |
2011 | warn(msg) |
|
2011 | warn(msg) | |
2012 | # local shortcut |
|
2012 | # local shortcut | |
2013 | shell = self.shell |
|
2013 | shell = self.shell | |
2014 |
|
2014 | |||
2015 | # Set prompt colors |
|
2015 | # Set prompt colors | |
2016 | try: |
|
2016 | try: | |
2017 | shell.outputcache.set_colors(new_scheme) |
|
2017 | shell.outputcache.set_colors(new_scheme) | |
2018 | except: |
|
2018 | except: | |
2019 | color_switch_err('prompt') |
|
2019 | color_switch_err('prompt') | |
2020 | else: |
|
2020 | else: | |
2021 | shell.rc.colors = \ |
|
2021 | shell.rc.colors = \ | |
2022 | shell.outputcache.color_table.active_scheme_name |
|
2022 | shell.outputcache.color_table.active_scheme_name | |
2023 | # Set exception colors |
|
2023 | # Set exception colors | |
2024 | try: |
|
2024 | try: | |
2025 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
2025 | shell.InteractiveTB.set_colors(scheme = new_scheme) | |
2026 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
2026 | shell.SyntaxTB.set_colors(scheme = new_scheme) | |
2027 | except: |
|
2027 | except: | |
2028 | color_switch_err('exception') |
|
2028 | color_switch_err('exception') | |
2029 |
|
2029 | |||
2030 | # threaded shells use a verbose traceback in sys.excepthook |
|
2030 | # threaded shells use a verbose traceback in sys.excepthook | |
2031 | if shell.isthreaded: |
|
2031 | if shell.isthreaded: | |
2032 | try: |
|
2032 | try: | |
2033 | shell.sys_excepthook.set_colors(scheme=new_scheme) |
|
2033 | shell.sys_excepthook.set_colors(scheme=new_scheme) | |
2034 | except: |
|
2034 | except: | |
2035 | color_switch_err('system exception handler') |
|
2035 | color_switch_err('system exception handler') | |
2036 |
|
2036 | |||
2037 | # Set info (for 'object?') colors |
|
2037 | # Set info (for 'object?') colors | |
2038 | if shell.rc.color_info: |
|
2038 | if shell.rc.color_info: | |
2039 | try: |
|
2039 | try: | |
2040 | shell.inspector.set_active_scheme(new_scheme) |
|
2040 | shell.inspector.set_active_scheme(new_scheme) | |
2041 | except: |
|
2041 | except: | |
2042 | color_switch_err('object inspector') |
|
2042 | color_switch_err('object inspector') | |
2043 | else: |
|
2043 | else: | |
2044 | shell.inspector.set_active_scheme('NoColor') |
|
2044 | shell.inspector.set_active_scheme('NoColor') | |
2045 |
|
2045 | |||
2046 | def magic_color_info(self,parameter_s = ''): |
|
2046 | def magic_color_info(self,parameter_s = ''): | |
2047 | """Toggle color_info. |
|
2047 | """Toggle color_info. | |
2048 |
|
2048 | |||
2049 | The color_info configuration parameter controls whether colors are |
|
2049 | The color_info configuration parameter controls whether colors are | |
2050 | used for displaying object details (by things like %psource, %pfile or |
|
2050 | used for displaying object details (by things like %psource, %pfile or | |
2051 | the '?' system). This function toggles this value with each call. |
|
2051 | the '?' system). This function toggles this value with each call. | |
2052 |
|
2052 | |||
2053 | Note that unless you have a fairly recent pager (less works better |
|
2053 | Note that unless you have a fairly recent pager (less works better | |
2054 | than more) in your system, using colored object information displays |
|
2054 | than more) in your system, using colored object information displays | |
2055 | will not work properly. Test it and see.""" |
|
2055 | will not work properly. Test it and see.""" | |
2056 |
|
2056 | |||
2057 | self.shell.rc.color_info = 1 - self.shell.rc.color_info |
|
2057 | self.shell.rc.color_info = 1 - self.shell.rc.color_info | |
2058 | self.magic_colors(self.shell.rc.colors) |
|
2058 | self.magic_colors(self.shell.rc.colors) | |
2059 | print 'Object introspection functions have now coloring:', |
|
2059 | print 'Object introspection functions have now coloring:', | |
2060 | print ['OFF','ON'][self.shell.rc.color_info] |
|
2060 | print ['OFF','ON'][self.shell.rc.color_info] | |
2061 |
|
2061 | |||
2062 | def magic_Pprint(self, parameter_s=''): |
|
2062 | def magic_Pprint(self, parameter_s=''): | |
2063 | """Toggle pretty printing on/off.""" |
|
2063 | """Toggle pretty printing on/off.""" | |
2064 |
|
2064 | |||
2065 | self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint |
|
2065 | self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint | |
2066 | print 'Pretty printing has been turned', \ |
|
2066 | print 'Pretty printing has been turned', \ | |
2067 | ['OFF','ON'][self.shell.outputcache.Pprint] |
|
2067 | ['OFF','ON'][self.shell.outputcache.Pprint] | |
2068 |
|
2068 | |||
2069 | def magic_exit(self, parameter_s=''): |
|
2069 | def magic_exit(self, parameter_s=''): | |
2070 | """Exit IPython, confirming if configured to do so. |
|
2070 | """Exit IPython, confirming if configured to do so. | |
2071 |
|
2071 | |||
2072 | You can configure whether IPython asks for confirmation upon exit by |
|
2072 | You can configure whether IPython asks for confirmation upon exit by | |
2073 | setting the confirm_exit flag in the ipythonrc file.""" |
|
2073 | setting the confirm_exit flag in the ipythonrc file.""" | |
2074 |
|
2074 | |||
2075 | self.shell.exit() |
|
2075 | self.shell.exit() | |
2076 |
|
2076 | |||
2077 | def magic_quit(self, parameter_s=''): |
|
2077 | def magic_quit(self, parameter_s=''): | |
2078 | """Exit IPython, confirming if configured to do so (like %exit)""" |
|
2078 | """Exit IPython, confirming if configured to do so (like %exit)""" | |
2079 |
|
2079 | |||
2080 | self.shell.exit() |
|
2080 | self.shell.exit() | |
2081 |
|
2081 | |||
2082 | def magic_Exit(self, parameter_s=''): |
|
2082 | def magic_Exit(self, parameter_s=''): | |
2083 | """Exit IPython without confirmation.""" |
|
2083 | """Exit IPython without confirmation.""" | |
2084 |
|
2084 | |||
2085 | self.shell.exit_now = True |
|
2085 | self.shell.exit_now = True | |
2086 |
|
2086 | |||
2087 | def magic_Quit(self, parameter_s=''): |
|
2087 | def magic_Quit(self, parameter_s=''): | |
2088 | """Exit IPython without confirmation (like %Exit).""" |
|
2088 | """Exit IPython without confirmation (like %Exit).""" | |
2089 |
|
2089 | |||
2090 | self.shell.exit_now = True |
|
2090 | self.shell.exit_now = True | |
2091 |
|
2091 | |||
2092 | #...................................................................... |
|
2092 | #...................................................................... | |
2093 | # Functions to implement unix shell-type things |
|
2093 | # Functions to implement unix shell-type things | |
2094 |
|
2094 | |||
2095 | def magic_alias(self, parameter_s = ''): |
|
2095 | def magic_alias(self, parameter_s = ''): | |
2096 | """Define an alias for a system command. |
|
2096 | """Define an alias for a system command. | |
2097 |
|
2097 | |||
2098 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
2098 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' | |
2099 |
|
2099 | |||
2100 | Then, typing 'alias_name params' will execute the system command 'cmd |
|
2100 | Then, typing 'alias_name params' will execute the system command 'cmd | |
2101 | params' (from your underlying operating system). |
|
2101 | params' (from your underlying operating system). | |
2102 |
|
2102 | |||
2103 | Aliases have lower precedence than magic functions and Python normal |
|
2103 | Aliases have lower precedence than magic functions and Python normal | |
2104 | variables, so if 'foo' is both a Python variable and an alias, the |
|
2104 | variables, so if 'foo' is both a Python variable and an alias, the | |
2105 | alias can not be executed until 'del foo' removes the Python variable. |
|
2105 | alias can not be executed until 'del foo' removes the Python variable. | |
2106 |
|
2106 | |||
2107 | You can use the %l specifier in an alias definition to represent the |
|
2107 | You can use the %l specifier in an alias definition to represent the | |
2108 | whole line when the alias is called. For example: |
|
2108 | whole line when the alias is called. For example: | |
2109 |
|
2109 | |||
2110 | In [2]: alias all echo "Input in brackets: <%l>"\\ |
|
2110 | In [2]: alias all echo "Input in brackets: <%l>"\\ | |
2111 | In [3]: all hello world\\ |
|
2111 | In [3]: all hello world\\ | |
2112 | Input in brackets: <hello world> |
|
2112 | Input in brackets: <hello world> | |
2113 |
|
2113 | |||
2114 | You can also define aliases with parameters using %s specifiers (one |
|
2114 | You can also define aliases with parameters using %s specifiers (one | |
2115 | per parameter): |
|
2115 | per parameter): | |
2116 |
|
2116 | |||
2117 | In [1]: alias parts echo first %s second %s\\ |
|
2117 | In [1]: alias parts echo first %s second %s\\ | |
2118 | In [2]: %parts A B\\ |
|
2118 | In [2]: %parts A B\\ | |
2119 | first A second B\\ |
|
2119 | first A second B\\ | |
2120 | In [3]: %parts A\\ |
|
2120 | In [3]: %parts A\\ | |
2121 | Incorrect number of arguments: 2 expected.\\ |
|
2121 | Incorrect number of arguments: 2 expected.\\ | |
2122 | parts is an alias to: 'echo first %s second %s' |
|
2122 | parts is an alias to: 'echo first %s second %s' | |
2123 |
|
2123 | |||
2124 | Note that %l and %s are mutually exclusive. You can only use one or |
|
2124 | Note that %l and %s are mutually exclusive. You can only use one or | |
2125 | the other in your aliases. |
|
2125 | the other in your aliases. | |
2126 |
|
2126 | |||
2127 | Aliases expand Python variables just like system calls using ! or !! |
|
2127 | Aliases expand Python variables just like system calls using ! or !! | |
2128 | do: all expressions prefixed with '$' get expanded. For details of |
|
2128 | do: all expressions prefixed with '$' get expanded. For details of | |
2129 | the semantic rules, see PEP-215: |
|
2129 | the semantic rules, see PEP-215: | |
2130 | http://www.python.org/peps/pep-0215.html. This is the library used by |
|
2130 | http://www.python.org/peps/pep-0215.html. This is the library used by | |
2131 | IPython for variable expansion. If you want to access a true shell |
|
2131 | IPython for variable expansion. If you want to access a true shell | |
2132 | variable, an extra $ is necessary to prevent its expansion by IPython: |
|
2132 | variable, an extra $ is necessary to prevent its expansion by IPython: | |
2133 |
|
2133 | |||
2134 | In [6]: alias show echo\\ |
|
2134 | In [6]: alias show echo\\ | |
2135 | In [7]: PATH='A Python string'\\ |
|
2135 | In [7]: PATH='A Python string'\\ | |
2136 | In [8]: show $PATH\\ |
|
2136 | In [8]: show $PATH\\ | |
2137 | A Python string\\ |
|
2137 | A Python string\\ | |
2138 | In [9]: show $$PATH\\ |
|
2138 | In [9]: show $$PATH\\ | |
2139 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... |
|
2139 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... | |
2140 |
|
2140 | |||
2141 | You can use the alias facility to acess all of $PATH. See the %rehash |
|
2141 | You can use the alias facility to acess all of $PATH. See the %rehash | |
2142 | and %rehashx functions, which automatically create aliases for the |
|
2142 | and %rehashx functions, which automatically create aliases for the | |
2143 | contents of your $PATH. |
|
2143 | contents of your $PATH. | |
2144 |
|
2144 | |||
2145 | If called with no parameters, %alias prints the current alias table.""" |
|
2145 | If called with no parameters, %alias prints the current alias table.""" | |
2146 |
|
2146 | |||
2147 | par = parameter_s.strip() |
|
2147 | par = parameter_s.strip() | |
2148 | if not par: |
|
2148 | if not par: | |
2149 | if self.shell.rc.automagic: |
|
2149 | if self.shell.rc.automagic: | |
2150 | prechar = '' |
|
2150 | prechar = '' | |
2151 | else: |
|
2151 | else: | |
2152 | prechar = self.shell.ESC_MAGIC |
|
2152 | prechar = self.shell.ESC_MAGIC | |
2153 | print 'Alias\t\tSystem Command\n'+'-'*30 |
|
2153 | print 'Alias\t\tSystem Command\n'+'-'*30 | |
2154 | atab = self.shell.alias_table |
|
2154 | atab = self.shell.alias_table | |
2155 | aliases = atab.keys() |
|
2155 | aliases = atab.keys() | |
2156 | aliases.sort() |
|
2156 | aliases.sort() | |
2157 | for alias in aliases: |
|
2157 | for alias in aliases: | |
2158 | print prechar+alias+'\t\t'+atab[alias][1] |
|
2158 | print prechar+alias+'\t\t'+atab[alias][1] | |
2159 | print '-'*30+'\nTotal number of aliases:',len(aliases) |
|
2159 | print '-'*30+'\nTotal number of aliases:',len(aliases) | |
2160 | return |
|
2160 | return | |
2161 | try: |
|
2161 | try: | |
2162 | alias,cmd = par.split(None,1) |
|
2162 | alias,cmd = par.split(None,1) | |
2163 | except: |
|
2163 | except: | |
2164 | print OInspect.getdoc(self.magic_alias) |
|
2164 | print OInspect.getdoc(self.magic_alias) | |
2165 | else: |
|
2165 | else: | |
2166 | nargs = cmd.count('%s') |
|
2166 | nargs = cmd.count('%s') | |
2167 | if nargs>0 and cmd.find('%l')>=0: |
|
2167 | if nargs>0 and cmd.find('%l')>=0: | |
2168 | error('The %s and %l specifiers are mutually exclusive ' |
|
2168 | error('The %s and %l specifiers are mutually exclusive ' | |
2169 | 'in alias definitions.') |
|
2169 | 'in alias definitions.') | |
2170 | else: # all looks OK |
|
2170 | else: # all looks OK | |
2171 | self.shell.alias_table[alias] = (nargs,cmd) |
|
2171 | self.shell.alias_table[alias] = (nargs,cmd) | |
2172 | self.shell.alias_table_validate(verbose=1) |
|
2172 | self.shell.alias_table_validate(verbose=1) | |
2173 | # end magic_alias |
|
2173 | # end magic_alias | |
2174 |
|
2174 | |||
2175 | def magic_unalias(self, parameter_s = ''): |
|
2175 | def magic_unalias(self, parameter_s = ''): | |
2176 | """Remove an alias""" |
|
2176 | """Remove an alias""" | |
2177 |
|
2177 | |||
2178 | aname = parameter_s.strip() |
|
2178 | aname = parameter_s.strip() | |
2179 | if aname in self.shell.alias_table: |
|
2179 | if aname in self.shell.alias_table: | |
2180 | del self.shell.alias_table[aname] |
|
2180 | del self.shell.alias_table[aname] | |
2181 |
|
2181 | |||
2182 | def magic_rehash(self, parameter_s = ''): |
|
2182 | def magic_rehash(self, parameter_s = ''): | |
2183 | """Update the alias table with all entries in $PATH. |
|
2183 | """Update the alias table with all entries in $PATH. | |
2184 |
|
2184 | |||
2185 | This version does no checks on execute permissions or whether the |
|
2185 | This version does no checks on execute permissions or whether the | |
2186 | contents of $PATH are truly files (instead of directories or something |
|
2186 | contents of $PATH are truly files (instead of directories or something | |
2187 | else). For such a safer (but slower) version, use %rehashx.""" |
|
2187 | else). For such a safer (but slower) version, use %rehashx.""" | |
2188 |
|
2188 | |||
2189 | # This function (and rehashx) manipulate the alias_table directly |
|
2189 | # This function (and rehashx) manipulate the alias_table directly | |
2190 | # rather than calling magic_alias, for speed reasons. A rehash on a |
|
2190 | # rather than calling magic_alias, for speed reasons. A rehash on a | |
2191 | # typical Linux box involves several thousand entries, so efficiency |
|
2191 | # typical Linux box involves several thousand entries, so efficiency | |
2192 | # here is a top concern. |
|
2192 | # here is a top concern. | |
2193 |
|
2193 | |||
2194 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2194 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) | |
2195 | alias_table = self.shell.alias_table |
|
2195 | alias_table = self.shell.alias_table | |
2196 | for pdir in path: |
|
2196 | for pdir in path: | |
2197 | for ff in os.listdir(pdir): |
|
2197 | for ff in os.listdir(pdir): | |
2198 | # each entry in the alias table must be (N,name), where |
|
2198 | # each entry in the alias table must be (N,name), where | |
2199 | # N is the number of positional arguments of the alias. |
|
2199 | # N is the number of positional arguments of the alias. | |
2200 | alias_table[ff] = (0,ff) |
|
2200 | alias_table[ff] = (0,ff) | |
2201 | # Make sure the alias table doesn't contain keywords or builtins |
|
2201 | # Make sure the alias table doesn't contain keywords or builtins | |
2202 | self.shell.alias_table_validate() |
|
2202 | self.shell.alias_table_validate() | |
2203 | # Call again init_auto_alias() so we get 'rm -i' and other modified |
|
2203 | # Call again init_auto_alias() so we get 'rm -i' and other modified | |
2204 | # aliases since %rehash will probably clobber them |
|
2204 | # aliases since %rehash will probably clobber them | |
2205 | self.shell.init_auto_alias() |
|
2205 | self.shell.init_auto_alias() | |
2206 |
|
2206 | |||
2207 | def magic_rehashx(self, parameter_s = ''): |
|
2207 | def magic_rehashx(self, parameter_s = ''): | |
2208 | """Update the alias table with all executable files in $PATH. |
|
2208 | """Update the alias table with all executable files in $PATH. | |
2209 |
|
2209 | |||
2210 | This version explicitly checks that every entry in $PATH is a file |
|
2210 | This version explicitly checks that every entry in $PATH is a file | |
2211 | with execute access (os.X_OK), so it is much slower than %rehash. |
|
2211 | with execute access (os.X_OK), so it is much slower than %rehash. | |
2212 |
|
2212 | |||
2213 | Under Windows, it checks executability as a match agains a |
|
2213 | Under Windows, it checks executability as a match agains a | |
2214 | '|'-separated string of extensions, stored in the IPython config |
|
2214 | '|'-separated string of extensions, stored in the IPython config | |
2215 | variable win_exec_ext. This defaults to 'exe|com|bat'. """ |
|
2215 | variable win_exec_ext. This defaults to 'exe|com|bat'. """ | |
2216 |
|
2216 | |||
2217 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2217 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) | |
2218 | alias_table = self.shell.alias_table |
|
2218 | alias_table = self.shell.alias_table | |
2219 |
|
2219 | |||
2220 | if os.name == 'posix': |
|
2220 | if os.name == 'posix': | |
2221 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
2221 | isexec = lambda fname:os.path.isfile(fname) and \ | |
2222 | os.access(fname,os.X_OK) |
|
2222 | os.access(fname,os.X_OK) | |
2223 | else: |
|
2223 | else: | |
2224 |
|
2224 | |||
2225 | try: |
|
2225 | try: | |
2226 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
2226 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |
2227 | except KeyError: |
|
2227 | except KeyError: | |
2228 | winext = 'exe|com|bat' |
|
2228 | winext = 'exe|com|bat' | |
2229 |
|
2229 | |||
2230 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
2230 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |
2231 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
2231 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | |
2232 | savedir = os.getcwd() |
|
2232 | savedir = os.getcwd() | |
2233 | try: |
|
2233 | try: | |
2234 | # write the whole loop for posix/Windows so we don't have an if in |
|
2234 | # write the whole loop for posix/Windows so we don't have an if in | |
2235 | # the innermost part |
|
2235 | # the innermost part | |
2236 | if os.name == 'posix': |
|
2236 | if os.name == 'posix': | |
2237 | for pdir in path: |
|
2237 | for pdir in path: | |
2238 | os.chdir(pdir) |
|
2238 | os.chdir(pdir) | |
2239 | for ff in os.listdir(pdir): |
|
2239 | for ff in os.listdir(pdir): | |
2240 | if isexec(ff): |
|
2240 | if isexec(ff): | |
2241 | # each entry in the alias table must be (N,name), |
|
2241 | # each entry in the alias table must be (N,name), | |
2242 | # where N is the number of positional arguments of the |
|
2242 | # where N is the number of positional arguments of the | |
2243 | # alias. |
|
2243 | # alias. | |
2244 | alias_table[ff] = (0,ff) |
|
2244 | alias_table[ff] = (0,ff) | |
2245 | else: |
|
2245 | else: | |
2246 | for pdir in path: |
|
2246 | for pdir in path: | |
2247 | os.chdir(pdir) |
|
2247 | os.chdir(pdir) | |
2248 | for ff in os.listdir(pdir): |
|
2248 | for ff in os.listdir(pdir): | |
2249 | if isexec(ff): |
|
2249 | if isexec(ff): | |
2250 | alias_table[execre.sub(r'\1',ff)] = (0,ff) |
|
2250 | alias_table[execre.sub(r'\1',ff)] = (0,ff) | |
2251 | # Make sure the alias table doesn't contain keywords or builtins |
|
2251 | # Make sure the alias table doesn't contain keywords or builtins | |
2252 | self.shell.alias_table_validate() |
|
2252 | self.shell.alias_table_validate() | |
2253 | # Call again init_auto_alias() so we get 'rm -i' and other |
|
2253 | # Call again init_auto_alias() so we get 'rm -i' and other | |
2254 | # modified aliases since %rehashx will probably clobber them |
|
2254 | # modified aliases since %rehashx will probably clobber them | |
2255 | self.shell.init_auto_alias() |
|
2255 | self.shell.init_auto_alias() | |
2256 | finally: |
|
2256 | finally: | |
2257 | os.chdir(savedir) |
|
2257 | os.chdir(savedir) | |
2258 |
|
2258 | |||
2259 | def magic_pwd(self, parameter_s = ''): |
|
2259 | def magic_pwd(self, parameter_s = ''): | |
2260 | """Return the current working directory path.""" |
|
2260 | """Return the current working directory path.""" | |
2261 | return os.getcwd() |
|
2261 | return os.getcwd() | |
2262 |
|
2262 | |||
2263 | def magic_cd(self, parameter_s=''): |
|
2263 | def magic_cd(self, parameter_s=''): | |
2264 | """Change the current working directory. |
|
2264 | """Change the current working directory. | |
2265 |
|
2265 | |||
2266 | This command automatically maintains an internal list of directories |
|
2266 | This command automatically maintains an internal list of directories | |
2267 | you visit during your IPython session, in the variable _dh. The |
|
2267 | you visit during your IPython session, in the variable _dh. The | |
2268 | command %dhist shows this history nicely formatted. |
|
2268 | command %dhist shows this history nicely formatted. | |
2269 |
|
2269 | |||
2270 | Usage: |
|
2270 | Usage: | |
2271 |
|
2271 | |||
2272 | cd 'dir': changes to directory 'dir'. |
|
2272 | cd 'dir': changes to directory 'dir'. | |
2273 |
|
2273 | |||
2274 | cd -: changes to the last visited directory. |
|
2274 | cd -: changes to the last visited directory. | |
2275 |
|
2275 | |||
2276 | cd -<n>: changes to the n-th directory in the directory history. |
|
2276 | cd -<n>: changes to the n-th directory in the directory history. | |
2277 |
|
2277 | |||
2278 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
|
2278 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark | |
2279 | (note: cd <bookmark_name> is enough if there is no |
|
2279 | (note: cd <bookmark_name> is enough if there is no | |
2280 | directory <bookmark_name>, but a bookmark with the name exists.) |
|
2280 | directory <bookmark_name>, but a bookmark with the name exists.) | |
2281 |
|
2281 | |||
2282 | Options: |
|
2282 | Options: | |
2283 |
|
2283 | |||
2284 | -q: quiet. Do not print the working directory after the cd command is |
|
2284 | -q: quiet. Do not print the working directory after the cd command is | |
2285 | executed. By default IPython's cd command does print this directory, |
|
2285 | executed. By default IPython's cd command does print this directory, | |
2286 | since the default prompts do not display path information. |
|
2286 | since the default prompts do not display path information. | |
2287 |
|
2287 | |||
2288 | Note that !cd doesn't work for this purpose because the shell where |
|
2288 | Note that !cd doesn't work for this purpose because the shell where | |
2289 | !command runs is immediately discarded after executing 'command'.""" |
|
2289 | !command runs is immediately discarded after executing 'command'.""" | |
2290 |
|
2290 | |||
2291 | parameter_s = parameter_s.strip() |
|
2291 | parameter_s = parameter_s.strip() | |
2292 | bkms = self.shell.persist.get("bookmarks",{}) |
|
2292 | bkms = self.shell.persist.get("bookmarks",{}) | |
2293 |
|
2293 | |||
2294 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
|
2294 | numcd = re.match(r'(-)(\d+)$',parameter_s) | |
2295 | # jump in directory history by number |
|
2295 | # jump in directory history by number | |
2296 | if numcd: |
|
2296 | if numcd: | |
2297 | nn = int(numcd.group(2)) |
|
2297 | nn = int(numcd.group(2)) | |
2298 | try: |
|
2298 | try: | |
2299 | ps = self.shell.user_ns['_dh'][nn] |
|
2299 | ps = self.shell.user_ns['_dh'][nn] | |
2300 | except IndexError: |
|
2300 | except IndexError: | |
2301 | print 'The requested directory does not exist in history.' |
|
2301 | print 'The requested directory does not exist in history.' | |
2302 | return |
|
2302 | return | |
2303 | else: |
|
2303 | else: | |
2304 | opts = {} |
|
2304 | opts = {} | |
2305 | else: |
|
2305 | else: | |
2306 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') |
|
2306 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') | |
2307 | # jump to previous |
|
2307 | # jump to previous | |
2308 | if ps == '-': |
|
2308 | if ps == '-': | |
2309 | try: |
|
2309 | try: | |
2310 | ps = self.shell.user_ns['_dh'][-2] |
|
2310 | ps = self.shell.user_ns['_dh'][-2] | |
2311 | except IndexError: |
|
2311 | except IndexError: | |
2312 | print 'No previous directory to change to.' |
|
2312 | print 'No previous directory to change to.' | |
2313 | return |
|
2313 | return | |
2314 | # jump to bookmark |
|
2314 | # jump to bookmark | |
2315 | elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)): |
|
2315 | elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)): | |
2316 | if bkms.has_key(ps): |
|
2316 | if bkms.has_key(ps): | |
2317 | target = bkms[ps] |
|
2317 | target = bkms[ps] | |
2318 | print '(bookmark:%s) -> %s' % (ps,target) |
|
2318 | print '(bookmark:%s) -> %s' % (ps,target) | |
2319 | ps = target |
|
2319 | ps = target | |
2320 | else: |
|
2320 | else: | |
2321 | if bkms: |
|
2321 | if bkms: | |
2322 | error("Bookmark '%s' not found. " |
|
2322 | error("Bookmark '%s' not found. " | |
2323 | "Use '%%bookmark -l' to see your bookmarks." % ps) |
|
2323 | "Use '%%bookmark -l' to see your bookmarks." % ps) | |
2324 | else: |
|
2324 | else: | |
2325 | print "Bookmarks not set - use %bookmark <bookmarkname>" |
|
2325 | print "Bookmarks not set - use %bookmark <bookmarkname>" | |
2326 | return |
|
2326 | return | |
2327 |
|
2327 | |||
2328 | # at this point ps should point to the target dir |
|
2328 | # at this point ps should point to the target dir | |
2329 | if ps: |
|
2329 | if ps: | |
2330 | try: |
|
2330 | try: | |
2331 | os.chdir(os.path.expanduser(ps)) |
|
2331 | os.chdir(os.path.expanduser(ps)) | |
2332 | except OSError: |
|
2332 | except OSError: | |
2333 | print sys.exc_info()[1] |
|
2333 | print sys.exc_info()[1] | |
2334 | else: |
|
2334 | else: | |
2335 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2335 | self.shell.user_ns['_dh'].append(os.getcwd()) | |
2336 | else: |
|
2336 | else: | |
2337 | os.chdir(self.shell.home_dir) |
|
2337 | os.chdir(self.shell.home_dir) | |
2338 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2338 | self.shell.user_ns['_dh'].append(os.getcwd()) | |
2339 | if not 'q' in opts: |
|
2339 | if not 'q' in opts: | |
2340 | print self.shell.user_ns['_dh'][-1] |
|
2340 | print self.shell.user_ns['_dh'][-1] | |
2341 |
|
2341 | |||
2342 | def magic_dhist(self, parameter_s=''): |
|
2342 | def magic_dhist(self, parameter_s=''): | |
2343 | """Print your history of visited directories. |
|
2343 | """Print your history of visited directories. | |
2344 |
|
2344 | |||
2345 | %dhist -> print full history\\ |
|
2345 | %dhist -> print full history\\ | |
2346 | %dhist n -> print last n entries only\\ |
|
2346 | %dhist n -> print last n entries only\\ | |
2347 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ |
|
2347 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ | |
2348 |
|
2348 | |||
2349 | This history is automatically maintained by the %cd command, and |
|
2349 | This history is automatically maintained by the %cd command, and | |
2350 | always available as the global list variable _dh. You can use %cd -<n> |
|
2350 | always available as the global list variable _dh. You can use %cd -<n> | |
2351 | to go to directory number <n>.""" |
|
2351 | to go to directory number <n>.""" | |
2352 |
|
2352 | |||
2353 | dh = self.shell.user_ns['_dh'] |
|
2353 | dh = self.shell.user_ns['_dh'] | |
2354 | if parameter_s: |
|
2354 | if parameter_s: | |
2355 | try: |
|
2355 | try: | |
2356 | args = map(int,parameter_s.split()) |
|
2356 | args = map(int,parameter_s.split()) | |
2357 | except: |
|
2357 | except: | |
2358 | self.arg_err(Magic.magic_dhist) |
|
2358 | self.arg_err(Magic.magic_dhist) | |
2359 | return |
|
2359 | return | |
2360 | if len(args) == 1: |
|
2360 | if len(args) == 1: | |
2361 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
2361 | ini,fin = max(len(dh)-(args[0]),0),len(dh) | |
2362 | elif len(args) == 2: |
|
2362 | elif len(args) == 2: | |
2363 | ini,fin = args |
|
2363 | ini,fin = args | |
2364 | else: |
|
2364 | else: | |
2365 | self.arg_err(Magic.magic_dhist) |
|
2365 | self.arg_err(Magic.magic_dhist) | |
2366 | return |
|
2366 | return | |
2367 | else: |
|
2367 | else: | |
2368 | ini,fin = 0,len(dh) |
|
2368 | ini,fin = 0,len(dh) | |
2369 | nlprint(dh, |
|
2369 | nlprint(dh, | |
2370 | header = 'Directory history (kept in _dh)', |
|
2370 | header = 'Directory history (kept in _dh)', | |
2371 | start=ini,stop=fin) |
|
2371 | start=ini,stop=fin) | |
2372 |
|
2372 | |||
2373 | def magic_env(self, parameter_s=''): |
|
2373 | def magic_env(self, parameter_s=''): | |
2374 | """List environment variables.""" |
|
2374 | """List environment variables.""" | |
2375 |
|
2375 | |||
2376 | return os.environ.data |
|
2376 | return os.environ.data | |
2377 |
|
2377 | |||
2378 | def magic_pushd(self, parameter_s=''): |
|
2378 | def magic_pushd(self, parameter_s=''): | |
2379 | """Place the current dir on stack and change directory. |
|
2379 | """Place the current dir on stack and change directory. | |
2380 |
|
2380 | |||
2381 | Usage:\\ |
|
2381 | Usage:\\ | |
2382 | %pushd ['dirname'] |
|
2382 | %pushd ['dirname'] | |
2383 |
|
2383 | |||
2384 | %pushd with no arguments does a %pushd to your home directory. |
|
2384 | %pushd with no arguments does a %pushd to your home directory. | |
2385 | """ |
|
2385 | """ | |
2386 | if parameter_s == '': parameter_s = '~' |
|
2386 | if parameter_s == '': parameter_s = '~' | |
2387 | dir_s = self.shell.dir_stack |
|
2387 | dir_s = self.shell.dir_stack | |
2388 | if len(dir_s)>0 and os.path.expanduser(parameter_s) != \ |
|
2388 | if len(dir_s)>0 and os.path.expanduser(parameter_s) != \ | |
2389 | os.path.expanduser(self.shell.dir_stack[0]): |
|
2389 | os.path.expanduser(self.shell.dir_stack[0]): | |
2390 | try: |
|
2390 | try: | |
2391 | self.magic_cd(parameter_s) |
|
2391 | self.magic_cd(parameter_s) | |
2392 | dir_s.insert(0,os.getcwd().replace(self.home_dir,'~')) |
|
2392 | dir_s.insert(0,os.getcwd().replace(self.home_dir,'~')) | |
2393 | self.magic_dirs() |
|
2393 | self.magic_dirs() | |
2394 | except: |
|
2394 | except: | |
2395 | print 'Invalid directory' |
|
2395 | print 'Invalid directory' | |
2396 | else: |
|
2396 | else: | |
2397 | print 'You are already there!' |
|
2397 | print 'You are already there!' | |
2398 |
|
2398 | |||
2399 | def magic_popd(self, parameter_s=''): |
|
2399 | def magic_popd(self, parameter_s=''): | |
2400 | """Change to directory popped off the top of the stack. |
|
2400 | """Change to directory popped off the top of the stack. | |
2401 | """ |
|
2401 | """ | |
2402 | if len (self.shell.dir_stack) > 1: |
|
2402 | if len (self.shell.dir_stack) > 1: | |
2403 | self.shell.dir_stack.pop(0) |
|
2403 | self.shell.dir_stack.pop(0) | |
2404 | self.magic_cd(self.shell.dir_stack[0]) |
|
2404 | self.magic_cd(self.shell.dir_stack[0]) | |
2405 | print self.shell.dir_stack[0] |
|
2405 | print self.shell.dir_stack[0] | |
2406 | else: |
|
2406 | else: | |
2407 | print "You can't remove the starting directory from the stack:",\ |
|
2407 | print "You can't remove the starting directory from the stack:",\ | |
2408 | self.shell.dir_stack |
|
2408 | self.shell.dir_stack | |
2409 |
|
2409 | |||
2410 | def magic_dirs(self, parameter_s=''): |
|
2410 | def magic_dirs(self, parameter_s=''): | |
2411 | """Return the current directory stack.""" |
|
2411 | """Return the current directory stack.""" | |
2412 |
|
2412 | |||
2413 | return self.shell.dir_stack[:] |
|
2413 | return self.shell.dir_stack[:] | |
2414 |
|
2414 | |||
2415 | def magic_sc(self, parameter_s=''): |
|
2415 | def magic_sc(self, parameter_s=''): | |
2416 | """Shell capture - execute a shell command and capture its output. |
|
2416 | """Shell capture - execute a shell command and capture its output. | |
2417 |
|
2417 | |||
2418 | %sc [options] varname=command |
|
2418 | %sc [options] varname=command | |
2419 |
|
2419 | |||
2420 | IPython will run the given command using commands.getoutput(), and |
|
2420 | IPython will run the given command using commands.getoutput(), and | |
2421 | will then update the user's interactive namespace with a variable |
|
2421 | will then update the user's interactive namespace with a variable | |
2422 | called varname, containing the value of the call. Your command can |
|
2422 | called varname, containing the value of the call. Your command can | |
2423 | contain shell wildcards, pipes, etc. |
|
2423 | contain shell wildcards, pipes, etc. | |
2424 |
|
2424 | |||
2425 | The '=' sign in the syntax is mandatory, and the variable name you |
|
2425 | The '=' sign in the syntax is mandatory, and the variable name you | |
2426 | supply must follow Python's standard conventions for valid names. |
|
2426 | supply must follow Python's standard conventions for valid names. | |
2427 |
|
2427 | |||
2428 | Options: |
|
2428 | Options: | |
2429 |
|
2429 | |||
2430 | -l: list output. Split the output on newlines into a list before |
|
2430 | -l: list output. Split the output on newlines into a list before | |
2431 | assigning it to the given variable. By default the output is stored |
|
2431 | assigning it to the given variable. By default the output is stored | |
2432 | as a single string. |
|
2432 | as a single string. | |
2433 |
|
2433 | |||
2434 | -v: verbose. Print the contents of the variable. |
|
2434 | -v: verbose. Print the contents of the variable. | |
2435 |
|
2435 | |||
2436 | In most cases you should not need to split as a list, because the |
|
2436 | In most cases you should not need to split as a list, because the | |
2437 | returned value is a special type of string which can automatically |
|
2437 | returned value is a special type of string which can automatically | |
2438 | provide its contents either as a list (split on newlines) or as a |
|
2438 | provide its contents either as a list (split on newlines) or as a | |
2439 | space-separated string. These are convenient, respectively, either |
|
2439 | space-separated string. These are convenient, respectively, either | |
2440 | for sequential processing or to be passed to a shell command. |
|
2440 | for sequential processing or to be passed to a shell command. | |
2441 |
|
2441 | |||
2442 | For example: |
|
2442 | For example: | |
2443 |
|
2443 | |||
2444 | # Capture into variable a |
|
2444 | # Capture into variable a | |
2445 | In [9]: sc a=ls *py |
|
2445 | In [9]: sc a=ls *py | |
2446 |
|
2446 | |||
2447 | # a is a string with embedded newlines |
|
2447 | # a is a string with embedded newlines | |
2448 | In [10]: a |
|
2448 | In [10]: a | |
2449 | Out[10]: 'setup.py\nwin32_manual_post_install.py' |
|
2449 | Out[10]: 'setup.py\nwin32_manual_post_install.py' | |
2450 |
|
2450 | |||
2451 | # which can be seen as a list: |
|
2451 | # which can be seen as a list: | |
2452 | In [11]: a.l |
|
2452 | In [11]: a.l | |
2453 | Out[11]: ['setup.py', 'win32_manual_post_install.py'] |
|
2453 | Out[11]: ['setup.py', 'win32_manual_post_install.py'] | |
2454 |
|
2454 | |||
2455 | # or as a whitespace-separated string: |
|
2455 | # or as a whitespace-separated string: | |
2456 | In [12]: a.s |
|
2456 | In [12]: a.s | |
2457 | Out[12]: 'setup.py win32_manual_post_install.py' |
|
2457 | Out[12]: 'setup.py win32_manual_post_install.py' | |
2458 |
|
2458 | |||
2459 | # a.s is useful to pass as a single command line: |
|
2459 | # a.s is useful to pass as a single command line: | |
2460 | In [13]: !wc -l $a.s |
|
2460 | In [13]: !wc -l $a.s | |
2461 | 146 setup.py |
|
2461 | 146 setup.py | |
2462 | 130 win32_manual_post_install.py |
|
2462 | 130 win32_manual_post_install.py | |
2463 | 276 total |
|
2463 | 276 total | |
2464 |
|
2464 | |||
2465 | # while the list form is useful to loop over: |
|
2465 | # while the list form is useful to loop over: | |
2466 | In [14]: for f in a.l: |
|
2466 | In [14]: for f in a.l: | |
2467 | ....: !wc -l $f |
|
2467 | ....: !wc -l $f | |
2468 | ....: |
|
2468 | ....: | |
2469 | 146 setup.py |
|
2469 | 146 setup.py | |
2470 | 130 win32_manual_post_install.py |
|
2470 | 130 win32_manual_post_install.py | |
2471 |
|
2471 | |||
2472 | Similiarly, the lists returned by the -l option are also special, in |
|
2472 | Similiarly, the lists returned by the -l option are also special, in | |
2473 | the sense that you can equally invoke the .s attribute on them to |
|
2473 | the sense that you can equally invoke the .s attribute on them to | |
2474 | automatically get a whitespace-separated string from their contents: |
|
2474 | automatically get a whitespace-separated string from their contents: | |
2475 |
|
2475 | |||
2476 | In [1]: sc -l b=ls *py |
|
2476 | In [1]: sc -l b=ls *py | |
2477 |
|
2477 | |||
2478 | In [2]: b |
|
2478 | In [2]: b | |
2479 | Out[2]: ['setup.py', 'win32_manual_post_install.py'] |
|
2479 | Out[2]: ['setup.py', 'win32_manual_post_install.py'] | |
2480 |
|
2480 | |||
2481 | In [3]: b.s |
|
2481 | In [3]: b.s | |
2482 | Out[3]: 'setup.py win32_manual_post_install.py' |
|
2482 | Out[3]: 'setup.py win32_manual_post_install.py' | |
2483 |
|
2483 | |||
2484 | In summary, both the lists and strings used for ouptut capture have |
|
2484 | In summary, both the lists and strings used for ouptut capture have | |
2485 | the following special attributes: |
|
2485 | the following special attributes: | |
2486 |
|
2486 | |||
2487 | .l (or .list) : value as list. |
|
2487 | .l (or .list) : value as list. | |
2488 | .n (or .nlstr): value as newline-separated string. |
|
2488 | .n (or .nlstr): value as newline-separated string. | |
2489 | .s (or .spstr): value as space-separated string. |
|
2489 | .s (or .spstr): value as space-separated string. | |
2490 | """ |
|
2490 | """ | |
2491 |
|
2491 | |||
2492 | opts,args = self.parse_options(parameter_s,'lv') |
|
2492 | opts,args = self.parse_options(parameter_s,'lv') | |
2493 | # Try to get a variable name and command to run |
|
2493 | # Try to get a variable name and command to run | |
2494 | try: |
|
2494 | try: | |
2495 | # the variable name must be obtained from the parse_options |
|
2495 | # the variable name must be obtained from the parse_options | |
2496 | # output, which uses shlex.split to strip options out. |
|
2496 | # output, which uses shlex.split to strip options out. | |
2497 | var,_ = args.split('=',1) |
|
2497 | var,_ = args.split('=',1) | |
2498 | var = var.strip() |
|
2498 | var = var.strip() | |
2499 | # But the the command has to be extracted from the original input |
|
2499 | # But the the command has to be extracted from the original input | |
2500 | # parameter_s, not on what parse_options returns, to avoid the |
|
2500 | # parameter_s, not on what parse_options returns, to avoid the | |
2501 | # quote stripping which shlex.split performs on it. |
|
2501 | # quote stripping which shlex.split performs on it. | |
2502 | _,cmd = parameter_s.split('=',1) |
|
2502 | _,cmd = parameter_s.split('=',1) | |
2503 | except ValueError: |
|
2503 | except ValueError: | |
2504 | var,cmd = '','' |
|
2504 | var,cmd = '','' | |
2505 | if not var: |
|
2505 | if not var: | |
2506 | error('you must specify a variable to assign the command to.') |
|
2506 | error('you must specify a variable to assign the command to.') | |
2507 | return |
|
2507 | return | |
2508 | # If all looks ok, proceed |
|
2508 | # If all looks ok, proceed | |
2509 | out,err = self.shell.getoutputerror(cmd) |
|
2509 | out,err = self.shell.getoutputerror(cmd) | |
2510 | if err: |
|
2510 | if err: | |
2511 | print >> Term.cerr,err |
|
2511 | print >> Term.cerr,err | |
2512 | if opts.has_key('l'): |
|
2512 | if opts.has_key('l'): | |
2513 | out = SList(out.split('\n')) |
|
2513 | out = SList(out.split('\n')) | |
2514 | else: |
|
2514 | else: | |
2515 | out = LSString(out) |
|
2515 | out = LSString(out) | |
2516 | if opts.has_key('v'): |
|
2516 | if opts.has_key('v'): | |
2517 | print '%s ==\n%s' % (var,pformat(out)) |
|
2517 | print '%s ==\n%s' % (var,pformat(out)) | |
2518 | self.shell.user_ns.update({var:out}) |
|
2518 | self.shell.user_ns.update({var:out}) | |
2519 |
|
2519 | |||
2520 | def magic_sx(self, parameter_s=''): |
|
2520 | def magic_sx(self, parameter_s=''): | |
2521 | """Shell execute - run a shell command and capture its output. |
|
2521 | """Shell execute - run a shell command and capture its output. | |
2522 |
|
2522 | |||
2523 | %sx command |
|
2523 | %sx command | |
2524 |
|
2524 | |||
2525 | IPython will run the given command using commands.getoutput(), and |
|
2525 | IPython will run the given command using commands.getoutput(), and | |
2526 | return the result formatted as a list (split on '\\n'). Since the |
|
2526 | return the result formatted as a list (split on '\\n'). Since the | |
2527 | output is _returned_, it will be stored in ipython's regular output |
|
2527 | output is _returned_, it will be stored in ipython's regular output | |
2528 | cache Out[N] and in the '_N' automatic variables. |
|
2528 | cache Out[N] and in the '_N' automatic variables. | |
2529 |
|
2529 | |||
2530 | Notes: |
|
2530 | Notes: | |
2531 |
|
2531 | |||
2532 | 1) If an input line begins with '!!', then %sx is automatically |
|
2532 | 1) If an input line begins with '!!', then %sx is automatically | |
2533 | invoked. That is, while: |
|
2533 | invoked. That is, while: | |
2534 | !ls |
|
2534 | !ls | |
2535 | causes ipython to simply issue system('ls'), typing |
|
2535 | causes ipython to simply issue system('ls'), typing | |
2536 | !!ls |
|
2536 | !!ls | |
2537 | is a shorthand equivalent to: |
|
2537 | is a shorthand equivalent to: | |
2538 | %sx ls |
|
2538 | %sx ls | |
2539 |
|
2539 | |||
2540 | 2) %sx differs from %sc in that %sx automatically splits into a list, |
|
2540 | 2) %sx differs from %sc in that %sx automatically splits into a list, | |
2541 | like '%sc -l'. The reason for this is to make it as easy as possible |
|
2541 | like '%sc -l'. The reason for this is to make it as easy as possible | |
2542 | to process line-oriented shell output via further python commands. |
|
2542 | to process line-oriented shell output via further python commands. | |
2543 | %sc is meant to provide much finer control, but requires more |
|
2543 | %sc is meant to provide much finer control, but requires more | |
2544 | typing. |
|
2544 | typing. | |
2545 |
|
2545 | |||
2546 | 3) Just like %sc -l, this is a list with special attributes: |
|
2546 | 3) Just like %sc -l, this is a list with special attributes: | |
2547 |
|
2547 | |||
2548 | .l (or .list) : value as list. |
|
2548 | .l (or .list) : value as list. | |
2549 | .n (or .nlstr): value as newline-separated string. |
|
2549 | .n (or .nlstr): value as newline-separated string. | |
2550 | .s (or .spstr): value as whitespace-separated string. |
|
2550 | .s (or .spstr): value as whitespace-separated string. | |
2551 |
|
2551 | |||
2552 | This is very useful when trying to use such lists as arguments to |
|
2552 | This is very useful when trying to use such lists as arguments to | |
2553 | system commands.""" |
|
2553 | system commands.""" | |
2554 |
|
2554 | |||
2555 | if parameter_s: |
|
2555 | if parameter_s: | |
2556 | out,err = self.shell.getoutputerror(parameter_s) |
|
2556 | out,err = self.shell.getoutputerror(parameter_s) | |
2557 | if err: |
|
2557 | if err: | |
2558 | print >> Term.cerr,err |
|
2558 | print >> Term.cerr,err | |
2559 | return SList(out.split('\n')) |
|
2559 | return SList(out.split('\n')) | |
2560 |
|
2560 | |||
2561 | def magic_bg(self, parameter_s=''): |
|
2561 | def magic_bg(self, parameter_s=''): | |
2562 | """Run a job in the background, in a separate thread. |
|
2562 | """Run a job in the background, in a separate thread. | |
2563 |
|
2563 | |||
2564 | For example, |
|
2564 | For example, | |
2565 |
|
2565 | |||
2566 | %bg myfunc(x,y,z=1) |
|
2566 | %bg myfunc(x,y,z=1) | |
2567 |
|
2567 | |||
2568 | will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the |
|
2568 | will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the | |
2569 | execution starts, a message will be printed indicating the job |
|
2569 | execution starts, a message will be printed indicating the job | |
2570 | number. If your job number is 5, you can use |
|
2570 | number. If your job number is 5, you can use | |
2571 |
|
2571 | |||
2572 | myvar = jobs.result(5) or myvar = jobs[5].result |
|
2572 | myvar = jobs.result(5) or myvar = jobs[5].result | |
2573 |
|
2573 | |||
2574 | to assign this result to variable 'myvar'. |
|
2574 | to assign this result to variable 'myvar'. | |
2575 |
|
2575 | |||
2576 | IPython has a job manager, accessible via the 'jobs' object. You can |
|
2576 | IPython has a job manager, accessible via the 'jobs' object. You can | |
2577 | type jobs? to get more information about it, and use jobs.<TAB> to see |
|
2577 | type jobs? to get more information about it, and use jobs.<TAB> to see | |
2578 | its attributes. All attributes not starting with an underscore are |
|
2578 | its attributes. All attributes not starting with an underscore are | |
2579 | meant for public use. |
|
2579 | meant for public use. | |
2580 |
|
2580 | |||
2581 | In particular, look at the jobs.new() method, which is used to create |
|
2581 | In particular, look at the jobs.new() method, which is used to create | |
2582 | new jobs. This magic %bg function is just a convenience wrapper |
|
2582 | new jobs. This magic %bg function is just a convenience wrapper | |
2583 | around jobs.new(), for expression-based jobs. If you want to create a |
|
2583 | around jobs.new(), for expression-based jobs. If you want to create a | |
2584 | new job with an explicit function object and arguments, you must call |
|
2584 | new job with an explicit function object and arguments, you must call | |
2585 | jobs.new() directly. |
|
2585 | jobs.new() directly. | |
2586 |
|
2586 | |||
2587 | The jobs.new docstring also describes in detail several important |
|
2587 | The jobs.new docstring also describes in detail several important | |
2588 | caveats associated with a thread-based model for background job |
|
2588 | caveats associated with a thread-based model for background job | |
2589 | execution. Type jobs.new? for details. |
|
2589 | execution. Type jobs.new? for details. | |
2590 |
|
2590 | |||
2591 | You can check the status of all jobs with jobs.status(). |
|
2591 | You can check the status of all jobs with jobs.status(). | |
2592 |
|
2592 | |||
2593 | The jobs variable is set by IPython into the Python builtin namespace. |
|
2593 | The jobs variable is set by IPython into the Python builtin namespace. | |
2594 | If you ever declare a variable named 'jobs', you will shadow this |
|
2594 | If you ever declare a variable named 'jobs', you will shadow this | |
2595 | name. You can either delete your global jobs variable to regain |
|
2595 | name. You can either delete your global jobs variable to regain | |
2596 | access to the job manager, or make a new name and assign it manually |
|
2596 | access to the job manager, or make a new name and assign it manually | |
2597 | to the manager (stored in IPython's namespace). For example, to |
|
2597 | to the manager (stored in IPython's namespace). For example, to | |
2598 | assign the job manager to the Jobs name, use: |
|
2598 | assign the job manager to the Jobs name, use: | |
2599 |
|
2599 | |||
2600 | Jobs = __builtins__.jobs""" |
|
2600 | Jobs = __builtins__.jobs""" | |
2601 |
|
2601 | |||
2602 | self.shell.jobs.new(parameter_s,self.shell.user_ns) |
|
2602 | self.shell.jobs.new(parameter_s,self.shell.user_ns) | |
2603 |
|
2603 | |||
2604 | def magic_store(self, parameter_s=''): |
|
2604 | def magic_store(self, parameter_s=''): | |
2605 | """Lightweight persistence for python variables. |
|
2605 | """Lightweight persistence for python variables. | |
2606 |
|
2606 | |||
2607 | Example: |
|
2607 | Example: | |
2608 |
|
2608 | |||
2609 | ville@badger[~]|1> A = ['hello',10,'world']\\ |
|
2609 | ville@badger[~]|1> A = ['hello',10,'world']\\ | |
2610 | ville@badger[~]|2> %store A\\ |
|
2610 | ville@badger[~]|2> %store A\\ | |
2611 | ville@badger[~]|3> Exit |
|
2611 | ville@badger[~]|3> Exit | |
2612 |
|
2612 | |||
2613 | (IPython session is closed and started again...) |
|
2613 | (IPython session is closed and started again...) | |
2614 |
|
2614 | |||
2615 | ville@badger:~$ ipython -p pysh\\ |
|
2615 | ville@badger:~$ ipython -p pysh\\ | |
2616 | ville@badger[~]|1> print A |
|
2616 | ville@badger[~]|1> print A | |
2617 |
|
2617 | |||
2618 | ['hello', 10, 'world'] |
|
2618 | ['hello', 10, 'world'] | |
2619 |
|
2619 | |||
2620 | Usage: |
|
2620 | Usage: | |
2621 |
|
2621 | |||
2622 | %store - Show list of all variables and their current values\\ |
|
2622 | %store - Show list of all variables and their current values\\ | |
2623 | %store <var> - Store the *current* value of the variable to disk\\ |
|
2623 | %store <var> - Store the *current* value of the variable to disk\\ | |
2624 | %store -d <var> - Remove the variable and its value from storage\\ |
|
2624 | %store -d <var> - Remove the variable and its value from storage\\ | |
2625 | %store -r - Remove all variables from storage |
|
2625 | %store -r - Remove all variables from storage | |
2626 |
|
2626 | |||
2627 | It should be noted that if you change the value of a variable, you |
|
2627 | It should be noted that if you change the value of a variable, you | |
2628 | need to %store it again if you want to persist the new value. |
|
2628 | need to %store it again if you want to persist the new value. | |
2629 |
|
2629 | |||
2630 | Note also that the variables will need to be pickleable; most basic |
|
2630 | Note also that the variables will need to be pickleable; most basic | |
2631 | python types can be safely %stored. |
|
2631 | python types can be safely %stored. | |
2632 | """ |
|
2632 | """ | |
2633 |
|
2633 | |||
2634 | opts,args = self.parse_options(parameter_s,'dr',mode='list') |
|
2634 | opts,args = self.parse_options(parameter_s,'dr',mode='list') | |
2635 | # delete |
|
2635 | # delete | |
2636 | if opts.has_key('d'): |
|
2636 | if opts.has_key('d'): | |
2637 | try: |
|
2637 | try: | |
2638 | todel = args[0] |
|
2638 | todel = args[0] | |
2639 | except IndexError: |
|
2639 | except IndexError: | |
2640 | error('You must provide the variable to forget') |
|
2640 | error('You must provide the variable to forget') | |
2641 | else: |
|
2641 | else: | |
2642 | try: |
|
2642 | try: | |
2643 | del self.shell.persist['S:' + todel] |
|
2643 | del self.shell.persist['S:' + todel] | |
2644 | except: |
|
2644 | except: | |
2645 | error("Can't delete variable '%s'" % todel) |
|
2645 | error("Can't delete variable '%s'" % todel) | |
2646 | # reset |
|
2646 | # reset | |
2647 | elif opts.has_key('r'): |
|
2647 | elif opts.has_key('r'): | |
2648 | for k in self.shell.persist.keys(): |
|
2648 | for k in self.shell.persist.keys(): | |
2649 | if k.startswith('S:'): |
|
2649 | if k.startswith('S:'): | |
2650 | del self.shell.persist[k] |
|
2650 | del self.shell.persist[k] | |
2651 |
|
2651 | |||
2652 | # run without arguments -> list variables & values |
|
2652 | # run without arguments -> list variables & values | |
2653 | elif not args: |
|
2653 | elif not args: | |
2654 | vars = [v[2:] for v in self.shell.persist.keys() |
|
2654 | vars = [v[2:] for v in self.shell.persist.keys() | |
2655 | if v.startswith('S:')] |
|
2655 | if v.startswith('S:')] | |
2656 | vars.sort() |
|
2656 | vars.sort() | |
2657 | if vars: |
|
2657 | if vars: | |
2658 | size = max(map(len,vars)) |
|
2658 | size = max(map(len,vars)) | |
2659 | else: |
|
2659 | else: | |
2660 | size = 0 |
|
2660 | size = 0 | |
2661 |
|
2661 | |||
2662 | print 'Stored variables and their in-memory values:' |
|
2662 | print 'Stored variables and their in-memory values:' | |
2663 | fmt = '%-'+str(size)+'s -> %s' |
|
2663 | fmt = '%-'+str(size)+'s -> %s' | |
2664 | get = self.shell.user_ns.get |
|
2664 | get = self.shell.user_ns.get | |
2665 | for var in vars: |
|
2665 | for var in vars: | |
2666 | # print 30 first characters from every var |
|
2666 | # print 30 first characters from every var | |
2667 | print fmt % (var,repr(get(var,'<unavailable>'))[:50]) |
|
2667 | print fmt % (var,repr(get(var,'<unavailable>'))[:50]) | |
2668 |
|
2668 | |||
2669 | # default action - store the variable |
|
2669 | # default action - store the variable | |
2670 | else: |
|
2670 | else: | |
2671 | pickled = pickle.dumps(self.shell.user_ns[args[0] ]) |
|
2671 | pickled = pickle.dumps(self.shell.user_ns[args[0] ]) | |
2672 | self.shell.persist[ 'S:' + args[0] ] = pickled |
|
2672 | self.shell.persist[ 'S:' + args[0] ] = pickled | |
2673 | print "Stored '%s' (%d bytes)" % (args[0], len(pickled)) |
|
2673 | print "Stored '%s' (%d bytes)" % (args[0], len(pickled)) | |
2674 |
|
2674 | |||
2675 | def magic_bookmark(self, parameter_s=''): |
|
2675 | def magic_bookmark(self, parameter_s=''): | |
2676 | """Manage IPython's bookmark system. |
|
2676 | """Manage IPython's bookmark system. | |
2677 |
|
2677 | |||
2678 | %bookmark <name> - set bookmark to current dir |
|
2678 | %bookmark <name> - set bookmark to current dir | |
2679 | %bookmark <name> <dir> - set bookmark to <dir> |
|
2679 | %bookmark <name> <dir> - set bookmark to <dir> | |
2680 | %bookmark -l - list all bookmarks |
|
2680 | %bookmark -l - list all bookmarks | |
2681 | %bookmark -d <name> - remove bookmark |
|
2681 | %bookmark -d <name> - remove bookmark | |
2682 | %bookmark -r - remove all bookmarks |
|
2682 | %bookmark -r - remove all bookmarks | |
2683 |
|
2683 | |||
2684 | You can later on access a bookmarked folder with: |
|
2684 | You can later on access a bookmarked folder with: | |
2685 | %cd -b <name> |
|
2685 | %cd -b <name> | |
2686 | or simply '%cd <name>' if there is no directory called <name> AND |
|
2686 | or simply '%cd <name>' if there is no directory called <name> AND | |
2687 | there is such a bookmark defined. |
|
2687 | there is such a bookmark defined. | |
2688 |
|
2688 | |||
2689 | Your bookmarks persist through IPython sessions, but they are |
|
2689 | Your bookmarks persist through IPython sessions, but they are | |
2690 | associated with each profile.""" |
|
2690 | associated with each profile.""" | |
2691 |
|
2691 | |||
2692 | opts,args = self.parse_options(parameter_s,'drl',mode='list') |
|
2692 | opts,args = self.parse_options(parameter_s,'drl',mode='list') | |
2693 | if len(args) > 2: |
|
2693 | if len(args) > 2: | |
2694 | error('You can only give at most two arguments') |
|
2694 | error('You can only give at most two arguments') | |
2695 | return |
|
2695 | return | |
2696 |
|
2696 | |||
2697 | bkms = self.shell.persist.get('bookmarks',{}) |
|
2697 | bkms = self.shell.persist.get('bookmarks',{}) | |
2698 |
|
2698 | |||
2699 | if opts.has_key('d'): |
|
2699 | if opts.has_key('d'): | |
2700 | try: |
|
2700 | try: | |
2701 | todel = args[0] |
|
2701 | todel = args[0] | |
2702 | except IndexError: |
|
2702 | except IndexError: | |
2703 | error('You must provide a bookmark to delete') |
|
2703 | error('You must provide a bookmark to delete') | |
2704 | else: |
|
2704 | else: | |
2705 | try: |
|
2705 | try: | |
2706 | del bkms[todel] |
|
2706 | del bkms[todel] | |
2707 | except: |
|
2707 | except: | |
2708 | error("Can't delete bookmark '%s'" % todel) |
|
2708 | error("Can't delete bookmark '%s'" % todel) | |
2709 | elif opts.has_key('r'): |
|
2709 | elif opts.has_key('r'): | |
2710 | bkms = {} |
|
2710 | bkms = {} | |
2711 | elif opts.has_key('l'): |
|
2711 | elif opts.has_key('l'): | |
2712 | bks = bkms.keys() |
|
2712 | bks = bkms.keys() | |
2713 | bks.sort() |
|
2713 | bks.sort() | |
2714 | if bks: |
|
2714 | if bks: | |
2715 | size = max(map(len,bks)) |
|
2715 | size = max(map(len,bks)) | |
2716 | else: |
|
2716 | else: | |
2717 | size = 0 |
|
2717 | size = 0 | |
2718 | fmt = '%-'+str(size)+'s -> %s' |
|
2718 | fmt = '%-'+str(size)+'s -> %s' | |
2719 | print 'Current bookmarks:' |
|
2719 | print 'Current bookmarks:' | |
2720 | for bk in bks: |
|
2720 | for bk in bks: | |
2721 | print fmt % (bk,bkms[bk]) |
|
2721 | print fmt % (bk,bkms[bk]) | |
2722 | else: |
|
2722 | else: | |
2723 | if not args: |
|
2723 | if not args: | |
2724 | error("You must specify the bookmark name") |
|
2724 | error("You must specify the bookmark name") | |
2725 | elif len(args)==1: |
|
2725 | elif len(args)==1: | |
2726 | bkms[args[0]] = os.getcwd() |
|
2726 | bkms[args[0]] = os.getcwd() | |
2727 | elif len(args)==2: |
|
2727 | elif len(args)==2: | |
2728 | bkms[args[0]] = args[1] |
|
2728 | bkms[args[0]] = args[1] | |
2729 | self.shell.persist['bookmarks'] = bkms |
|
2729 | self.shell.persist['bookmarks'] = bkms | |
2730 |
|
2730 | |||
2731 | def magic_pycat(self, parameter_s=''): |
|
2731 | def magic_pycat(self, parameter_s=''): | |
2732 | """Show a syntax-highlighted file through a pager. |
|
2732 | """Show a syntax-highlighted file through a pager. | |
2733 |
|
2733 | |||
2734 | This magic is similar to the cat utility, but it will assume the file |
|
2734 | This magic is similar to the cat utility, but it will assume the file | |
2735 | to be Python source and will show it with syntax highlighting. """ |
|
2735 | to be Python source and will show it with syntax highlighting. """ | |
2736 |
|
2736 | |||
2737 | filename = get_py_filename(parameter_s) |
|
2737 | filename = get_py_filename(parameter_s) | |
2738 | page(self.shell.colorize(file_read(filename)), |
|
2738 | page(self.shell.pycolorize(file_read(filename)), | |
2739 | screen_lines=self.shell.rc.screen_length) |
|
2739 | screen_lines=self.shell.rc.screen_length) | |
2740 |
|
2740 | |||
2741 | # end Magic |
|
2741 | # end Magic |
General Comments 0
You need to be logged in to leave comments.
Login now