An iteration idiom (Was: Re: [Guppy-pe-list] loading files containing multiple dumps)

Boris Borcic bborcic at gmail.com
Wed Sep 2 15:33:56 CEST 2009


Sverker Nilsson wrote:
>> Sverker Nilsson wrote:
>>> It reads one Stat object at a time and wants to report something
>>> when there is no more to be read from the file. 
>> Hmm, am I right in thinking the above can more nicely be written as:
>>
>>  >>> from guppy import hpy
>>  >>> h = hpy()
>>  >>> f = open(r'your.hpy')
>>  >>> sets = []
>>  >>> for s in iter(h.load(f)): sets.append(s)
>> ...
>>
> The above iterates over one Stat object returned by h.load(f). I assume
> you want to iterate over all the objects loaded.

I dont know guppy,
but if h.load(f) raises StopIteration upon eof, as seems implied by your 
proposal, then something like the following would work.

sets.extend(h.load(f) for _ in xrange(1e9))




More information about the Python-list mailing list