Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'else:': 0.03; 'amounts': 0.07; 'hettinger': 0.07; 'subject:file': 0.07; 'subject:two': 0.07; 'versions.': 0.07; 'iterate': 0.09; 'performs': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'suggest': 0.14; 'wrote': 0.14; '"*"': 0.16; '1000):': 0.16; '3):': 0.16; 'b):': 0.16; 'benjamin': 0.16; 'caching': 0.16; 'descriptor': 0.16; 'descriptors': 0.16; 'finite': 0.16; 'garbage': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'itertools': 0.16; 'objects.': 0.16; 'programmers.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'roy': 0.16; 'trade-offs': 0.16; 'true:': 0.16; 'whitespace.': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'cheap': 0.19; '>>>': 0.22; 'memory': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'holds': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; "doesn't": 0.30; 'easier': 0.31; 'lines': 0.31; 'sep': 0.31; 'file': 0.32; 'agreed': 0.32; 'another': 0.32; 'open': 0.33; 'says': 0.33; 'url:python': 0.33; "i'd": 0.34; 'subject:with': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'doing': 0.36; 'possible': 0.36; 'url:org': 0.36; 'application': 0.37; 'two': 0.37; 'list': 0.37; 'minimum': 0.38; 'problems': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'dave': 0.60; 'most': 0.60; 'break': 0.61; "you're": 0.61; "you've": 0.63; 'costs': 0.63; 'such': 0.63; 'more': 0.64; 'talking': 0.65; 'temporary': 0.65; 'life': 0.66; 'believe': 0.68; 'smith': 0.68; 'around,': 0.84; 'different.': 0.84; 'hanging': 0.84; 'oscar': 0.84; 'subject:over': 0.84; '2013,': 0.91; 'angel': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: iterating over a file with two pointers Date: Thu, 19 Sep 2013 09:23:36 +0200 Organization: None References: <3018b3d4-f914-4c89-9f26-cd4b2af32e73@googlegroups.com> <52B7F7EA-C7C4-4DB6-A93C-25F4C058EB58@panix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849f04.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379575403 news.xs4all.nl 15987 [2001:888:2000:d::a6]:33107 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54418 Roy Smith wrote: >> Dave Angel wrote (and I agreed with): >>> I'd suggest you open the file twice, and get two file objects. Then you >>> can iterate over them independently. > > > On Sep 18, 2013, at 9:09 AM, Oscar Benjamin wrote: >> There's no need to use OS resources by opening the file twice or to >> screw up the IO caching with seek(). > > There's no reason NOT to use OS resources. That's what the OS is there > for; to make life easier on application programmers. Opening a file twice > costs almost nothing. File descriptors are almost as cheap as whitespace. > >> Peter's version holds just as many lines as is necessary in an >> internal Python buffer and performs the minimum possible >> amount of IO. > > I believe by "Peter's version", you're talking about: > >> from itertools import islice, tee >> >> with open("tmp.txt") as f: >> while True: >> for outer in f: >> print outer, >> if "*" in outer: >> f, g = tee(f) >> for inner in islice(g, 3): >> print " ", inner, del g # a good idea in the general case >> break >> else: >> break > > > There's this note from > http://docs.python.org/2.7/library/itertools.html#itertools.tee: > >> This itertool may require significant auxiliary storage (depending on how >> much temporary data needs to be stored). In general, if one iterator uses >> most or all of the data before another iterator starts, it is faster to >> use list() instead of tee(). > > > I have no idea how that interacts with the pattern above where you call > tee() serially. As I understand it the above says that items = infinite() a, b = tee(items) for item in islice(a, 1000): pass for pair in izip(a, b): pass stores 1000 items and can go on forever, but items = infinite() a, b = tee(items) for item in a: pass will consume unbounded memory and that if items is finite using a list instead of tee is more efficient. The documentation says nothing about items = infinite() a, b = tee(items) del a for item in b: pass so you have to trust Mr Hettinger or come up with a test case... > You're basically doing > > with open("my_file") as f: > while True: > f, g = tee(f) > > Are all of those g's just hanging around, eating up memory, while waiting > to be garbage collected? I have no idea. I'd say you've just devised a nice test to find out ;) > But I do know that no such > problems exist with the two file descriptor versions. The trade-offs are different. My version works with arbitrary iterators (think stdin), but will consume unbounded amounts of memory when the inner loop doesn't stop.