def _enter(context): queue = context.get(QUEUE, dq()) if not queue: return succeed(context) stack = context.get(STACK, dq()) interceptor = queue.left new_queue = queue.popleft() new_stack = stack.appendleft(interceptor) d = _try_f(context.update({ QUEUE: new_queue, STACK: new_stack }), interceptor, 'enter') d.addCallback(_check_error) d.addCallback(_enter) return d
def test_non_empty(self): """ Enqueue interceptors to a non-empty queue. """ context = enqueue(empty_context, [1, 2, 3]) self.assertThat(enqueue(context, [4, 5, 6]), ContainsDict({QUEUE: Equals(dq(1, 2, 3, 4, 5, 6))}))
def test_empty(self): """ Enqueuing interceptors creates the queue if necessary. """ context = empty_context self.assertThat(enqueue(context, [1, 2, 3]), ContainsDict({QUEUE: Equals(dq(1, 2, 3))}))
def _leave(context): stack = context.get(STACK, dq()) if not stack: return succeed(context) interceptor = stack.left context = context.transform([STACK], lambda s: s.popleft()) if ERROR in context: d = _try_error(context, interceptor) else: d = _try_f(context, interceptor, 'leave') return d.addCallback(_leave)
def enqueue(context, interceptors): """ Add interceptors to the end of a context's execution queue. :param context: Context. :type interceptors: ``Iterable[`Interceptor`]`` :param interceptors: Iterable of interceptors. :return: Updated context. """ return context.transform([QUEUE], lambda xs: (xs or dq()).extend(interceptors))
def test_supports_weakref(): import weakref weakref.ref(dq(1, 2))
def test_literalish(): assert dq(1, 2, 3) == pdeque([1, 2, 3])
@attr.s(hash=True) class Player(object): """ A chess player. """ name = attr.ib() def piece(self, piece): return _OwnedPiece(player=self, piece=piece) WHITE = Player(name=u"white") BLACK = Player(name=u"black") PLAYERS = dq(WHITE, BLACK) _STANDARD = pmap([ (v(0, 1), WHITE.piece(Pawn())), (v(1, 1), WHITE.piece(Pawn())), (v(2, 1), WHITE.piece(Pawn())), (v(3, 1), WHITE.piece(Pawn())), (v(4, 1), WHITE.piece(Pawn())), (v(5, 1), WHITE.piece(Pawn())), (v(6, 1), WHITE.piece(Pawn())), (v(7, 1), WHITE.piece(Pawn())), (v(0, 6), BLACK.piece(Pawn())), (v(1, 6), BLACK.piece(Pawn())), (v(2, 6), BLACK.piece(Pawn())), (v(3, 6), BLACK.piece(Pawn())), (v(4, 6), BLACK.piece(Pawn())),