예제 #1
0
    def test_regen_gen(self):
        g = regen(iter(list(range(10))))
        self.assertEqual(g[7], 7)
        self.assertEqual(g[6], 6)
        self.assertEqual(g[5], 5)
        self.assertEqual(g[4], 4)
        self.assertEqual(g[3], 3)
        self.assertEqual(g[2], 2)
        self.assertEqual(g[1], 1)
        self.assertEqual(g[0], 0)
        self.assertEqual(g.data, list(range(10)))
        self.assertEqual(g[8], 8)
        self.assertEqual(g[0], 0)
        g = regen(iter(list(range(10))))
        self.assertEqual(g[0], 0)
        self.assertEqual(g[1], 1)
        self.assertEqual(g.data, list(range(10)))
        g = regen(iter([1]))
        self.assertEqual(g[0], 1)
        with self.assertRaises(IndexError):
            g[1]
        self.assertEqual(g.data, [1])

        g = regen(iter(list(range(10))))
        self.assertEqual(g[-1], 9)
        self.assertEqual(g[-2], 8)
        self.assertEqual(g[-3], 7)
        self.assertEqual(g[-4], 6)
        self.assertEqual(g[-5], 5)
        self.assertEqual(g[5], 5)
        self.assertEqual(g.data, list(range(10)))

        self.assertListEqual(list(iter(g)), list(range(10)))
예제 #2
0
    def test_gen(self):
        g = regen(iter(list(range(10))))
        assert g[7] == 7
        assert g[6] == 6
        assert g[5] == 5
        assert g[4] == 4
        assert g[3] == 3
        assert g[2] == 2
        assert g[1] == 1
        assert g[0] == 0
        assert g.data, list(range(10))
        assert g[8] == 8
        assert g[0] == 0
        g = regen(iter(list(range(10))))
        assert g[0] == 0
        assert g[1] == 1
        assert g.data == list(range(10))
        g = regen(iter([1]))
        assert g[0] == 1
        with pytest.raises(IndexError):
            g[1]
        assert g.data == [1]

        g = regen(iter(list(range(10))))
        assert g[-1] == 9
        assert g[-2] == 8
        assert g[-3] == 7
        assert g[-4] == 6
        assert g[-5] == 5
        assert g[5] == 5
        assert g.data == list(range(10))

        assert list(iter(g)) == list(range(10))
예제 #3
0
    def test_regen_gen(self):
        g = regen(iter(list(range(10))))
        self.assertEqual(g[7], 7)
        self.assertEqual(g[6], 6)
        self.assertEqual(g[5], 5)
        self.assertEqual(g[4], 4)
        self.assertEqual(g[3], 3)
        self.assertEqual(g[2], 2)
        self.assertEqual(g[1], 1)
        self.assertEqual(g[0], 0)
        self.assertEqual(g.data, list(range(10)))
        self.assertEqual(g[8], 8)
        self.assertEqual(g[0], 0)
        g = regen(iter(list(range(10))))
        self.assertEqual(g[0], 0)
        self.assertEqual(g[1], 1)
        self.assertEqual(g.data, list(range(10)))
        g = regen(iter([1]))
        self.assertEqual(g[0], 1)
        with self.assertRaises(IndexError):
            g[1]
        self.assertEqual(g.data, [1])

        g = regen(iter(list(range(10))))
        self.assertEqual(g[-1], 9)
        self.assertEqual(g[-2], 8)
        self.assertEqual(g[-3], 7)
        self.assertEqual(g[-4], 6)
        self.assertEqual(g[-5], 5)
        self.assertEqual(g[5], 5)
        self.assertEqual(g.data, list(range(10)))

        self.assertListEqual(list(iter(g)), list(range(10)))
예제 #4
0
    def test_regen_list(self):
        l = [1, 2]
        r = regen(iter(l))
        self.assertIs(regen(l), l)
        self.assertEqual(r, l)
        self.assertEqual(r, l)
        self.assertEqual(r.__length_hint__(), 0)

        fun, args = r.__reduce__()
        self.assertEqual(fun(*args), l)
예제 #5
0
    def test_regen_list(self):
        l = [1, 2]
        r = regen(iter(l))
        self.assertIs(regen(l), l)
        self.assertEqual(r, l)
        self.assertEqual(r, l)
        self.assertEqual(r.__length_hint__(), 0)

        fun, args = r.__reduce__()
        self.assertEqual(fun(*args), l)
