##// END OF EJS Templates
- Add autocall 'smart' mode....
fperez -
r84:53b45345
parent child
Show More
@@ -1,7 +1,7
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 988 2006-01-02 21:21:47Z fperez $"""
4 $Id: Magic.py 990 2006-01-04 06:59:02Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -424,11 +424,12 Currently the magic system has the following functions:\n"""
424 def magic_autocall(self, parameter_s = ''):
424 def magic_autocall(self, parameter_s = ''):
425 """Make functions callable without having to type parentheses.
425 """Make functions callable without having to type parentheses.
426
426
427 This toggles the autocall command line option on and off."""
427 This cycles the autocall command line through its three valid values
428 (0->Off, 1->Smart, 2->Full)"""
428
429
429 rc = self.shell.rc
430 rc = self.shell.rc
430 rc.autocall = not rc.autocall
431 rc.autocall = not rc.autocall
431 print "Automatic calling is:",['OFF','ON'][rc.autocall]
432 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
432
433
433 def magic_autoindent(self, parameter_s = ''):
434 def magic_autoindent(self, parameter_s = ''):
434 """Toggle autoindent on/off (if available)."""
435 """Toggle autoindent on/off (if available)."""
@@ -1,5 +1,5
1 # -*- Mode: Shell-Script -*- Not really, but shows comments correctly
1 # -*- Mode: Shell-Script -*- Not really, but shows comments correctly
2 # $Id: ipythonrc 963 2005-12-28 19:21:29Z fperez $
2 # $Id: ipythonrc 990 2006-01-04 06:59:02Z fperez $
3
3
4 #***************************************************************************
4 #***************************************************************************
5 #
5 #
@@ -37,8 +37,9 include
37 # is encountered more than once, the last value remains, all earlier ones get
37 # is encountered more than once, the last value remains, all earlier ones get
38 # discarded.
38 # discarded.
39
39
40 # Automatic calling of callable objects. If set to true, callable objects are
40
41 # automatically called when invoked at the command line, even if you don't
41 # Automatic calling of callable objects. If set to 1 or 2, callable objects
42 # are automatically called when invoked at the command line, even if you don't
42 # type parentheses. IPython adds the parentheses for you. For example:
43 # type parentheses. IPython adds the parentheses for you. For example:
43
44
44 #In [1]: str 45
45 #In [1]: str 45
@@ -48,7 +49,28 include
48 # IPython reprints your line with '---->' indicating that it added
49 # IPython reprints your line with '---->' indicating that it added
49 # parentheses. While this option is very convenient for interactive use, it
50 # parentheses. While this option is very convenient for interactive use, it
50 # may occasionally cause problems with objects which have side-effects if
51 # may occasionally cause problems with objects which have side-effects if
51 # called unexpectedly. Set it to 0 if you want to disable it.
52 # called unexpectedly.
53
54 # The valid values for autocall are:
55
56 # autocall 0 -> disabled (you can toggle it at runtime with the %autocall magic)
57
58 # autocall 1 -> active, but do not apply if there are no arguments on the line.
59
60 # In this mode, you get:
61
62 #In [1]: callable
63 #Out[1]: <built-in function callable>
64
65 #In [2]: callable 'hello'
66 #------> callable('hello')
67 #Out[2]: False
68
69 # 2 -> Active always. Even if no arguments are present, the callable object
70 # is called:
71
72 #In [4]: callable
73 #------> callable()
52
74
53 # Note that even with autocall off, you can still use '/' at the start of a
75 # Note that even with autocall off, you can still use '/' at the start of a
54 # line to treat the first argument on the command line as a function and add
76 # line to treat the first argument on the command line as a function and add
@@ -73,6 +73,13 import readline
73 import sys
73 import sys
74 import types
74 import types
75
75
76 # Python 2.4 offers sets as a builtin
77 try:
78 set([1,2])
79 except NameError:
80 from sets import Set as set
81
82
76 from IPython.genutils import shlex_split
83 from IPython.genutils import shlex_split
77
84
78 __all__ = ['Completer','IPCompleter']
85 __all__ = ['Completer','IPCompleter']
@@ -192,11 +199,21 class Completer:
192 except:
199 except:
193 object = eval(expr, self.global_namespace)
200 object = eval(expr, self.global_namespace)
194
201
202 # Start building the attribute list via dir(), and then complete it
203 # with a few extra special-purpose calls.
195 words = dir(object)
204 words = dir(object)
205
196 if hasattr(object,'__class__'):
206 if hasattr(object,'__class__'):
197 words.append('__class__')
207 words.append('__class__')
198 words.extend(get_class_members(object.__class__))
208 words.extend(get_class_members(object.__class__))
199
209
210 # this is the 'dir' function for objects with Enthought's traits
211 if hasattr(object, 'trait_names'):
212 words.extend(object.trait_names())
213 # eliminate possible duplicates, as some traits may also appear as
214 # normal attributes in the dir() call.
215 words = set(words)
216
200 # filter out non-string attributes which may be stuffed by dir() calls
217 # filter out non-string attributes which may be stuffed by dir() calls
201 # and poor coding in third-party modules
218 # and poor coding in third-party modules
202 words = [w for w in words
219 words = [w for w in words
@@ -5,7 +5,7 General purpose utilities.
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 980 2005-12-30 15:42:04Z fperez $"""
8 $Id: genutils.py 990 2006-01-04 06:59:02Z fperez $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -207,6 +207,18 def fatal(msg,exit_val=1):
207
207
208 warn(msg,exit_val=exit_val,level=4)
208 warn(msg,exit_val=exit_val,level=4)
209
209
210
211 # useful for debugging
212 def debugp(expr):
213 """Print the value of an expression from the caller's frame.
214
215 Takes an expression, evaluates it in the caller's frame and prints both
216 the given expression and the resulting value. The input must be of a form
217 suitable for eval()."""
218
219 cf = sys._getframe(1)
220 print '[DBG] %s -> %r' % (expr,eval(expr,cf.f_globals,cf.f_locals))
221
210 #----------------------------------------------------------------------------
222 #----------------------------------------------------------------------------
211 StringTypes = types.StringTypes
223 StringTypes = types.StringTypes
212
224
@@ -6,7 +6,7 Requires Python 2.1 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 988 2006-01-02 21:21:47Z fperez $
9 $Id: iplib.py 990 2006-01-04 06:59:02Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -472,7 +472,7 class InteractiveShell(object,Magic):
472 # RegExp to identify potential function names
472 # RegExp to identify potential function names
473 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
473 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
474 # RegExp to exclude strings with this start from autocalling
474 # RegExp to exclude strings with this start from autocalling
475 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
475 self.re_exclude_auto = re.compile('^[!=()\[\]<>,\*/\+-]|^is ')
476
476
477 # try to catch also methods for stuff in lists/tuples/dicts: off
477 # try to catch also methods for stuff in lists/tuples/dicts: off
478 # (experimental). For this to work, the line_split regexp would need
478 # (experimental). For this to work, the line_split regexp would need
@@ -1836,7 +1836,8 want to merge them back into the new files.""" % locals()
1836 self.re_fun_name.match(iFun) and \
1836 self.re_fun_name.match(iFun) and \
1837 callable(oinfo['obj']) :
1837 callable(oinfo['obj']) :
1838 #print 'going auto' # dbg
1838 #print 'going auto' # dbg
1839 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1839 return self.handle_auto(line,continue_prompt,
1840 pre,iFun,theRest,oinfo['obj'])
1840 else:
1841 else:
1841 #print 'was callable?', callable(oinfo['obj']) # dbg
1842 #print 'was callable?', callable(oinfo['obj']) # dbg
1842 return self.handle_normal(line,continue_prompt)
1843 return self.handle_normal(line,continue_prompt)
@@ -1919,15 +1920,17 want to merge them back into the new files.""" % locals()
1919 return cmd
1920 return cmd
1920
1921
1921 def handle_auto(self, line, continue_prompt=None,
1922 def handle_auto(self, line, continue_prompt=None,
1922 pre=None,iFun=None,theRest=None):
1923 pre=None,iFun=None,theRest=None,obj=None):
1923 """Hande lines which can be auto-executed, quoting if requested."""
1924 """Hande lines which can be auto-executed, quoting if requested."""
1924
1925
1925 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1926 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1926
1927
1927 # This should only be active for single-line input!
1928 # This should only be active for single-line input!
1928 if continue_prompt:
1929 if continue_prompt:
1930 self.log(line,continue_prompt)
1929 return line
1931 return line
1930
1932
1933 auto_rewrite = True
1931 if pre == self.ESC_QUOTE:
1934 if pre == self.ESC_QUOTE:
1932 # Auto-quote splitting on whitespace
1935 # Auto-quote splitting on whitespace
1933 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1936 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
@@ -1935,19 +1938,31 want to merge them back into the new files.""" % locals()
1935 # Auto-quote whole string
1938 # Auto-quote whole string
1936 newcmd = '%s("%s")' % (iFun,theRest)
1939 newcmd = '%s("%s")' % (iFun,theRest)
1937 else:
1940 else:
1938 # Auto-paren
1941 # Auto-paren.
1939 if theRest[0:1] in ('=','['):
1942 # We only apply it to argument-less calls if the autocall
1940 # Don't autocall in these cases. They can be either
1943 # parameter is set to 2. We only need to check that autocall is <
1941 # rebindings of an existing callable's name, or item access
1944 # 2, since this function isn't called unless it's at least 1.
1942 # for an object which is BOTH callable and implements
1945 if not theRest and self.rc.autocall < 2:
1943 # __getitem__.
1946 newcmd = '%s %s' % (iFun,theRest)
1944 return '%s %s' % (iFun,theRest)
1947 auto_rewrite = False
1945 if theRest.endswith(';'):
1946 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1947 else:
1948 else:
1948 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1949 if theRest.startswith('['):
1950 if hasattr(obj,'__getitem__'):
1951 # Don't autocall in this case: item access for an object
1952 # which is BOTH callable and implements __getitem__.
1953 newcmd = '%s %s' % (iFun,theRest)
1954 auto_rewrite = False
1955 else:
1956 # if the object doesn't support [] access, go ahead and
1957 # autocall
1958 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1959 elif theRest.endswith(';'):
1960 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1961 else:
1962 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1949
1963
1950 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1964 if auto_rewrite:
1965 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1951 # log what is now valid Python, not the actual user input (without the
1966 # log what is now valid Python, not the actual user input (without the
1952 # final newline)
1967 # final newline)
1953 self.log(newcmd,continue_prompt)
1968 self.log(newcmd,continue_prompt)
@@ -6,7 +6,7 Requires Python 2.1 or better.
6
6
7 This file contains the main make_IPython() starter function.
7 This file contains the main make_IPython() starter function.
8
8
9 $Id: ipmaker.py 967 2005-12-29 09:02:13Z fperez $"""
9 $Id: ipmaker.py 990 2006-01-04 06:59:02Z fperez $"""
10
10
11 #*****************************************************************************
11 #*****************************************************************************
12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -147,7 +147,7 object? -> Details about 'object'. ?object also works, ?? prints more.
147 # DPyGetOpt syntax (=s,=i,:f,etc).
147 # DPyGetOpt syntax (=s,=i,:f,etc).
148
148
149 # Make sure there's a space before each end of line (they get auto-joined!)
149 # Make sure there's a space before each end of line (they get auto-joined!)
150 cmdline_opts = ('autocall! autoindent! automagic! banner! cache_size|cs=i '
150 cmdline_opts = ('autocall=i autoindent! automagic! banner! cache_size|cs=i '
151 'c=s classic|cl color_info! colors=s confirm_exit! '
151 'c=s classic|cl color_info! colors=s confirm_exit! '
152 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
152 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
153 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
153 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
@@ -6,7 +6,7
6 # the file COPYING, distributed as part of this software.
6 # the file COPYING, distributed as part of this software.
7 #*****************************************************************************
7 #*****************************************************************************
8
8
9 # $Id: usage.py 966 2005-12-29 08:34:07Z fperez $
9 # $Id: usage.py 990 2006-01-04 06:59:02Z fperez $
10
10
11 from IPython import Release
11 from IPython import Release
12 __author__ = '%s <%s>' % Release.authors['Fernando']
12 __author__ = '%s <%s>' % Release.authors['Fernando']
@@ -144,10 +144,14 REGULAR OPTIONS
144 to correctly execute (without blocking) any matplotlib-based
144 to correctly execute (without blocking) any matplotlib-based
145 script which calls show() at the end.
145 script which calls show() at the end.
146
146
147 -[no]autocall
147 -autocall <val>
148 Make IPython automatically call any callable object even if you
148 Make IPython automatically call any callable object even if you
149 didnt type explicit parentheses. For example, ’str 43’ becomes
149 didn't type explicit parentheses. For example, 'str 43' becomes
150 ’str(43)’ automatically.
150 'str(43)' automatically. The value can be '0' to disable the
151 feature, '1' for 'smart' autocall, where it is not applied if
152 there are no more arguments on the line, and '2' for 'full'
153 autocall, where all callable objects are automatically called
154 (even if no arguments are present). The default is '1'.
151
155
152 -[no]autoindent
156 -[no]autoindent
153 Turn automatic indentation on/off.
157 Turn automatic indentation on/off.
@@ -1,3 +1,17
1 2006-01-03 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/iplib.py (handle_auto): changed autocall semantics to
4 include 'smart' mode, where the autocall transformation is NOT
5 applied if there are no arguments on the line. This allows you to
6 just type 'foo' if foo is a callable to see its internal form,
7 instead of having it called with no arguments (typically a
8 mistake). The old 'full' autocall still exists: for that, you
9 need to set the 'autocall' parameter to 2 in your ipythonrc file.
10
11 * IPython/completer.py (Completer.attr_matches): add
12 tab-completion support for Enthoughts' traits. After a report by
13 Arnd and a patch by Prabhu.
14
1 2006-01-02 Fernando Perez <Fernando.Perez@colorado.edu>
15 2006-01-02 Fernando Perez <Fernando.Perez@colorado.edu>
2
16
3 * IPython/ultraTB.py (_fixed_getinnerframes): added Alex
17 * IPython/ultraTB.py (_fixed_getinnerframes): added Alex
@@ -115,9 +115,14 All options with a [no] prepended can be specified in negated form
115 .B \-h, \-\-help
115 .B \-h, \-\-help
116 Show summary of options.
116 Show summary of options.
117 .TP
117 .TP
118 .B \-[no]autocall
118 .B \-autocall <val>
119 Make IPython automatically call any callable object even if you didn't type
119 Make IPython automatically call any callable object even if you didn't type
120 explicit parentheses. For example, 'str 43' becomes 'str(43)' automatically.
120 explicit parentheses. For example, 'str 43' becomes
121 'str(43)' automatically. The value can be '0' to disable the
122 feature, '1' for 'smart' autocall, where it is not applied if
123 there are no more arguments on the line, and '2' for 'full'
124 autocall, where all callable objects are automatically called
125 (even if no arguments are present). The default is '1'.
121 .TP
126 .TP
122 .B \-[no]autoindent
127 .B \-[no]autoindent
123 Turn automatic indentation on/off.
128 Turn automatic indentation on/off.
@@ -2686,12 +2686,24 show()
2686
2686
2687 \family typewriter
2687 \family typewriter
2688 \series bold
2688 \series bold
2689 -[no]autocall:
2689 -autocall <val>:
2690 \family default
2690 \family default
2691 \series default
2691 \series default
2692 Make IPython automatically call any callable object even if you didn't
2692 Make IPython automatically call any callable object even if you didn't
2693 type explicit parentheses.
2693 type explicit parentheses.
2694 For example, `str 43' becomes `str(43)' automatically.
2694 For example, `str 43' becomes `str(43)' automatically.
2695 The value can be `0' to disable the feature, `1' for
2696 \emph on
2697 smart
2698 \emph default
2699 autocall, where it is not applied if there are no more arguments on the
2700 line, and `2' for
2701 \emph on
2702 full
2703 \emph default
2704 autocall, where all callable objects are automatically called (even if
2705 no arguments are present).
2706 The default is `1'.
2695 \layout List
2707 \layout List
2696 \labelwidthstring 00.00.0000
2708 \labelwidthstring 00.00.0000
2697
2709
General Comments 0
You need to be logged in to leave comments. Login now