Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions celery/utils/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ def options(self):
def subtask_type(self):
pass

@abstractproperty
def chord_size(self):
pass

@abstractproperty
def immutable(self):
pass
Expand Down
71 changes: 61 additions & 10 deletions celery/utils/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import inspect
import sys
from functools import partial
from itertools import chain, islice
from itertools import islice, tee

from kombu.utils.functional import (LRUCache, dictfilter, is_list, lazy,
maybe_evaluate, maybe_list, memoize)
from vine import promise

from celery.five import UserList, getfullargspec, range
from celery.five import UserList, getfullargspec, range, zip_longest

__all__ = (
'LRUCache', 'is_list', 'maybe_list', 'memoize', 'mlazy', 'noop',
'first', 'firstmethod', 'chunks', 'padlist', 'mattrgetter', 'uniq',
'regen', 'dictfilter', 'lazy', 'maybe_evaluate', 'head_from_fun',
'maybe', 'fun_accepts_kwargs',
'lookahead', 'regen', 'dictfilter', 'lazy', 'maybe_evaluate',
'head_from_fun', 'maybe', 'fun_accepts_kwargs',
)

IS_PY3 = sys.version_info[0] == 3
Expand Down Expand Up @@ -167,6 +167,22 @@ def uniq(it):
return (seen.add(obj) or obj for obj in it if obj not in seen)


def lookahead(it):
"""Yield pairs of (current, next) items in `it`.

`next` is None if `current` is the last item.

Example:

>>> list(lookahead(x for x in range(6)))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, None)]

"""
a, b = tee(it)
next(b, None)
return zip_longest(a, b, fillvalue=None)


def regen(it):
"""Convert iterator to an object that can be consumed multiple times.

Expand All @@ -189,38 +205,73 @@ def __init__(self, it):
self.__it = it
self.__index = 0
self.__consumed = []
self.__done = False

def __reduce__(self):
return list, (self.data,)

def __length_hint__(self):
return self.__it.__length_hint__()

def __repr__(self):
# override list.__repr__ to avoid consuming the generator
if self.__done:
return repr(self.__consumed)
else:
return '[{0}]'.format(', '.join(
[repr(x) for x in self.__consumed] + ['...'])
)

def __iter__(self):
return chain(self.__consumed, self.__it)
for x in self.__consumed:
yield x
if not self.__done:
for y in self.__it:
self.__consumed.append(y)
yield y
self.__done = True

def __getitem__(self, index):
if index < 0:
return self.data[index]
try:
return self.__consumed[index]
except IndexError:
it = iter(self)
try:
for _ in range(self.__index, index + 1):
self.__consumed.append(next(self.__it))
next(it)
except StopIteration:
raise IndexError(index)
else:
return self.__consumed[index]

@property
def data(self):
def __nonzero__(self):
# nonzero for list calls len() which would consume the generator:
# override to consume maximum of one item.
if len(self.__consumed):
return True
try:
self.__consumed.extend(list(self.__it))
next(iter(self))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear that an item is not lost because of https://github.com/celery/celery/pull/4459/files#diff-427d1dbcd92e9e366527f44ff54821a1R226
Please add a comment explaining what happens when you do so.

except StopIteration:
pass
return False
else:
return True

# Python3

__bool__ = __nonzero__

@property
def data(self):
# consume the generator
list(iter(self))
return self.__consumed

def fully_consumed(self):
"""Return whether the iterator has been fully consumed"""
return self.__done


def _argsfromspec(spec, replace_defaults=True):
if spec.defaults:
Expand Down
9 changes: 7 additions & 2 deletions t/unit/utils/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from celery.utils.functional import (DummyContext, first, firstmethod,
fun_accepts_kwargs, fun_takes_argument,
head_from_fun, maybe_list, mlazy,
padlist, regen, seq_concat_item,
seq_concat_seq)
padlist, lookahead, regen,
seq_concat_item, seq_concat_seq)


def test_DummyContext():
Expand Down Expand Up @@ -70,6 +70,11 @@ def predicate(value):
assert iterations[0] == 10


def test_lookahead(self):
assert list(lookahead(x for x in range(6)) == [
(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, None)])


def test_maybe_list():
assert maybe_list(1) == [1]
assert maybe_list([1]) == [1]
Expand Down