Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12547

idiomatic analogue of Perl's: while (<>) { ... }

Date 2011-09-01 00:56 -0400
From Sahil Tandon <sahil@FreeBSD.org>
Subject idiomatic analogue of Perl's: while (<>) { ... }
Newsgroups comp.lang.python
Message-ID <mailman.644.1314853527.27778.python-list@python.org> (permalink)

Show all headers | View raw


I've been tasked with converting some programs from Perl -> Python, and
am (as will soon be obvious) new to the language.  A few archive/google
searches were inconclusive on a consensus approach, which is OK, but I
just wonder if there is a more Python-esque way to do the following in
Python 2.7.1:

%%
# unbuffer STDOUT
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

# process input, line-by-line, and print responses after parsing input 
while 1:
  rval = parse(raw_input())
  if rval == None:
    print('foo')
  else:
    print('bar')
%%

This works, but while reading the documentation, I thought of using 'for
line in fileinput.input()' in lieu of 'while 1:' construct.  This does
not work when debugging the program on the command line -- the script
appears to just hang no matter what is typed into STDIN.  I believe this
is because of some internal buffering when using fileinput.  Is there a
recommended way to disable such buffering?  Am I taking a totally wrong
approach?

Feel free to just link me to previous discussions on the topic(s) if I
have missed them.  Please be gentle with your cluebats. :-)

Thanks,
-- 
Sahil Tandon <sahil@FreeBSD.org>

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

idiomatic analogue of Perl's: while (<>) { ... } Sahil Tandon <sahil@FreeBSD.org> - 2011-09-01 00:56 -0400
  Re: idiomatic analogue of Perl's: while (<>) { ... } Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-01 16:02 +1000
    Re: idiomatic analogue of Perl's: while (<>) { ... } Sahil Tandon <sahil@FreeBSD.org> - 2011-09-01 22:02 -0400
    Re: idiomatic analogue of Perl's: while (<>) { ... } Dan Sommers <dan@tombstonezero.net> - 2011-09-02 02:32 +0000
  Re: idiomatic analogue of Perl's: while (<>) { ... } Anssi Saari <as@sci.fi> - 2011-09-01 18:25 +0300

csiph-web