Re: [Tutor] Difference between popens

2006-06-10 Thread Michael P. Reilly
On 6/9/06, Bernard Lebel <[EMAIL PROTECTED]> wrote:
Hey, thanks for the nice explanation Michael!BernardWhoops.. Hit "reply" instead of "reply to all".  My apologies to the group.  Dang gmail.  -Michael
-- There's so many different worlds,So many different suns.And we have just one world,But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between popens

2006-06-09 Thread Alan Gauld
> I'd like to know what are the differences at the various os.popenX
> flavors. I read the documentation and I can see they return file
> objects. so what can you do with these file objects? I mean, why
> would you need a set of file objects rather than another?

My OS topic covers some of the popen variants with explanation.
It might help.

Here are the most relevant two paragraphs:

--
In fact there are several variations of the popen command called 
popen, popen2, popen3 and popen4. The numbers refer to the various 
data stream combinations that are made available. The standard data 
streams were described in a sidebar in the Talking to the User topic. 
The basic version of popen simply creates a single data stream where 
all input/output is sent/received depending on a mode parameter passed 
to the function. In essence it tries to make executing a command look 
like using a file object.

By contrast, popen2 offers two streams, one for standard output and 
another for standard input, so we can send data to the process and 
read the output without closing the process. popen3 provides stderr 
access in addition to stdin/stdout. Finally there is popen4 that 
combines stderr and stdout into a single stream which appears very 
like normal console output. In Python 2.4 all of these popen calls 
have been superseded by a new Popen class found in a new subprocess 
module which we will look at later. For now we will only look at the 
standard os.popen() function, the others I will leave as a research 
exercise!

---

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between popens

2006-06-09 Thread Bernard Lebel
Hey, thanks for the nice explanation Michael!


Bernard



On 6/9/06, Michael P. Reilly <[EMAIL PROTECTED]> wrote:
> Excuse the slightly pedantic discussion before I get to a real-world
> example, but the differences get into something a bit deeper than Python and
> into what is called "interprocess communication".  Each program that's
> running has an input data stream and an output data steam and on many modern
> computer systems there is a third stream for error messages.  The idea is
> that the program doesn't need to know that the input is coming from a user
> or a keyboard; it could be coming from a file or another program - the
> program shouldn't care.  The same thing with the output and the error
> messages.
>
> These three data streams have traditionally been called "stdin" (standard
> input), "stdout" (standard output) and "stderr" (standard error).  And in
> Python, they are known by the same names in the 'sys' module.
>
> Now before I said that the concept of these is that the program should not
> care where its data is going to or coming from, including another program.
> This is where interprocess communication comes into play.  Specifically,
> this is accomplished with a data construct called "pipes" (the name 'popen'
> comes from "pipe open" in the C world, akin to 'fopen' for "file open").
> Pipes are created between the programs to redirect data between inputs and
> outputs.  The most well known and viewed form of this is the vertical bar
> "|" on command-lines in UNIX, then DOS had to adopt it:  type README.txt |
> more.
>
> Back to the question at hand: why and how would you use the files returned.
> Let's say you have a large dictionary of values in your Python program, and
> the system has a very nice spelling program out there.  You would like to
> use that program so you don't have to write you own routine.  How the
> spelling program is normally called is that words are sent to its standard
> input (stdin) stream, separated by newlines, and the misspelled words are
> printed on its stdout, also separated by newlines.  We want to start the
> program (called "spawning"), write our dictionary values to its stdin and
> read its stdout.  For this, we need to gain access to the program's stdin
> and stdout.  Since we are spawning it, it is called our "child" process.
>
> def verify_dictionary(dict_o_words):
> reverse_dict = {}  # to get back to the keys
> (child_stdin, child_stdout) = os.popen2('spell')
> for (key, value) in dict_o_words.items():
> if reverse_dict.has_key(value):
> reverse_dict[value].append(key)
> else:
> reverse_dict[value] = [key]
> child_stdin.write('%s\n' % value)
> child_stdin.close() # close the data stream to tell other program we're
> finished
> misspellings = []
> for line in child_stdout.readlines():
> value = line.strip()
> if reverse_dict.has_key(value):
> misspellings.extend(reverse_dict[value])
> close_stdout.close() # tells other program we're finished reading from
> it
> return misspellings
>
> I hope this discussion answers your questions and hasn't been too beneath
> you. :)  Some of it might be arcane history now, but I always feel it is
> good to know the roots to understand where you are going.
>   -Michael
>
>
> On 6/9/06, Bernard Lebel <[EMAIL PROTECTED]> wrote:
> >
>  Hi,
>
> I'd like to know what are the differences at the various os.popenX
> flavors. I read the documentation and I can see they return file
> objects. so what can you do with these file objects? I mean, why
>  would you need a set of file objects rather than another?
>
> Sorry the difference is very not clear to me.
>
>
> Thanks
> Bernard
> ___
> Tutor maillist  -   Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> There's so many different worlds,
> So many different suns.
> And we have just one world,
> But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between popens

2006-06-09 Thread Dave Kuhlman
On Fri, Jun 09, 2006 at 03:38:58PM -0400, Bernard Lebel wrote:
> Hi,
> 
> I'd like to know what are the differences at the various os.popenX
> flavors. I read the documentation and I can see they return file
> objects. so what can you do with these file objects? I mean, why
> would you need a set of file objects rather than another?
> 


See documentation at
http://docs.python.org/lib/os-newstreams.html#l2h-1552.

And, notice how the return values from the various versions of
popen are different file types: stdin, stdout, and stderr.

A summary:

- popen() gives you either an input pipe (stdin) or an output
  pipe (stdout) but not both.

- popen2() gives you both an input pipe (stdin) and output pipe
  (stdout).

- popen3() gives you an input pipe (stdin) and output pipe
  (stdout) and an error pipe (stderr).

- popen4() gives you an input pipe and a pipe that combines output
  and errors (stdout and stderr).

Specifically, if you want to run a command, just like you would
with os.system(), but:

1. You want to *either* feed (pipe) text to your command *or* read
   results from your command, use os.popen() and use mode= 'w' or
   'r'.

2. You want to both feed (pipe) text to your command *and* read
   results from your command, use os.popen2().

Etc.

If you get an input pipe, you should write text to that pipe, then
close that stream.  Doing close() is what triggers execution of the
command.

If you get an output pipe, then (after the command runs), read
from that pipe to get the results of your command (i.e. the text
that the command wrote to stdout).

Here is a simple example that uses popen2::

import os

def test():
instream, outstream = os.popen2('grep dave')
instream.write('Line #1\n')
instream.write('Line #2 dave is here\n')
instream.write('Line #3\n')
instream.write('Line #4 dave is also here\n')
instream.write('Line #5\n')
instream.close()
for line in outstream:
print 'Line: %s' % line.rstrip()

test()

Note that there is also a popen module, which has functions with
the same names and functionality:

"This functionality is also available in the popen2 module
using functions of the same names, but the return values of
those functions have a different order."

See: http://docs.python.org/lib/module-popen2.html

Hope this helps.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor