This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author oscarbenjamin
Recipients oscarbenjamin
Date 2013-08-23.12:19:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1377260394.63.0.429867593592.issue18821@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2013-08-23 12:19:54oscarbenjaminsetrecipients: + oscarbenjamin
2013-08-23 12:19:54oscarbenjaminsetmessageid: <1377260394.63.0.429867593592.issue18821@psf.upfronthosting.co.za>
2013-08-23 12:19:54oscarbenjaminlinkissue18821 messages
2013-08-23 12:19:54oscarbenjamincreate