예제 #6
0
    def test_list(self):
        l = [1, 2]
        r = regen(iter(l))
        assert regen(l) is l
        assert r == l
        assert r == l  # again
        assert r.__length_hint__() == 0

        fun, args = r.__reduce__()
        assert fun(*args) == l
예제 #7
0
파일: canvas.py 프로젝트: YuelianINC/celery
 def __init__(self, *tasks, **options):
     tasks = (regen(tasks[0]) if len(tasks) == 1 and is_list(tasks[0])
              else tasks)
     Signature.__init__(
         self, 'celery.chain', (), {'tasks': tasks}, **options
     )
     self.subtask_type = 'chain'
예제 #8
0
 def __init__(self, *tasks, **options):
     tasks = (regen(tasks[0])
              if len(tasks) == 1 and is_list(tasks[0]) else tasks)
     Signature.__init__(self, 'celery.chain', (), {'tasks': tasks},
                        **options)
     self.tasks = tasks
     self.subtask_type = 'chain'
예제 #9
0
    def test_partial_reconcretisation(self):
        class WeirdIterator():
            def __init__(self, iter_):
                self.iter_ = iter_
                self._errored = False

            def __iter__(self):
                yield from self.iter_
                if not self._errored:
                    try:
                        # This should stop the regen instance from marking
                        # itself as being done
                        raise AssertionError("Iterator errored")
                    finally:
                        self._errored = True

        original_list = list(range(42))
        g = regen(WeirdIterator(original_list))
        iter_g = iter(g)
        for e in original_list:
            assert e == next(iter_g)
        with pytest.raises(AssertionError, match="Iterator errored"):
            next(iter_g)
        # The following checks are for the known "misbehaviour"
        assert getattr(g, "_regen__done") is False
        # If the `regen()` instance doesn't think it's done then it'll dupe the
        # elements from the underlying iterator if it can be re-used
        iter_g = iter(g)
        for e in original_list * 2:
            assert next(iter_g) == e
        with pytest.raises(StopIteration):
            next(iter_g)
        assert getattr(g, "_regen__done") is True
        # Finally we xfail this test to keep track of it
        raise pytest.xfail(reason="#6794")
예제 #10
0
    def test_repr(self):
        def die():
            raise AssertionError("Generator died")
            yield None

        # Confirm that `regen()` instances are not concretised when represented
        g = regen(die())
        assert "..." in repr(g)
예제 #11
0
파일: canvas.py 프로젝트: bigtreeljc/celery
def _maybe_group(tasks):
    if isinstance(tasks, group):
        tasks = list(tasks.tasks)
    elif isinstance(tasks, abstract.CallableSignature):
        tasks = [tasks]
    else:
        tasks = [signature(t) for t in regen(tasks)]
    return tasks
예제 #12
0
파일: canvas.py 프로젝트: zzkristy/celery
 def __init__(self, *tasks, **options):
     tasks = (regen(tasks[0])
              if len(tasks) == 1 and is_list(tasks[0]) else tasks)
     Signature.__init__(self, 'celery.chain', (), {'tasks': tasks},
                        **options)
     self._use_link = options.pop('use_link', None)
     self.subtask_type = 'chain'
     self._frozen = None
예제 #13
0
 def test_deque(self):
     original_list = [42]
     d = collections.deque(original_list)
     # Confirm that concretising a `regen()` instance repeatedly for an
     # equality check always returns the original list
     g = regen(d)
     assert g == original_list
     assert g == original_list
예제 #14
0
 def __init__(self, task, it, **options):
     Signature.__init__(self,
                        self._task_name, (), {
                            'task': task,
                            'it': regen(it)
                        },
                        immutable=True,
                        **options)
예제 #15
0
def _maybe_group(tasks):
    if isinstance(tasks, group):
        tasks = list(tasks.tasks)
    elif isinstance(tasks, Signature):
        tasks = [tasks]
    else:
        tasks = regen(tasks)
    return tasks
예제 #16
0
def _maybe_group(tasks):
    if isinstance(tasks, group):
        tasks = list(tasks.tasks)
    elif isinstance(tasks, abstract.CallableSignature):
        tasks = [tasks]
    else:
        tasks = [signature(t) for t in regen(tasks)]
    return tasks
