program in interactive mode
mwm at mired.org
Mon Dec 27 05:49:39 CET 2004
"B.G.R." <nospam at yahoo.tk> writes:
> Hi all,
> I'm working on an interpreter for a university subject, which is programmed
> in python under linux.
> I got most of the work done and I'm just trying to settle some problems I've
> found on my way.
> Right now, the most important is reading the user input. Everything goes
> through the standard input, and now I got something like this:
> for line in sys.stdin:
> A little bit more complex, but that's the idea. That will work if the user
> does something like "./myprog.py < code" or "cat code | ./myprog.py", and
> that's ok, but if the user only does "./myprog.py" then I got to get into
> interactive mode and show a prompt in every line expecting the user input
> for that line. Problem is I don't know how to tell if I've been "piped" or
> I used to think that the piped program doesn't know anything and it's just
> OS dependant to close and open the right descriptors, but I'm not sure
> anymore. Any help or pointer in the right direction would be greatly
I've discovered a truly elegant trick with python programs that
interpret other data. You make them ignore lines that start with # at
the beginning of the line, and accept the name of a file to be
interpreted as a first argument. Your users can then put
at the top of their files, and then treat their files as normal
executables. mycode.py has to be on their path; if not, they need to
plug in the full path to mycode.py.
I save the state of TkInter programs by writing this out then pickling
the objects that define the state to the file. Executing that file
will bring the program back up in the same state it was saved in.
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list