473,586 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ 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(li ne)

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 1619
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.isatt y()

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

Hth,
M.E.Farmer

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

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.isatt y() should serve you well.
Alex
Jul 18 '05 #3

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

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.isatt y() 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(li ne)


I'd use:

for numline, line in enumerate(sys.s tdin):
workwithline(li ne)

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

--Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #5
"B.G.R." <no****@yahoo.t k> 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(li ne)

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.or g> 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
"HashmarkAtStar tOfOtherThanLin eZeroError", 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******@lexic on.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
"HashmarkAtStar tOfOtherThanLin eZeroError", 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__(__na me__)
f.__dict__['name'] = value

<mike
--
Mike Meyer <mw*@mired.or g> 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
7799
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 finding it a bit cumbersome. It works, but if I switch from the interactive window to the plot window and back, the plot window gets trashed.
1
3299
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. "When Python prompts for the next command with the primary prompt ">>> ", the interpreter is said to be in an interactive mode, or in a calculator mode....
3
1383
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
831
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 another application to perform some functions. However since the service is in noninterative mode it lauches the secondary program using the same mode. ...
18
7511
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++ program. For example I want to run a very simple command like "plot sin(x)". So after running another program, what is the way to pipe that with some...
14
2792
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 by df -k lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset Filesystem 1K-blocks Used Available Use% Mounted on
4
1750
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. Background: I would like to have the program run in an "interactive mode" if double clicked, and silently in a "batch mode" when started otherwise.
4
1756
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 test.py 11060) but when I type sequencially it doesn't work as usual. Can any give me a hand??
3
1801
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. Isn't there a way to stay in interactive mode after a syntax error?
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8216
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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 we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.