Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-19 Thread Peter Otten
George R Goffe wrote:

 When you run a python program, it appears that stdin, stdout, and stderr
 are opened automatically.
 
 I've been trying to find out how you tell if there's data in stdin (like
 when you pipe data to a python program) rather than in a named input file.
 It seems like most/all the Unix/Linux commands are able to figure this
 out. Do you know how Python programs do this or might do this?

I don't think there is a way to guess that. Instead there is an optional 
commandline argument; if that is missing or '-' the script assumes stdin as 
the default. With argparse you spell it like this:

$ cat upper.py
#!/usr/bin/env python3

import argparse
import sys

parser = argparse.ArgumentParser(description=Convert input to uppercase)
parser.add_argument(
input, type=argparse.FileType(r), default=sys.stdin, nargs=?,
help=Input file or '-' for stdin. Default: stdin.)
for line in parser.parse_args().input:
print(line.upper(), end=)
$ ./upper.py 
Hello
HELLO
$ ./upper.py upper.py
#!/USR/BIN/ENV PYTHON3

IMPORT ARGPARSE
IMPORT SYS

PARSER = ARGPARSE.ARGUMENTPARSER(DESCRIPTION=CONVERT INPUT TO STDIN)
PARSER.ADD_ARGUMENT(
INPUT, TYPE=ARGPARSE.FILETYPE(R), DEFAULT=SYS.STDIN, NARGS=?,
HELP=INPUT FILE OR '-' FOR STDIN. DEFAULT: STDIN.)
FOR LINE IN PARSER.PARSE_ARGS().INPUT:
PRINT(LINE.UPPER(), END=)

There is also the fileinput module.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-19 Thread Alan Gauld

On 18/10/14 19:36, George R Goffe wrote:


When you run a python program, it appears that stdin, stdout, and stderr are 
opened automatically.


correct.


I've been trying to find out how you tell if there's data in stdin


Same way you tell if there's data in any file/stream - you read
from it.

In the old days when I programmed in C there were a pair of calls
often used for this: getc() and ungetc(). You read a character
with getc() then pout it back with ungetc(). Unfortunately they
don't exist in Python stdlib. But, there is a getch()/ungetch()
in the msvcrt for Windows, so you could use that. The curses module
on linux has an ungetch() but no getch() - which seems bizarre...

Steven has posted a solution using read(1) but that blocks
so you need to use the isatty() method which all seems a bit
messy and doesn't work with tty input.

On further investigation the curses.screen object has a getch()
method, but its not clear how the curses.ungetch() relates to
that (there's no help() documentation) and I've run out of time to
experiment. But if you need a linux solution that works with
any kind of stdin that might be worth exploring.

Sorry, not a great answer but hopefully its of some use.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-19 Thread David Rock
* Peter Otten __pete...@web.de [2014-10-19 10:05]:
 George R Goffe wrote:
 
  When you run a python program, it appears that stdin, stdout, and stderr
  are opened automatically.
  
  I've been trying to find out how you tell if there's data in stdin (like
  when you pipe data to a python program) rather than in a named input file.
  It seems like most/all the Unix/Linux commands are able to figure this
  out. Do you know how Python programs do this or might do this?
 
 There is also the fileinput module.

I use fileinput all the time.

This iterates over the lines of all files listed in sys.argv[1:],
defaulting to sys.stdin if the list is empty. If a filename is '-', it
is also replaced by sys.stdin. To specify an alternative list of
filenames, pass it as the first argument to input(). A single file name
is also allowed.

It gives a fairly clean way to just do the Right Thing whether you are
feeding files, or reading from stdin.

-- 
David Rock
da...@graniteweb.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] A question about using stdin/out/err vs named files

2014-10-19 Thread George R Goffe
Hi,

Wow. Lots of feedback. REALLY GOOD FEEDBACK!

This was my first question to this list. Let me clarify my question.

I want to use tst.py as follows:

tst.py input-file output-file OR
cat data-file | tst.py - output-file OR
cat data-file | tst.py output-file

tst.py input-file output-file works well
tst.py - output-file works well

The question boils down to tst.py output-file... which is a parsing the 
args question which you guys have caused me to think more clearly about. If 
there's just 1 arg, consider it an output-file and read from stdin and write to 
output-file ONLY if output-file does not exist.

Thank you all for your very helpful and informative responses.

Regards,

George...
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-19 Thread Adam Jensen
On 10/18/2014 02:36 PM, George R Goffe wrote:
 Hi,
 
 When you run a python program, it appears that stdin, stdout, and stderr are 
 opened automatically.
 
 I've been trying to find out how you tell if there's data in stdin (like when 
 you pipe data to a python program) rather 
 than in a named input file. It seems like most/all the Unix/Linux 
 commands are able to figure this out. Do you know how Python programs do this 
 or might do this?
 
 MANY thanks for any/all help/hints/tips/suggestions,
 
 George...

Command line argument parsing aside, perhaps something like this would
be useful:

script.py
---
#!/usr/bin/env python3
import os, stat
mode = os.fstat(0).st_mode

if stat.S_ISFIFO(mode):
 print(stdin is a pipe)
