On Jun 14, 2005, at 1:25 AM, Raymond Hettinger wrote:

>>>> By the way, whatever happened to "and while"?  i.e.:
>>>>
>>>>      while True:
>>>>          data = inp.read(blocksize)
>>>>      and while data:
>>>>          out.write(data)
>>>>
>>>>
>>>
>>> My favourite version of this is
>>>
>>>    while:
>>>      data = inp.read(blocksize)
>>>    gives data:
>>>      out.write(data)
>>>
>>
>> Well, we could always just make iterator factories for the common
>> cases and stuff them in itertools.  I do need to use this pattern
>> from time to time, but primarily for this exact use case, so an
>> "itertools.readby" or the like would probably solve this problem for
>> most people most of the time.
>>
>
> Doesn't work.  You still need to be able to pass through the blocksize
> argument and the target bound method.  To get what you want, there  
> would
> need to be a new method in the file API.  The reason is roughly  
> similar
> to why we have iteritems as part of the mapping API and not as a
> standalone itertool.
>
>     for data in inp.read(blocksize):
>         . . .

Reality check?

def readby(inp, blocksize=1024):
     while True:
         data = inp.read(blocksize)
         if not data:
             break
         yield data

for data in readby(inp, blocksize):
     . . .

I always thought was that iteritems is part of the mapping API and  
not as a standalone itertool because it is the *most primitive* way  
to iterate over the key/value pairs.  It is simply not implementable  
without access to the private guts of the dict.  That is simply not  
the case here.

If you need an ultra-flexible (but less obvious) implementation you  
should just use the sentinel version of the iter function as JP  
demonstrated.

-bob

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to