##// END OF EJS Templates
implement callable (i.e. straight python) aliases and _sh shadow namespace
vivainio -
Show More
@@ -0,0 +1,1 b''
1 """ Shadow namespace """ No newline at end of file
@@ -6,7 +6,7 b' Uses syntax highlighting for presenting the various information elements.'
6 6 Similar in spirit to the inspect module, but all calls take a name argument to
7 7 reference the name under which an object is being read.
8 8
9 $Id: OInspect.py 1850 2006-10-28 19:48:13Z fptest $
9 $Id: OInspect.py 2463 2007-06-27 22:51:16Z vivainio $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -337,7 +337,10 b' class Inspector:'
337 337 ospace = info.namespace
338 338 # Get docstring, special-casing aliases:
339 339 if isalias:
340 ds = "Alias to the system command:\n %s" % obj[1]
340 if not callable(obj):
341 ds = "Alias to the system command:\n %s" % obj[1]
342 else:
343 ds = "Alias to " + str(obj)
341 344 else:
342 345 ds = getdoc(obj)
343 346 if ds is None:
@@ -335,6 +335,12 b' class IPApi:'
335 335 Creates a new alias named 'bb' in ipython user namespace
336 336 """
337 337
338 if callable(cmd):
339 self.IP.alias_table[name] = cmd
340 import IPython.shawodns
341 setattr(IPython.shadowns, name,cmd)
342 return
343
338 344
339 345 nargs = cmd.count('%s')
340 346 if nargs>0 and cmd.find('%l')>=0:
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 2442 2007-06-14 21:20:10Z vivainio $
9 $Id: iplib.py 2463 2007-06-27 22:51:16Z vivainio $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -76,7 +76,7 b' from IPython.strdispatch import StrDispatch'
76 76 import IPython.ipapi
77 77 import IPython.history
78 78 import IPython.prefilter as prefilter
79
79 import IPython.shadowns
80 80 # Globals
81 81
82 82 # store the builtin raw_input globally, and use this always, in case user code
@@ -379,6 +379,7 b' class InteractiveShell(object,Magic):'
379 379 self.user_ns['In'] = self.input_hist
380 380 self.user_ns['Out'] = self.output_hist
381 381
382 self.user_ns['_sh'] = IPython.shadowns
382 383 # Object variable to store code object waiting execution. This is
383 384 # used mainly by the multithreaded shells, but it can come in handy in
384 385 # other situations. No need to use a Queue here, since it's a single
@@ -2119,13 +2120,18 b' want to merge them back into the new files.""" % locals()'
2119 2120 return line
2120 2121
2121 2122 def handle_alias(self,line_info):
2122 """Handle alias input lines. """
2123 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2123 """Handle alias input lines. """
2124 tgt = self.alias_table[line_info.iFun]
2125 # print "=>",tgt #dbg
2126 if callable(tgt):
2127 line_out = "_sh." + line_info.iFun + '(r"""' + line_info.theRest + '""")'
2128 else:
2129 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2124 2130
2125 # pre is needed, because it carries the leading whitespace. Otherwise
2126 # aliases won't work in indented sections.
2127 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2128 make_quoted_expr( transformed ))
2131 # pre is needed, because it carries the leading whitespace. Otherwise
2132 # aliases won't work in indented sections.
2133 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2134 make_quoted_expr( transformed ))
2129 2135
2130 2136 self.log(line_info.line,line_out,line_info.continue_prompt)
2131 2137 #print 'line out:',line_out # dbg
@@ -1,3 +1,17 b''
1 2007-06-28 Ville Vainio <vivainio@gmail.com>
2
3 * shadowns.py, iplib.py, ipapi.py, OInspect.py:
4 Implement "shadow" namespace, and callable aliases that reside there.
5 Use them by:
6
7 _ip.defalias('foo',myfunc) # creates _sh.foo that points to myfunc
8
9 foo hello world
10 (gets translated to:)
11 _sh.foo(r"""hello world""")
12
13 In practice, this kind of alias can take the role of a magic function
14
1 15 2007-06-14 Ville Vainio <vivainio@gmail.com>
2 16
3 17 * iplib.py (handle_auto): Try to use ascii for printing "--->"
General Comments 0
You need to be logged in to leave comments. Login now