elif stat.S_ISREG(mode):
 print(stdin is a redirected file)
elif stat.S_ISCHR(mode):
 print(stdin is terminal)
else:
 print(stdin is weird)
---

$ ./script.py
stdin is terminal

$ cat script.py | ./script.py
stdin is a pipe

$ ./script.py  script.py
stdin is a redirected file

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread George R Goffe
Hi,

When you run a python program, it appears that stdin, stdout, and stderr are 
opened automatically.

I've been trying to find out how you tell if there's data in stdin (like when 
you pipe data to a python program) rather 
than in a named input file. It seems like most/all the Unix/Linux 
commands are able to figure this out. Do you know how Python programs do this 
or might do this?

MANY thanks for any/all help/hints/tips/suggestions,

George...
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread Ben Finney
George R Goffe grgo...@yahoo.com.dmarc.invalid writes:

 When you run a python program, it appears that stdin, stdout, and
 stderr are opened automatically.

That's true of any program on a POSIX-compliant operating system.

 I've been trying to find out how you tell if there's data in stdin
 (like when you pipe data to a python program) rather than in a named
 input file.

What does “there's data in [a stream]” mean here, and how is it distinct
from there being “data in … a named input file”?

The advantage of the standard POSIX streams is that processes can treat
them as very nearly normal files. I don't doubt you have a distinction
you want to detect, but can you be clearer about what that distinction
is?

 It seems like most/all the Unix/Linux commands are able to figure this
 out. Do you know how Python programs do this or might do this?

Hmm. The standard input stream is a separate object from any named input
file you might otherwise open. That's a trivially obvious difference to
detect, so I guess you must not mean that.

Perhaps you mean “is the ‘stdin’ stream connected to an interactive
terminal?”. That's quite a different question from what you seem to be
asking, so I don't know.

-- 
 \   “The best is the enemy of the good.” —Voltaire, _Dictionnaire |
  `\Philosophique_ |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread wolfrage8...@gmail.com
Are you planning to pipe data to a python program? If so please
specify and you will get more complete answers.
Specifically I am thinking you want information pertaining to
subprocess in the standard library.
https://docs.python.org/3/library/subprocess.html

On Sat, Oct 18, 2014 at 2:36 PM, George R Goffe
grgo...@yahoo.com.dmarc.invalid wrote:
 Hi,

 When you run a python program, it appears that stdin, stdout, and stderr are 
 opened automatically.

 I've been trying to find out how you tell if there's data in stdin (like when 
 you pipe data to a python program) rather
 than in a named input file. It seems like most/all the Unix/Linux
 commands are able to figure this out. Do you know how Python programs do this 
 or might do this?

 MANY thanks for any/all help/hints/tips/suggestions,

 George...
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread Steven D'Aprano
On Sat, Oct 18, 2014 at 11:36:43AM -0700, George R Goffe wrote:
 Hi,
 
 When you run a python program, it appears that stdin, stdout, and 
 stderr are opened automatically.
 
 I've been trying to find out how you tell if there's data in stdin 
 (like when you pipe data to a python program) rather than in a named 
 input file. It seems like most/all the Unix/Linux commands are able to 
 figure this out. Do you know how Python programs do this or might do 
 this?

Hmmm, good question. I've never actually done this before, but I think 
the right way is to just try reading from stdin and see if anything is 
there. Let's try it, using Python 2.7:

# stdin_demo.py
import sys
c = sys.stdin.read(1)
if c:
text = c + sys.stdin.read()  # Slurp everything.
print Read text from stdin:
print text
else:
print Nothing in stdin.



Alas, that doesn't do what I expected. It works if I pipe something 
into stdin:

[steve@ando ~]$ echo Some data here | python2.7 stdin_demo.py
Read text from stdin:
Some data here



but if stdin is empty, the call to read() blocks, waiting for input. I 
have to manually type something (or nothing, as the case may be) 
then type Ctrl-D to force end-of-file:

[steve@ando ~]$ python2.7 stdin_demo.py  # enter something, then Ctrl-D
hello world
Read text from stdin:
hello world

[steve@ando ~]$ python2.7 stdin_demo.py  # immediately Ctrl-D
Nothing in stdin.



Here's my second attempt:

# stdin_demo.py
import sys
if sys.stdin.isatty():
print Ignoring stdin.
else:
c = sys.stdin.read(1)
if c:
text = c + sys.stdin.read()  # Slurp everything.
print Read text from stdin:
print text
else:
print Nothing in stdin.



This version seems to do what I think you want:


[steve@ando ~]$ python2.7 stdin_demo.py
Ignoring stdin.
[steve@ando ~]$ echo Some data here | python2.7 stdin_demo.py
Read text from stdin:
Some data here

[steve@ando ~]$ echo -n  | python2.7 stdin_demo.py
Nothing in stdin.


I haven't tried this under Windows, and I'm not sure if it is the 
correct way to do it under POSIX systems like Unix, Linux or Mac, but 
it seems to work.

Oh, in case it wasn't obvious... it's probably not a good idea to slurb 
everything as I do above, but I leave processing stdin line-by-line as 
an exercise.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor