Additional refactoring to use the QuerySequence wrapper, so that we can still
[mailman.git] / src / mailman / utilities / queries.py
blob6f4bc6b4d57e50ba20e83ff40c4c8eab958174a4
1 # Copyright (C) 2016 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Some helpers for queries."""
20 __all__ = [
21 'QuerySequence',
25 from collections.abc import Sequence
28 class QuerySequence(Sequence):
29 def __init__(self, query=None):
30 super().__init__()
31 self._query = query
32 self._cached_results = None
34 def __len__(self):
35 return (0 if self._query is None else self._query.count())
37 def __getitem__(self, index):
38 if self._query is None:
39 raise IndexError('index out of range')
40 if self._cached_results is None:
41 self._cached_results = list(self._query)
42 return self._cached_results[index]