Каплан Владимир Сергеевич 611 ВМК FilterQueue 18234
Демихов Валентин, 611 группа FilterQueue 17683
f1import asynciof1import asyncio
22
3class FilterQueue(asyncio.Queue):3class FilterQueue(asyncio.Queue):
44
n5    def __init__(self, *a, **b):n5    def __init__(self, *args, **kwargs):
6        super().__init__(*a, **b)6        super().__init__(*args, **kwargs)
7        self.window = None7        self.window = None
88
n9    def _put(self, y):n9    def _put(self, item):
10        self._queue.append(y)10        self._queue.append(item)
11        if self.window is None:11        if self.window is None:
n12            self.window = yn12            self.window = item
1313
n14    def __contains__(self, c):n14    def __contains__(self, checker):
15        for d in self._queue:15        for item in self._queue:
16            if c(d):16            if checker(item):
17                return True17                return True
18        return False18        return False
1919
20    def _get(self):20    def _get(self):
n21        e = self._queue.popleft()n21        item = self._queue.popleft()
22        if self._queue:22        if self._queue:
23            self.window = self._queue[0]23            self.window = self._queue[0]
24        else:24        else:
25            self.window = None25            self.window = None
n26        return en26        return item
2727
28    def later(self):28    def later(self):
29        if not self._queue:29        if not self._queue:
30            self.window = None30            self.window = None
31            raise asyncio.QueueEmpty31            raise asyncio.QueueEmpty
n32        g = self._get()n32        item = self._get()
33        self._put(g)33        self._put(item)
3434
n35    async def get(self, h=None):n35    async def get(self, checker=None):
36        if h is not None and h in self:36        if checker is not None and checker in self:
37            while not h(self.window):37            while not checker(self.window):
38                self.later()38                self.later()
39        while self.empty():39        while self.empty():
n40            i = self._get_loop().create_future()n40            future = self._get_loop().create_future()
41            self._getters.append(i)41            self._getters.append(future)
42            try:42            try:
n43                await in43                await future
44            except:44            except:
n45                i.cancel()n45                future.cancel()
46                try:46                try:
n47                    self._getters.remove(i)n47                    self._getters.remove(future)
48                except ValueError:48                except ValueError:
49                    pass49                    pass
t50                if not self.empty() and (not i.cancelled()):t50                if not self.empty() and (not future.cancelled()):
51                    self._wakeup_next(self._getters)51                    self._wakeup_next(self._getters)
52                raise52                raise
53        return self.get_nowait()53        return self.get_nowait()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op