Message195962
I've often wanted to be able to query a takewhile object to discover the item that failed the predicate but the item is currently discarded.
A usage example:
def sum(items):
it = iter(items)
ints = takewhile(Integral.__instancecheck__, it)
subtotal = sum(ints)
if not hasattr(ints.lastitem):
return subtotal
floats = takewhile(float.__instancecheck__, it)
subtotalf = fsum(floats)
if not hasattr(floats.lastitem):
return subtotal + subtotalf
# Deal with more types
...
Loosely what I'm thinking is this but perhaps with different attribute names:
class takewhile(pred, iterable):
def __init__(self):
self.pred = pred
self.iterable = iterable
self.failed = False
def __iter__(self):
for item in self.iterable:
if self.pred(item):
yield item
else:
self.failed = True
self.lastitem = item
return |
|
Date |
User |
Action |
Args |
2013-08-23 12:19:54 | oscarbenjamin | set | recipients:
+ oscarbenjamin |
2013-08-23 12:19:54 | oscarbenjamin | set | messageid: <1377260394.63.0.429867593592.issue18821@psf.upfronthosting.co.za> |
2013-08-23 12:19:54 | oscarbenjamin | link | issue18821 messages |
2013-08-23 12:19:54 | oscarbenjamin | create | |
|