[IPython-user] customized magic function limitation
belinda thom
bthom at cs.hmc.edu
Wed Jan 10 22:34:49 CST 2007
Hi,
Is there a way to write a customized magic function that takes
multiple arguments?
I've tried various things, as enumerated below.
I'd like to create a 2 arg function that can do "import ... as ..."
for me.
As always, thanks,
--b
Using only one argument, everything's great:
In [9]: %t1??
Type: Magic function
[snip]
Definition: %t1(self, arg)
Source:
def dotest1(self, arg):
"""
tests taking in one arg
"""
self.api.ex("print %s" % arg)
In [10]: %t1 'hi'
hi
But w/two, things fall apart:
In [3]: %t2??
Type: Magic function
[snip]
Definition: %t2(self, arg1, arg2)
Source:
def dotest2(self, arg1, arg2):
"""
tests two args
"""
self.api.ex("print %s, %s" % (arg1,arg2))
In [4]: %t2 'boo' 'hoo'
------------------------------------------------------------------------
---
exceptions.TypeError Traceback (most
recent call last)
[snip]
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/IPython/iplib.py in ipmagic(self, arg_s)
933 else:
934 magic_args = self.var_expand(magic_args,1)
--> 935 return fn(magic_args)
936
937 def ipalias(self,arg_s):
TypeError: dotest2() takes exactly 3 arguments (2 given)
In [6]: %t2 'boo','hoo'
------------------------------------------------------------------------
---
[snip]
TypeError: dotest2() takes exactly 3 arguments (2 given)
In [7]: %t3??
Type: Magic function
[snip]
def dotest3(self, pair):
"""
tests taking tuple arg
"""
self.api.ex("print %s, %s" % pair)
In [8]: %t3 ('boo','hoo')
------------------------------------------------------------------------
---
exceptions.TypeError Traceback (most
recent call last)
[snip]
TypeError: not enough arguments for format string
More information about the IPython-user
mailing list