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


Groups > comp.lang.python > #12613

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(except': 0.05; 'received:edu.au': 0.07; 'subject:while': 0.07; 'disable': 0.09; 'flush': 0.09; 'parsing': 0.09; "'w',": 0.16; '(via': 0.16; ">i've": 0.16; '>on': 0.16; 'buffering': 0.16; 'flushed': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'hangs': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'newlines': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'stdout': 0.16; 'sys.stdout': 0.16; 'cc:addr:python-list': 0.16; 'written': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'seems': 0.20; "doesn't": 0.22; 'stuff': 0.22; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'away.': 0.23; 'sep': 0.23; 'sfxlen:0': 0.23; 'testing': 0.24; 'fine': 0.26; "i'm": 0.27; 'thu,': 0.28; 'explicitly': 0.29; 'looks': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'turned': 0.30; 'yes.': 0.30; 'probably': 0.33; 'instead': 0.33; 'header:User-Agent:1': 0.34; 'normally': 0.34; 'unless': 0.36; 'charset:us-ascii': 0.36; 'received:au': 0.36; 'response,': 0.37; 'but': 0.37; 'steven': 0.38; 'everyone': 0.38; 'subject:: ': 0.39; 'manually': 0.39; 'suggestions': 0.39; 'enough': 0.39; 'your': 0.61; 'skip:o 30': 0.63; 'reply': 0.64; 'received:202': 0.66; 'cameron': 0.67; 'bothered': 0.68; 'saw': 0.69; 'gentle': 0.77; 'print.': 0.84; 'tracy': 0.84; 'to:addr:freebsd.org': 0.93
Date Fri, 2 Sep 2011 15:01:59 +1000
From Cameron Simpson <cs@zip.com.au>
To Sahil Tandon <sahil@FreeBSD.org>
Subject Re: idiomatic analogue of Perl's: while (<>) { ... }
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <4E60394E.2080708@FreeBSD.org>
User-Agent Mutt/1.5.21 (2010-09-15)
References <4E60394E.2080708@FreeBSD.org>
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.683.1314939729.27778.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314939729 news.xs4all.nl 2538 [2001:888:2000:d::a6]:48583
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12613

Show key headers only | View raw


On 01Sep2011 22:02, Sahil Tandon <sahil@FreeBSD.org> 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 that looks fine to me.
| >
| >I'm not sure if it is necessary though, because print seems to automatically
| >flush the buffer after each line in my testing. Unless you're printing
| >repeatedly to the same line, I'm not sure unbuffered stdout is helpful.
| 
| I found it necessary because without reopening sys.stdout with
| buffering explicitly turned off, I would have to manually flush the
| buffer after each print.  This is because the program must reply
| (via writing to STDOUT) after parsing each line read via STDIN.  If
| I neither disable buffering nor manually flush after each print, the
| program just hangs instead of printing right away.

Yes. Steven was probably testing on a terminal. UNIX stdio buffering is
line buffered on a terminal and block buffered otherwise (except for
stderr, which is normally unbuffered regardless). So Steven saw stuff
flushed on newlines and you would see stuff flushed when you explicitly
flush or when the buffer fills (probably doesn't happen for your use
because you need a response, and never write enough to fill the buffer).

Cheers,
-- 
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Gentle suggestions being those which are written on rocks of less than 5lbs.
        - Tracy Nelson in comp.lang.c

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


Thread

Re: idiomatic analogue of Perl's: while (<>) { ... } Cameron Simpson <cs@zip.com.au> - 2011-09-02 15:01 +1000

csiph-web