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

2011-09-02 Thread Sahil Tandon
Dennis Lee Bieber wrote: On Thu, 1 Sep 2011 00:56:50 -0400, Sahil Tandon # process input, line-by-line, and print responses after parsing input while 1: rval = parse(raw_input()) if rval == None: There is only ONE "None" object so the preferred method is if rval is None:

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

2011-09-01 Thread Cameron Simpson
On 01Sep2011 22:02, Sahil Tandon wrote: | [Thanks to everyone who responded] | | Steven D'Aprano wrote: | >On Thu, 1 Sep 2011 02:56 pm Sahil Tandon wrote: | >>%% | >># unbuffer STDOUT | >>sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) | > | >I've never bothered with unbuffered stdout, but th

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

2011-09-01 Thread Dan Sommers
On Thu, 01 Sep 2011 16:02:54 +1000, Steven D'Aprano wrote: > On Thu, 1 Sep 2011 02:56 pm Sahil Tandon wrote: >> # 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'

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

2011-09-01 Thread Sahil Tandon
[Thanks to everyone who responded] Steven D'Aprano wrote: On Thu, 1 Sep 2011 02:56 pm Sahil Tandon wrote: %% # unbuffer STDOUT sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) I've never bothered with unbuffered stdout, but that looks fine to me. I'm not sure if it is necessary though, be

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

2011-09-01 Thread Anssi Saari
Sahil Tandon writes: > I've been tasked with converting some programs from Perl -> Python, and > am (as will soon be obvious) new to the language. If it's any help, I have usually done handling of standard input line by line with this kind of thing: for inputline in sys.stdin: -- http://mail.

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

2011-09-01 Thread Peter Otten
Peter Otten wrote: > A quick look into fileinput.py reveals that it uses readlines() and slurps > in the complete "file". I'm not sure that was a clever design decision... Correction: >>> with open("tmp.txt") as f: lines = f.readlines(0) ... >>> len(lines) 100 >>> with open("tmp.txt") as f:

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

2011-08-31 Thread Peter Otten
Sahil Tandon wrote: > 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 t

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

2011-08-31 Thread Steven D'Aprano
On Thu, 1 Sep 2011 02:56 pm Sahil Tandon wrote: > 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 m

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

2011-08-31 Thread Sahil Tandon
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: