|
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 | from typing import Iterable |
8 | 8 | from typing import Iterator |
| 9 | +from typing import List |
9 | 10 | from typing import Optional |
10 | 11 | from typing import Tuple |
11 | 12 |
|
@@ -33,47 +34,37 @@ def __init__(self, it: Iterable[JSONPathMatch]) -> None: |
33 | 34 | def __iter__(self) -> Iterator[JSONPathMatch]: |
34 | 35 | return self._it |
35 | 36 |
|
36 | | - def take(self, n: int) -> Query: |
| 37 | + def limit(self, n: int) -> Query: |
37 | 38 | """Limit the query iterator to at most _n_ matches. |
38 | 39 |
|
39 | 40 | Raises: |
40 | 41 | ValueError: If _n_ < 0. |
41 | 42 | """ |
42 | 43 | if n < 0: |
43 | | - raise ValueError("can't take a negative number of matches") |
| 44 | + raise ValueError("can't limit by a negative number of matches") |
44 | 45 |
|
45 | 46 | self._it = itertools.islice(self._it, n) |
46 | 47 | return self |
47 | 48 |
|
48 | | - def limit(self, n: int) -> Query: |
49 | | - """Limit the query iterator to at most _n_ matches. |
50 | | -
|
51 | | - `limit()` is an alias of `take()`. |
52 | | -
|
53 | | - Raises: |
54 | | - ValueError: If _n_ < 0. |
55 | | - """ |
56 | | - return self.take(n) |
57 | | - |
58 | 49 | def head(self, n: int) -> Query: |
59 | 50 | """Limit the query iterator to at most the first _n_ matches. |
60 | 51 |
|
61 | | - `head()` is an alias for `take()`. |
| 52 | + `head()` is an alias for `limit()`. |
62 | 53 |
|
63 | 54 | Raises: |
64 | 55 | ValueError: If _n_ < 0. |
65 | 56 | """ |
66 | | - return self.take(n) |
| 57 | + return self.limit(n) |
67 | 58 |
|
68 | 59 | def first(self, n: int) -> Query: |
69 | 60 | """Limit the query iterator to at most the first _n_ matches. |
70 | 61 |
|
71 | | - `first()` is an alias for `take()`. |
| 62 | + `first()` is an alias for `limit()`. |
72 | 63 |
|
73 | 64 | Raises: |
74 | 65 | ValueError: If _n_ < 0. |
75 | 66 | """ |
76 | | - return self.take(n) |
| 67 | + return self.limit(n) |
77 | 68 |
|
78 | 69 | def drop(self, n: int) -> Query: |
79 | 70 | """Skip up to _n_ matches from the query iterator. |
@@ -162,3 +153,10 @@ def tee(self, n: int = 2) -> Tuple[Query, ...]: |
162 | 153 | It is not safe to use a `Query` instance after calling `tee()`. |
163 | 154 | """ |
164 | 155 | return tuple(Query(it) for it in itertools.tee(self._it, n)) |
| 156 | + |
| 157 | + def take(self, n: int) -> Query: |
| 158 | + """Return a new query iterating over the next _n_ matches. |
| 159 | +
|
| 160 | + It is safe to continue using this query after calling take. |
| 161 | + """ |
| 162 | + return Query(list(itertools.islice(self._it, n))) |
0 commit comments