[IPython-user] customized magic function limitation
Ville M. Vainio
vivainio at gmail.com
Thu Jan 11 10:05:06 CST 2007
On 1/11/07, belinda thom <bthom at cs.hmc.edu> wrote:
> def dtest(self, arg):
> self.api.ex("print %s, %s" % (arg.split().pop(0),arg.split().pop
a1, a2 = arg.split()
is a bit cleaner...
> In [8]: %t2 "hi there"
> hi, there
>
> It appears that w/two args, you must enclose all w/in matching string
> markers ("" or ''), which makes sense given that's how Python
> recognizes strings.
Note that you forgot quotes after print. You don't need quotes for
multiple args. Try printing the created command before .ex:ing it.
You wanted to say:
'print "%s, %s" % (a1,a2)'
> def doimp(self, arg):
> """
> does (another) import / reload sequence of file arg.
> """
> self.api.ex("import %s; reload(%s)" % (arg,arg))
>
> In [2]: %imp dice
This imports a module named "dice" and succeeds.
> Without import, I get a naming error:
>
> 94 % ipython
> <snip>
> IPython 0.7.4.svn.r2010 -- An enhanced Interactive Python.
> <snip>
> In [1]: %t1??
> Type: Magic function
> <snip>
> Definition: %t1(self, arg)
> Source:
> def dtest(self, arg):
> self.api.ex("print %s" % arg)
>
> In [2]: %t1 dice
> ------------------------------------------------------------------------
> ---
> exceptions.NameError Traceback (most
> recent call last)
> /Users/bthom/belinda/mills/aicourse/python_exs/<ipython console>
> <snip>
> /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
> packages/IPython/ipapi.py in ex(self, cmd)
> 210 def ex(self,cmd):
> 211 """ Execute a normal python statement in user
> namespace """
> --> 212 exec cmd in self.user_ns
> 213
> 214 def ev(self,expr):
> /Users/bthom/belinda/mills/aicourse/python_exs/<string>
> NameError: name 'dice' is not defined
This is because the name 'dice' is not defined, which is expected.
IPython only does $var expansion to the strings before passing them to
magic functions.
--
Ville M. Vainio - vivainio.googlepages.com
blog=360.yahoo.com/villevainio - g[mail | talk]='vivainio'
More information about the IPython-user
mailing list