[IPython-user] Re: a problem about how to run it in batch

Fernando Perez Fernando.Perez at colorado.edu
Tue Apr 12 10:48:41 CDT 2005


Hi Robin,

I have cc-ed the user list, and I'd very much prefer it if in the future you 
direct these questions to the list.  That way you benefit from the replies of 
others besides me, and the discussion is archived for future reference.

The mailing list page is here, please subscribe so you get future replies:

http://scipy.net/mailman/listinfo/ipython-user

Robin Wu wrote:
> Dear sir,
> 
> I know you are very busy, sorry to ask you problem! Our environment is
> window, and will interact with sqlplus, so we need here document like perl
> and shell,such as this: sqlplus /nolog <<END conn / as sysdba ..... exit 
> END can this be done?

Python doesn't exactly have HERE docs, but you can obtain similar results in 
multiple ways with all the string formatting options in the language.  Please 
consult a tutorial or book about Python programming, there are many good ones 
on the internet.

> another problem is when I collect all the command issue under pysh and save
> them into a file, then run them in batch, it does not work such as :
> 
> $$b=dir print b
> 
> 
> this two command can be run individually, but can not run in batch: 
> C:\Python24\python.exe C:\Python24\scripts\ipython -p pysh test.py
> 
> and test.py like this ------- $$b=dir print b -------
> 
> when run it, it complain: File "test.py", line 1 $$b=dir ^
> 
> we want to ipython like shell in this case, but can not be done, and the
> document just touch the aspect of interacting, no others. I need your help,
>  thanks Best Regard

You must understand that ipython makes some _extensions_ to normal python 
syntax to make it more convenient to type things at the command line. 
However, these are not valid python, so there is no way the above will ever 
work.  I am not trying to change the Python language, these extensions are 
_strictly_ a convenience feature for interactive work.  Since they tend to 
make the code much less readable, they should never be used in permanent scripts.

If you want to see the equivalent real python code which ipython generates for 
you, use the logging features of ipython:

planck[~]> pysh
Welcome to pysh, a set of extensions to IPython for shell usage.
help(pysh) -> help on the installed shell extensions and syntax.

planck[~]|1> logstart mylog.py
Activating auto-logging.
Current session state plus future input saved to: mylog.py
Logging mode:  backup
planck[~]|2> $$b=ls
planck[~]|3> print b
['code', 'Desktop', 'ipython', 'mail', 'misc', 'music', 'mylog.py', 'prof', 
'ref', 'research', 'store', 'talks', 'teach', 'test', 'texmf', 'tmp', 'usr', 
'www']
planck[~]|4>Exit

#######
And now at a terminal, this is what the log looks like, with the magic syntax 
replaced by real python code:

planck[~]> cat mylog.py
#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
#log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
#log# opts = Struct({'profile': 'pysh', 'logfile': 'mylog.py', 'banner': 0})
#log# args = []
#log# It is safe to make manual edits below here.
#log#-----------------------------------------------------------------------
ipmagic("logstart mylog.py")
ipmagic("%sc -l b=ls")
print b


#######
Best,

f




More information about the IPython-user mailing list