Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'python,': 0.01; 'else:': 0.03; 'discussions': 0.03; 'sys': 0.05; ':-)': 0.06; 'subject:while': 0.07; 'typed': 0.07; 'python': 0.08; 'disable': 0.09; 'files:': 0.09; 'none:': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'debugging': 0.13; 'def': 0.15; 'converting': 0.15; "'w',": 0.16; 'approach,': 0.16; 'buffering': 0.16; 'construct.': 0.16; 'input,': 0.16; 'open(file)': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'stdout': 0.16; 'sys.stdout': 0.16; 'tasked': 0.16; 'wrote:': 0.16; 'perl': 0.18; 'appears': 0.20; 'wonder': 0.23; 'missed': 0.24; 'command': 0.24; 'input': 0.24; "i'm": 0.27; 'language.': 0.28; 'import': 0.28; 'yield': 0.29; 'print': 0.29; 'script': 0.29; 'skip:( 20': 0.29; 'from:addr:web.de': 0.30; 'does': 0.32; 'there': 0.33; 'to:addr :python-list': 0.33; "i've": 0.34; '(as': 0.34; 'consensus': 0.34; 'searches': 0.34; 'header:X-Complaints-To:1': 0.35; 'uses': 0.35; 'totally': 0.35; 'file': 0.36; 'partial': 0.37; 'using': 0.37; 'but': 0.37; 'received:org': 0.38; 'some': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'ok,': 0.39; 'header:Mime-Version:1': 0.39; 'recommended': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; 'your': 0.61; 'skip:o 30': 0.63; 'link': 0.63; 'free': 0.63; 'believe': 0.65; 'chain': 0.66; 'taking': 0.66; 'works,': 0.68; 'soon': 0.72; 'gentle': 0.77; 'reveals': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: idiomatic analogue of Perl's: while (<>) { ... } Date: Thu, 01 Sep 2011 08:31:32 +0200 Organization: None References: <20110901045650.GA3466@magic.hamla.org> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a90a.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314858674 news.xs4all.nl 2466 [2001:888:2000:d::a6]:45004 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12549 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 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. :-) 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... Here's a partial reimplementation that should work for your use-case: import sys from itertools import chain def _open_many(files): for file in files: if file == "-": yield sys.stdin else: with open(file) as f: yield f def input(files=None, buffered=False): if files is None: files = sys.argv[1:] if not files: files = ["-"] files = _open_many(files) if not buffered: files = (iter(file.readline, "") for file in files) return chain.from_iterable(files) for line in input(): print line.strip().upper()