[issue28939] Groupby Is Roughly Equivalent To ...

2016-12-11 Thread Greg Solomon

Greg Solomon added the comment:

Oh, I get it. There needs to be a next(self.it) loop in __next__ as well as in 
_grouper in case the user doesn't call _grouper.

My test was 
for ( k , g ) in groupby( L ):
print ( k , len( list( g ) ) )
so I was executing _grouper on every row.

Thanks !!!

--
resolution:  -> not a bug
status: open -> closed

___
Python tracker 

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



[issue28939] Groupby Is Roughly Equivalent To ...

2016-12-11 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
assignee: docs@python -> rhettinger
nosy: +rhettinger
priority: normal -> low

___
Python tracker 

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



[issue28939] Groupby Is Roughly Equivalent To ...

2016-12-11 Thread Julien Palard

Julien Palard added the comment:

I renamed your function groupby2 to compare it with itertools.groupby and 
tested but:

>>> print(list(groupby2(['A', 'B'])))

does not returns, looks like your implementation have a bug, so I tried:

>>> for k in groupby2(['A', 'B']):
... print(k)

and I'm getting loads of:

('A', )
('A', )
('A', )
('A', )
('A', )
('A', )
('A', )
('A', )
('A', )
('A', )

You may also want to test your implementation against 
https://github.com/python/cpython/blob/master/Lib/test/test_itertools.py#L699

--
nosy: +mdk

___
Python tracker 

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



[issue28939] Groupby Is Roughly Equivalent To ...

2016-12-11 Thread Greg Solomon

New submission from Greg Solomon:

https://docs.python.org/3/library/itertools.html#itertools.groupby

I found the "equivalent" code a bit hard to read. Is there any merit in 
changing it to something a bit like the following ?

Kind Rgds, Greg

class groupby:
def __init__( self , iterable , key_function = ( lambda x: x ) ):
self.iterable = iter( iterable )
self.key_function = key_function
self.FINISHED = object()
try:
self.next_value = next( self.iterable )
except StopIteration: 
self.next_value = self.FINISHED
def __iter__( self ):
return self
def __next__( self ):
if self.next_value == self.FINISHED:
raise StopIteration
self.group_key_value = self.key_function( self.next_value )
return ( self.group_key_value , self._group() )
def _group( self ):
while self.next_value != self.FINISHED \
  and self.group_key_value == self.key_function( self.next_value ):
yield self.next_value
try:
self.next_value = next( self.iterable )
except StopIteration:
self.next_value = self.FINISHED 
return

--
assignee: docs@python
components: Documentation
messages: 282943
nosy: docs@python, greg.solomon
priority: normal
severity: normal
status: open
title: Groupby Is Roughly Equivalent To ...
versions: Python 3.7

___
Python tracker 

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