[IPython-user] Using __IP.push()
Fernando Perez
Fernando.Perez at colorado.edu
Wed Mar 23 10:48:36 CST 2005
Frédéric Mantegazza wrote:
> Le Mercredi 23 Mars 2005 15:58, Fernando Perez a écrit :
>
>>Now, the restriction of this not working from inside ipython itself
>>remains, because of the recursive problem. However, if for some reason
>>you do need to access runlines from within a running ipython, you can
>>write a magic to do so.
>
>
> Yes, I would like to have it works from inside ipython. I already have a
> magic called 'do' which takes a batch file as argument. At the moment, the
> batch file can only contain magics, because I use the magic() method to
> execute lines. Here is the code :
If you update CVS (I just fixed a small bug in runlines()), the following
magic will run a file containing text as if it had been typed in the ipython
prompt:
def magic_runbatch(self, parameter_s=''):
""" runbatch magic command.
"""
try:
fileobj = open(parameter_s,'r')
except:
print "Error: could not open file",parameter_s
return
else:
self.shell.runlines(fileobj.read())
For example, for the file:
[test]> cat ipbatch.py
print "This is being run by IPython"
%sc a=ls i*py
print "a is:\\n",a
print "run a loop over a.l"
for f in a.l:
print f
!wc -l $f
3+5
print _
#########
these are the results:
In [1]: %runbatch ipbatch.py
This is being run by IPython
a is:\n input.py
ipbatch.py
ippush.py
iters2.py
iters3.py
iters.py
run a loop over a.l
input.py
2 input.py
ipbatch.py
12 ipbatch.py
ippush.py
22 ippush.py
iters2.py
23 iters2.py
iters3.py
19 iters3.py
iters.py
18 iters.py
Out[1]: 8
8
################
Note one important caveat: in this format, when you want to finish an indented
loop or function definition, to signify execution, you _must_ put a blank
line. This is just like what happens when working interactively, and for the
same reason (you are using the same mechanism).
You should be able to modify the above to suit your needs.
Best,
f
More information about the IPython-user
mailing list