Show More
@@ -1,7 +1,7 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 1 |
|
4 | $Id: Magic.py 1815 2006-10-10 04:46:24Z fptest $""" | |
5 |
|
5 | |||
6 | #***************************************************************************** |
|
6 | #***************************************************************************** | |
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
@@ -62,6 +62,17 b' def on_off(tag):' | |||||
62 |
|
62 | |||
63 | class Bunch: pass |
|
63 | class Bunch: pass | |
64 |
|
64 | |||
|
65 | def arg_split(s,posix=True): | |||
|
66 | """Split a command line's arguments in a shell-like manner. | |||
|
67 | ||||
|
68 | This is a modified version of the standard library's shlex.split() | |||
|
69 | function, but with a default of posix=False for splitting, so that quotes | |||
|
70 | in inputs are respected.""" | |||
|
71 | ||||
|
72 | lex = shlex.shlex(s, posix=posix) | |||
|
73 | lex.whitespace_split = True | |||
|
74 | return list(lex) | |||
|
75 | ||||
65 | #*************************************************************************** |
|
76 | #*************************************************************************** | |
66 | # Main class implementing Magic functionality |
|
77 | # Main class implementing Magic functionality | |
67 | class Magic: |
|
78 | class Magic: | |
@@ -301,8 +312,12 b' license. To use profiling, please install"python2.3-profiler" from non-free.""")' | |||||
301 | returned as a list (split on whitespace) instead of a string. |
|
312 | returned as a list (split on whitespace) instead of a string. | |
302 |
|
313 | |||
303 | -list_all: put all option values in lists. Normally only options |
|
314 | -list_all: put all option values in lists. Normally only options | |
304 |
appearing more than once are put in a list. |
|
315 | appearing more than once are put in a list. | |
305 |
|
|
316 | ||
|
317 | -posix (True): whether to split the input line in POSIX mode or not, | |||
|
318 | as per the conventions outlined in the shlex module from the | |||
|
319 | standard library.""" | |||
|
320 | ||||
306 | # inject default options at the beginning of the input line |
|
321 | # inject default options at the beginning of the input line | |
307 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') |
|
322 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') | |
308 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
323 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) | |
@@ -312,6 +327,7 b' license. To use profiling, please install"python2.3-profiler" from non-free.""")' | |||||
312 | raise ValueError,'incorrect mode given: %s' % mode |
|
327 | raise ValueError,'incorrect mode given: %s' % mode | |
313 | # Get options |
|
328 | # Get options | |
314 | list_all = kw.get('list_all',0) |
|
329 | list_all = kw.get('list_all',0) | |
|
330 | posix = kw.get('posix',True) | |||
315 |
|
331 | |||
316 | # Check if we have more than one argument to warrant extra processing: |
|
332 | # Check if we have more than one argument to warrant extra processing: | |
317 | odict = {} # Dictionary with options |
|
333 | odict = {} # Dictionary with options | |
@@ -319,7 +335,7 b' license. To use profiling, please install"python2.3-profiler" from non-free.""")' | |||||
319 | if len(args) >= 1: |
|
335 | if len(args) >= 1: | |
320 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
336 | # If the list of inputs only has 0 or 1 thing in it, there's no | |
321 | # need to look for options |
|
337 | # need to look for options | |
322 |
argv = |
|
338 | argv = arg_split(arg_str,posix) | |
323 | # Do regular option processing |
|
339 | # Do regular option processing | |
324 | try: |
|
340 | try: | |
325 | opts,args = getopt(argv,opt_str,*long_opts) |
|
341 | opts,args = getopt(argv,opt_str,*long_opts) | |
@@ -1636,19 +1652,22 b' Currently the magic system has the following functions:\\n"""' | |||||
1636 | 1 loops, best of 3: 2 s per loop |
|
1652 | 1 loops, best of 3: 2 s per loop | |
1637 |
|
1653 | |||
1638 |
|
1654 | |||
1639 |
The times reported by %timeit will be slightly higher than those |
|
1655 | The times reported by %timeit will be slightly higher than those | |
1640 |
by the timeit.py script when variables are accessed. This is |
|
1656 | reported by the timeit.py script when variables are accessed. This is | |
1641 |
fact that %timeit executes the statement in the namespace |
|
1657 | due to the fact that %timeit executes the statement in the namespace | |
1642 |
compared with timeit.py, which uses a single setup |
|
1658 | of the shell, compared with timeit.py, which uses a single setup | |
1643 |
function or create variables. Generally, the bias |
|
1659 | statement to import function or create variables. Generally, the bias | |
1644 |
as results from timeit.py are not mixed with |
|
1660 | does not matter as long as results from timeit.py are not mixed with | |
|
1661 | those from %timeit.""" | |||
|
1662 | ||||
1645 | import timeit |
|
1663 | import timeit | |
1646 | import math |
|
1664 | import math | |
1647 |
|
1665 | |||
1648 | units = ["s", "ms", "\xc2\xb5s", "ns"] |
|
1666 | units = ["s", "ms", "\xc2\xb5s", "ns"] | |
1649 | scaling = [1, 1e3, 1e6, 1e9] |
|
1667 | scaling = [1, 1e3, 1e6, 1e9] | |
1650 |
|
1668 | |||
1651 |
opts, stmt = self.parse_options(parameter_s,'n:r:tcp:' |
|
1669 | opts, stmt = self.parse_options(parameter_s,'n:r:tcp:', | |
|
1670 | posix=False) | |||
1652 | if stmt == "": |
|
1671 | if stmt == "": | |
1653 | return |
|
1672 | return | |
1654 | timefunc = timeit.default_timer |
|
1673 | timefunc = timeit.default_timer | |
@@ -1665,7 +1684,8 b' Currently the magic system has the following functions:\\n"""' | |||||
1665 | # but is there a better way to achieve that the code stmt has access |
|
1684 | # but is there a better way to achieve that the code stmt has access | |
1666 | # to the shell namespace? |
|
1685 | # to the shell namespace? | |
1667 |
|
1686 | |||
1668 |
src = timeit.template % {'stmt': timeit.reindent(stmt, 8), |
|
1687 | src = timeit.template % {'stmt': timeit.reindent(stmt, 8), | |
|
1688 | 'setup': "pass"} | |||
1669 | code = compile(src, "<magic-timeit>", "exec") |
|
1689 | code = compile(src, "<magic-timeit>", "exec") | |
1670 | ns = {} |
|
1690 | ns = {} | |
1671 | exec code in self.shell.user_ns, ns |
|
1691 | exec code in self.shell.user_ns, ns |
@@ -1,3 +1,10 b'' | |||||
|
1 | 2006-10-09 Fernando Perez <Fernando.Perez@colorado.edu> | |||
|
2 | ||||
|
3 | * IPython/Magic.py (Magic.parse_options): add a new posix option | |||
|
4 | to allow parsing of input args in magics that doesn't strip quotes | |||
|
5 | (if posix=False). This also closes %timeit bug reported by | |||
|
6 | Stefan. | |||
|
7 | ||||
1 | 2006-10-03 Ville Vainio <vivainio@gmail.com> |
|
8 | 2006-10-03 Ville Vainio <vivainio@gmail.com> | |
2 |
|
9 | |||
3 | * iplib.py (raw_input, interact): Return ValueError catching for |
|
10 | * iplib.py (raw_input, interact): Return ValueError catching for |
General Comments 0
You need to be logged in to leave comments.
Login now