[IPython-dev] magics in blocks (was Wish list updated)

Frédéric Mantegazza mantegazza at ill.fr
Wed Apr 6 10:09:03 EDT 2005


Le Mercredi 6 Avril 2005 13:13, Fernando Perez a écrit :

> Please update CVS and give things a try.  Magics can now be used in
> multiline statements (with or wihtout leading %, depending on your
> %automagic settings). Furthermore, magic calls now honor the same
> variable expansion mechanism which aliases and system calls use:

:o)

> Here's a trivial example:
>
> In [17]: for i in range(3):
>     ....:     mkdir $i
>     ....:     !touch $i/hello
>     ....:     ls -l $i
>     ....:
> total 0
> -rw-r--r--  1 fperez wavelet 0 Apr  6 05:10 hello
> total 0
> -rw-r--r--  1 fperez wavelet 0 Apr  6 05:10 hello
> total 0
> -rw-r--r--  1 fperez wavelet 0 Apr  6 05:10 hello

But I can't make it work this way. It only works if I use ipmagic() 
function...

Here is some examples of the problems without ipmagic(). Note examples 17 
and 18. 'pr' is one of our magic. The behaviour is different than magic 
'cd': when a param follow 'pr', the error occurs just after I hit enter. 
Without param, the error occurs after the blank line. 

I may have a bad config or so...

In [13]:cd
/home/fma

In [14]:for i in xrange(3):
   ....:    cd
   ....:
---------------------------------------------------------------------------
exceptions.NameError                      Traceback (most recent call last)

In [15]:for i in xrange(3):
   ...:    cd /tmp
   ...:
---------------------------------------------------------------------------
exceptions.NameError                      Traceback (most recent call last)

/home/fma/develop/PyMAD/client/ipython/<console>

NameError: name 'cd' is not defined

In [16]:for i in xrange(3):
   ....:    %cd /tmp
------------------------------------------------------------
   File "<console>", line 2
     %cd /tmp
     ^
SyntaxError: invalid syntax

In [17]:for i in range(3):
   ....:    pr
   ....:
---------------------------------------------------------------------------
exceptions.NameError                      Traceback (most recent call last)

/home/fma/develop/PyMAD/client/ipython/<console>

NameError: name 'pr' is not defined

In [18]:for i in range(3):
   ....:    pr a1
------------------------------------------------------------
   File "<console>", line 2
     pr a1
         ^
SyntaxError: invalid syntax

In [18]:for i in range(3):
   ....:    %pr a1
------------------------------------------------------------
   File "<console>", line 2
     %pr a1
     ^
SyntaxError: invalid syntax

In [19]:for i in xrange(3):
   ....:    ipmagic("cd /tmp")
   ....:
/tmp
/tmp
/tmp

In [19]:for mot in ('a1','a2','a3'):
   ....:    ipmagic("pr $mot")
   ....:
angle=+0.00 deg, zero=-5.00 deg, lowLimit=-170.00 deg, highLimit=+190.00 deg
angle=+0.00 deg, zero=+0.00 deg, lowLimit=-180.00 deg, highLimit=+180.00 deg
angle=+0.00 deg, zero=+0.00 deg, lowLimit=-180.00 deg, highLimit=+180.00 deg

-- 
   Frédéric




More information about the IPython-dev mailing list