예제 #17
0
파일: canvas.py 프로젝트: AnSavvides/celery
def _maybe_group(tasks):
    if isinstance(tasks, group):
        tasks = list(tasks.tasks)
    elif isinstance(tasks, Signature):
        tasks = [tasks]
    else:
        tasks = regen(tasks)
    return tasks
예제 #18
0
    def test_nonzero__does_not_consume_more_than_first_item(self):
        def build_generator():
            yield 1
            pytest.fail("generator should not consume past first item")
            yield 2

        g = regen(build_generator())
        assert bool(g)
        assert g[0] == 1
예제 #19
0
파일: canvas.py 프로젝트: tothegump/celery
 def __init__(self, *tasks, **options):
     tasks = (regen(tasks[0]) if len(tasks) == 1 and is_list(tasks[0])
              else tasks)
     Signature.__init__(
         self, 'celery.chain', (), {'tasks': tasks}, **options
     )
     self._use_link = options.pop('use_link', None)
     self.subtask_type = 'chain'
     self._frozen = None
예제 #20
0
 def __init__(self, task, it, n, **options):
     Signature.__init__(self,
                        'celery.chunks', (), {
                            'task': task,
                            'it': regen(it),
                            'n': n
                        },
                        immutable=True,
                        **options)
예제 #21
0
파일: canvas.py 프로젝트: zzkristy/celery
 def __init__(self, *tasks, **options):
     if len(tasks) == 1:
         tasks = tasks[0]
         if isinstance(tasks, group):
             tasks = tasks.tasks
         if not isinstance(tasks, _regen):
             tasks = regen(tasks)
     Signature.__init__(self, 'celery.group', (), {'tasks': tasks},
                        **options)
     self.subtask_type = 'group'
예제 #22
0
파일: canvas.py 프로젝트: brakhane/celery
def _maybe_group(tasks, app):
    if isinstance(tasks, dict):
        tasks = signature(tasks, app=app)

    if isinstance(tasks, group):
        tasks = tasks.tasks
    elif isinstance(tasks, abstract.CallableSignature):
        tasks = [tasks]
    else:
        tasks = [signature(t, app=app) for t in regen(tasks)]
    return tasks
예제 #23
0
    def test_nonzero__does_not_consume_more_than_first_item(self):
        def build_generator():
            yield 1
            self.consumed_second_item = True
            yield 2

        self.consumed_second_item = False
        g = regen(build_generator())
        assert bool(g)
        assert g[0] == 1
        assert not self.consumed_second_item
예제 #24
0
def _maybe_group(tasks, app):
    if isinstance(tasks, dict):
        tasks = signature(tasks, app=app)

    if isinstance(tasks, group):
        tasks = tasks.tasks
    elif isinstance(tasks, abstract.CallableSignature):
        tasks = [tasks]
    else:
        tasks = [signature(t, app=app) for t in regen(tasks)]
    return tasks
예제 #25
0
파일: canvas.py 프로젝트: buckensl/celery
 def __init__(self, *tasks, **options):
     if len(tasks) == 1:
         tasks = tasks[0]
         if isinstance(tasks, group):
             tasks = tasks.tasks
         if not isinstance(tasks, _regen):
             tasks = regen(tasks)
     Signature.__init__(
         self, 'celery.group', (), {'tasks': tasks}, **options
     )
     self.subtask_type = 'group'
예제 #26
0
    def test_lookahead_consume(self, subtests):
        """
        Confirm that regen looks ahead by a single item as expected.
        """
        def g():
            yield from ["foo", "bar"]
            raise pytest.fail("This should never be reached")

        with subtests.test(msg="bool does not overconsume"):
            assert bool(regen(g()))
        with subtests.test(msg="getitem 0th does not overconsume"):
            assert regen(g())[0] == "foo"
        with subtests.test(msg="single iter does not overconsume"):
            assert next(iter(regen(g()))) == "foo"

        class ExpectedException(BaseException):
            pass

        def g2():
            yield from ["foo", "bar"]
            raise ExpectedException()

        with subtests.test(msg="getitem 1th does overconsume"):
            r = regen(g2())
            with pytest.raises(ExpectedException):
                r[1]
            # Confirm that the item was concretised anyway
            assert r[1] == "bar"
        with subtests.test(msg="full iter does overconsume"):
            r = regen(g2())
            with pytest.raises(ExpectedException):
                for _ in r:
                    pass
            # Confirm that the items were concretised anyway
            assert r == ["foo", "bar"]
        with subtests.test(msg="data access does overconsume"):
            r = regen(g2())
            with pytest.raises(ExpectedException):
                r.data
            # Confirm that the items were concretised anyway
            assert r == ["foo", "bar"]
