[IPython-User] $ sign is removed in magic function

Zoltán Vörös zvoros@gmail....
Sun Jan 1 16:09:58 CST 2012


Hi All,

I have an embarrassing problem, and I was wondering whether someone 
could point out where the error is. In short, I wanted to implement a 
primitive parser for importing data from a file. What I mean is that 
most of the time I need one or two columns, or perhaps some functions of 
columns from a file. I wanted to use the $ sign to indicate which column 
I want. And for all this, I was thinking of using a magic function.

So, something like this

    %fimp test.dat x $3


which should load the third column of the file 'test.dat' into the 
variable 'x'. And before someone asks why I need $3 in the first place, 
the answer is that I want to be able to load

    %fimp test.dat x $3*$3


which should be different to

    %fimp test.dat x $3*3


OK. So, I tried to implement this as a magic function, and I have this

    ip = get_ipython()
    def dofimp(self, arg):
         '''    Fast import of data files. This function also allows for
    conversion of columns
         '''
         if len(arg) > 0:
             opts, arg_lst = ip.parse_options(arg, '', mode='list')
             print arg_lst

    ip.define_magic('fimp', dofimp)

If I run this in an ipython shell as

    In [24]: %fimp foo.dat $123
    [u'foo.dat', u'123']


i.e., the $ sign is removed. However, if I do this on the command line

    a, b = ip.parse_options('foo ($123)', '', mode='list')
    print b
    ['foo', '($123)']


i.e., the $ is there. (And this is what I was after.) I don't see why 
the same two lines of code produce differing results. Any hints? This is 
ipython 0.12.
Cheers,
Zoltán

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20120101/2a6237e7/attachment.html 


More information about the IPython-User mailing list