[issue18821] Add .lastitem attribute to takewhile instances

2013-09-08 Thread Raymond Hettinger

Raymond Hettinger added the comment:

Oscar, the solution proposed by Serhiy looks like a better choice.

I'm wary of increasing the API complexity of the itertools.  Right now, their 
learnability is aided by having simple signatures and no side-values.

The itertools are modeled on functional tools in other languages with mature 
APIs.  I look to those languages to provide an indication of whether proposed 
features are needed in practice.  AFAICT, there is no precedent for a 
takewhile-with-failed-value combo.

I appreciate your request (especially because it was accompanied by a use case) 
but am going to decline.  IMO, the module as a whole is better served by 
keeping the tools simple and clean. 

If an individual itertool doesn't have an exact fit to a particular use case, 
it may indicate that the programmer would be better served by a simple 
generator which can express the logic more cleanly than a tricked-out itertool 
with side-values.

--
resolution:  - rejected
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18821] Add .lastitem attribute to takewhile instances

2013-09-08 Thread Oscar Benjamin

Oscar Benjamin added the comment:

Thank you Claudiu very much for writing a patch; I was expecting to
have to do that myself!

Serhiy, you're right groupby is a better fit for this. It does mean a
bit of reworking for the (more complicated) sum function I'm working
on but I've just checked with timeit and it performs very well using
the type function as a predicate. I think it might make the function a
few times faster than takewhile in my common cases for reasons that
are particular to this problem.

Raymond, thanks for taking the time to consider this. I agree that it
should now be closed.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18821] Add .lastitem attribute to takewhile instances

2013-09-07 Thread Claudiu.Popa

Claudiu.Popa added the comment:

Hello. Here's a basic patch with tests which accomplishes your request. 
Currently, it defaults to None if no item failed, but probably it can be set 
only when something fails the predicate (and the user will check using 
hasattr(t, 'last') ).

--
keywords: +patch
nosy: +Claudiu.Popa
Added file: http://bugs.python.org/file31647/itertools_takewhile.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18821] Add .lastitem attribute to takewhile instances

2013-09-07 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Oscar, did you considered itertools.groupby()? Perhaps it better meets your 
needs.

--
nosy: +serhiy.storchaka

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18821] Add .lastitem attribute to takewhile instances

2013-09-01 Thread Raymond Hettinger

Changes by Raymond Hettinger raymond.hettin...@gmail.com:


--
assignee:  - rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18821] Add .lastitem attribute to takewhile instances

2013-08-23 Thread Oscar Benjamin

New submission from Oscar Benjamin:

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

--
components: Library (Lib)
messages: 195962
nosy: oscarbenjamin
priority: normal
severity: normal
status: open
title: Add .lastitem attribute to takewhile instances
type: enhancement
versions: Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18821] Add .lastitem attribute to takewhile instances

2013-08-23 Thread Ned Deily

Changes by Ned Deily n...@acm.org:


--
nosy: +rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18821
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com