Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12600
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <sahil@FreeBSD.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'subject:while': 0.07; 'typed': 0.07; 'ah,': 0.09; 'disable': 0.09; 'flush': 0.09; 'kinda': 0.09; 'none:': 0.09; 'parsing': 0.09; 'debugging': 0.13; '"while': 0.16; "'w',": 0.16; '(via': 0.16; 'buffering': 0.16; 'construct.': 0.16; 'hangs': 0.16; 'input,': 0.16; 'stdout': 0.16; 'sys.stdout': 0.16; 'wrote:': 0.16; 'solution.': 0.17; 'stick': 0.18; 'otherwise,': 0.19; 'slightly': 0.19; 'seems': 0.20; 'appears': 0.20; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'away.': 0.23; 'sep': 0.23; 'changed': 0.24; 'command': 0.24; 'input': 0.24; 'fine': 0.26; "i'm": 0.27; 'repeatedly': 0.28; 'thu,': 0.28; 'bit': 0.28; 'explicitly': 0.29; 'fine.': 0.29; 'looks': 0.29; 'print': 0.29; 'script': 0.29; 'turned': 0.30; 'thanks': 0.30; 'does': 0.32; 'it.': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; "i've": 0.34; 'header:User-Agent:1': 0.34; 'approach.': 0.34; 'totally': 0.35; 'unless': 0.36; 'anything': 0.36; 'response,': 0.37; 'using': 0.37; 'but': 0.37; 'think': 0.38; 'received:org': 0.38; 'steven': 0.38; 'everyone': 0.38; 'some': 0.38; 'subject:: ': 0.39; 'manually': 0.39; 'recommended': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; 'more': 0.60; 'your': 0.61; 'skip:o 30': 0.63; 'reply': 0.64; 'believe': 0.65; 'received:12': 0.66; 'taking': 0.66; 'account': 0.66; 'bothered': 0.68; 'works,': 0.68; 'want,': 0.71; "'1'": 0.84; 'idiomatic': 0.84; "others'": 0.84; 'print.': 0.84; 'slow.': 0.84 |
| X-Virus-Scanned | by ClamAV at spartan.hamla.org |
| Date | Thu, 01 Sep 2011 22:02:54 -0400 |
| From | Sahil Tandon <sahil@FreeBSD.org> |
| User-Agent | Postbox Express 1.0.1 (Macintosh/20100705) |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: idiomatic analogue of Perl's: while (<>) { ... } |
| References | <mailman.644.1314853527.27778.python-list@python.org> <4e5f2010$0$29987$c3e8da3$5496439d@news.astraweb.com> |
| In-Reply-To | <4e5f2010$0$29987$c3e8da3$5496439d@news.astraweb.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| 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.678.1314928995.27778.python-list@python.org> (permalink) |
| Lines | 57 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1314928995 news.xs4all.nl 2451 [2001:888:2000:d::a6]:48272 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:12600 |
Show key headers only | View raw
[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.
>> # 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')
>> %%
>
> "while True" is considered slightly more idiomatic (readable), but
> otherwise, that seems fine.
Ah, thanks -- I've changed '1' to 'True'.
>> 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?
>
> I'm not sure anything about fileinput is exactly *recommended*, it's kinda
> discouraged on account of being a bit slow. See help(fileinput) at the
> interactive prompt.
>
> For what it's worth, the default buffersize for fileinput.input is 0, so if
> that doesn't do what you want, I don't think fileinput is the right
> solution.
Got it. Based on your and others' response, I will stick with my
existing approach.
--
Sahil Tandon <sahil@FreeBSD.org>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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