473,240 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

program in interactive mode

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:

numline=0
for line in sys.stdin:
numline+=1
workwithline(line)

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
not.
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
appreciated.

Happy christmas everyone,

RGB


Jul 18 '05 #1
7 1598
B.G.R. wrote:
Hi all, [snip] 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 not.
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
appreciated.

Happy christmas everyone,

RGB

Hello RGB,
what you are loking for is sys.stdin.isatty()

py> if sys.stdin.isatty():
.... print 'Console'
.... else:
.... print 'Redirected'

Hth,
M.E.Farmer

Jul 18 '05 #2
B.G.R. <no****@yahoo.tk> wrote:
...
numline=0
for line in sys.stdin:
numline+=1
workwithline(line)
Consider the alternative:
for numline, line in enumerate(sys.stdin):

Nothing to do with your main program, but still neater...
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


sys.stdin.isatty() should serve you well.
Alex
Jul 18 '05 #3

"Alex Martelli" <al*****@yahoo.com> escribió en el mensaje
news:1gpf7qp.c9e4w97cdazzN%al*****@yahoo.com...
B.G.R. <no****@yahoo.tk> wrote:
...
numline=0
for line in sys.stdin:
numline+=1
workwithline(line)
Consider the alternative:
for numline, line in enumerate(sys.stdin):

Nothing to do with your main program, but still neater...
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
sys.stdin.isatty() should serve you well.
Alex

Thank you all very much for your help and tips, that's exactly what I was
looking for.
I guess my knowledge of the libraries is still quite limited. Time to work
on that too.

Regards
RGB

Jul 18 '05 #4
B.G.R. wrote:
numline=0
for line in sys.stdin:
numline+=1
workwithline(line)


I'd use:

for numline, line in enumerate(sys.stdin):
workwithline(line)

Note: The line numbers start at 0, but that is often acceptable.

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #5
"B.G.R." <no****@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:

numline=0
for line in sys.stdin:
numline+=1
workwithline(line)

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
not.
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
appreciated.


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

#!/usr/bin/env mycode.py

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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #6

Mike Meyer wrote:

I've discovered a truly elegant trick with python programs that
interpret other data.
Q0. Other than what?
You make them ignore lines that start with # at
the beginning of the line,
Q1. After the first user accidentally gets a # at the start of a real
data line, a few hundred lines into their file, then what will you do?
Fix your script to detect this error and re-issue your documentation,
emphasising that this is not a general comment convention?

Q2. Then when users 2+ steam up complaining that they have stacks of
files containing lines like "#### Next section is frappenwanger
readings in picoHertz ####", and the script is printing out a whole lot
of what they regard as gobbledegook followed by
"HashmarkAtStartOfOtherThanLineZeroError", and then stopping, what do
you do?
and accept the name of a file to be
interpreted as a first argument. Your users can then put

#!/usr/bin/env mycode.py
Q3. Will that work on 'Doze?

Q4. Doesn't that tie their file to your script, or force other scripts
to ignore the first line?

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.


Q5. For comparison purposes, could you please post an example of what
you regard as a filthy ugly trick?

Jul 18 '05 #7
"John Machin" <sj******@lexicon.net> writes:
Mike Meyer wrote:

I've discovered a truly elegant trick with python programs that
interpret other data. Q0. Other than what?


Other than Python code.
You make them ignore lines that start with # at
the beginning of the line,

Q1. After the first user accidentally gets a # at the start of a real
data line, a few hundred lines into their file, then what will you do?
Fix your script to detect this error and re-issue your documentation,
emphasising that this is not a general comment convention?


Depends on how you implement it. Possibly issue an error
message. Possibly treat this as data. Possibly treat this as a comment.
Q2. Then when users 2+ steam up complaining that they have stacks of
files containing lines like "#### Next section is frappenwanger
readings in picoHertz ####", and the script is printing out a whole lot
of what they regard as gobbledegook followed by
"HashmarkAtStartOfOtherThanLineZeroError", and then stopping, what do
you do?
You don't implement the hashmark that way, of course.
and accept the name of a file to be
interpreted as a first argument. Your users can then put

#!/usr/bin/env mycode.py

Q3. Will that work on 'Doze?


Probably not. I don't know if this is part of the Posix compatability
level or not.
Q4. Doesn't that tie their file to your script, or force other scripts
to ignore the first line?


This trick is really only applicable to data where you control the
file format. As I mentioned, I use it to treat pickled program
configuration files as executables.
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.


Q5. For comparison purposes, could you please post an example of what
you regard as a filthy ugly trick?


f = __import__(__name__)
f.__dict__['name'] = value

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Charles Krug | last post by:
List: I'm trying to us pylab to see what I'm doing with some DSP algorithms, in case my posts about convolution and ffts weren't giving it away. I've been using pylab's plot function, but I'm...
1
by: Alex | last post by:
Loking at Rossum's tutorial I've seen that he sometimes uses the expression "interactive mode" and sometimes "calculator mode". Or these concepts same? I've made the following assertion. ...
3
by: Carramba | last post by:
hi! if I have for exemple program #include <stdio.h> main() { printf("Enter number:\n"); scanf("%d", Num); printf("Number is %d!\n", Num); fflush(stdin);
0
by: temp795 | last post by:
Hi all I am acutally using VB6, I couldn't find the posting for vb6. I have created a service that runs in noninteractive mode. My problem is this, when a user is logged in and I need to launch...
18
by: utab | last post by:
Dear all, I am making a system call to the well known Gnuplot with system("gnuplot"); gnuplot opens if I only supply this command but I would like to pipe that command line in my C++...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
4
by: Benjamin Hell | last post by:
Hi! I wonder whether there might be a way to find out how a Python program was started (in my case in Windows): By double clicking the file or by calling it on the "DOS" command line prompt. ...
4
by: digitnctu | last post by:
Dear all: I am coming with problem, to apply ctypes under interactive mode in python. libdll.dll is a third-party library. The below code segment will run well under the batch mode(ie. python...
3
by: Sal | last post by:
I'm running PHP 5.2.6 on WindowsXP. When I try interactive mode with ">php -a" the slightest syntax error throws me out of the interpreter and back to the command line. This is not very useful....
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.