473,473 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1604
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.