예제 #27
0
 def __init__(self, task, it, n, **options):
     Signature.__init__(self, "celery.chunks", (), {
         "task": task,
         "it": regen(it),
         "n": n
     }, **options)
예제 #28
0
 def __init__(self, task, it, **options):
     Signature.__init__(self, self._task_name, (), {
         "task": task,
         "it": regen(it)
     }, **options)
예제 #29
0
 def __init__(self, header, body=None, **options):
     Signature.__init__(self, 'celery.chord', (), {
         'header': regen(header),
         'body': maybe_subtask(body)
     }, options)
     self.subtask_type = 'chord'
예제 #30
0
파일: canvas.py 프로젝트: aliscott/celery
def _maybe_group(tasks):
    if isinstance(tasks, group):
        tasks = list(tasks.tasks)
    else:
        tasks = regen(tasks if is_list(tasks) else tasks)
    return tasks
예제 #31
0
 def g(self):
     return regen(iter(list(range(10))))
예제 #32
0
 def test_nonzero__empty_iter(self):
     assert not regen(iter([]))
예제 #33
0
파일: canvas.py 프로젝트: rossdeane/celery
 def __init__(self, task, it, n, **options):
     Signature.__init__(
         self, "celery.chunks", (), {"task": task, "it": regen(it), "n": n}, immutable=True, **options
     )
예제 #34
0
파일: canvas.py 프로젝트: rossdeane/celery
 def __init__(self, task, it, **options):
     Signature.__init__(self, self._task_name, (), {"task": task, "it": regen(it)}, immutable=True, **options)
예제 #35
0
 def __init__(self, *tasks, **options):
     tasks = regen(
         tasks[0] if len(tasks) == 1 and is_list(tasks[0]) else tasks)
     Signature.__init__(self, "celery.group", (), {"tasks": tasks}, options)
     self.tasks, self.subtask_type = tasks, "group"
예제 #36
0
 def __init__(self, header, body=None, **options):
     Signature.__init__(self, "celery.chord", (), {
         "header": regen(header),
         "body": maybe_subtask(body)
     }, options)
     self.subtask_type = "chord"
예제 #37
0
def _maybe_group(tasks):
    if isinstance(tasks, group):
        tasks = list(tasks.tasks)
    else:
        tasks = regen(tasks if is_list(tasks) else tasks)
    return tasks
예제 #38
0
 def __init__(self, *tasks, **options):
     tasks = regen(tasks[0] if len(tasks) == 1 and is_list(tasks[0])
                            else tasks)
     Signature.__init__(self, "celery.group", (), {"tasks": tasks}, options)
     self.tasks, self.subtask_type = tasks, "group"
예제 #39
0
파일: canvas.py 프로젝트: tothegump/celery
 def __init__(self, task, it, **options):
     Signature.__init__(
         self, self._task_name, (),
         {'task': task, 'it': regen(it)}, immutable=True, **options
     )
예제 #40
0
파일: canvas.py 프로젝트: tothegump/celery
 def __init__(self, task, it, n, **options):
     Signature.__init__(
         self, 'celery.chunks', (),
         {'task': task, 'it': regen(it), 'n': n},
         immutable=True, **options
     )
예제 #41
0
 def __init__(self, header, body=None, **options):
     Signature.__init__(self, "celery.chord", (),
                      {"header": regen(header),
                       "body": maybe_subtask(body)}, options)
     self.subtask_type = "chord"
예제 #42
0
파일: canvas.py 프로젝트: rossdeane/celery
 def __init__(self, *tasks, **options):
     tasks = regen(tasks[0]) if len(tasks) == 1 and is_list(tasks[0]) else tasks
     Signature.__init__(self, "celery.chain", (), {"tasks": tasks}, **options)
     self.subtask_type = "chain"
예제 #43
0
파일: canvas.py 프로젝트: xeneta/celery
 def __init__(self, *tasks, **options):
     tasks = regen(tasks[0]) if len(tasks) == 1 and is_list(tasks[0]) else tasks
     Signature.__init__(self, "celery.chain", (), {"tasks": tasks}, **options)
     self._use_link = options.pop("use_link", None)
     self.subtask_type = "chain"
     self._frozen = None