|
| 1 | +"""A fluent API for managing JSONPathMatch iterators.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import collections |
| 5 | +import itertools |
| 6 | +from typing import TYPE_CHECKING |
| 7 | +from typing import Iterable |
| 8 | +from typing import Iterator |
| 9 | +from typing import Optional |
| 10 | +from typing import Tuple |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from jsonpath import JSONPathMatch |
| 14 | + |
| 15 | + |
| 16 | +class Query: |
| 17 | + """A fluent API for managing `JSONPathMatch` iterators. |
| 18 | +
|
| 19 | + Usually you'll want to use `jsonpath.query()` or `JSONPathEnvironment.query()` |
| 20 | + to create instances of `Query` rather than instantiating `Query` directly. |
| 21 | +
|
| 22 | + Arguments: |
| 23 | + it: A `JSONPathMatch` iterable, as you'd get from `jsonpath.finditer()` or |
| 24 | + `JSONPathEnvironment.finditer()`. |
| 25 | +
|
| 26 | + **New in version 1.1.0** |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, it: Iterable[JSONPathMatch]) -> None: |
| 30 | + self._it = iter(it) |
| 31 | + |
| 32 | + def __iter__(self) -> Iterator[JSONPathMatch]: |
| 33 | + return self._it |
| 34 | + |
| 35 | + def take(self, n: int) -> Query: |
| 36 | + """Limit the result set to at most _n_ matches. |
| 37 | +
|
| 38 | + Raises: |
| 39 | + ValueError: If _n_ < 0. |
| 40 | + """ |
| 41 | + if n < 0: |
| 42 | + raise ValueError("can't take a negative number of matches") |
| 43 | + |
| 44 | + self._it = itertools.islice(self._it, n) |
| 45 | + return self |
| 46 | + |
| 47 | + def limit(self, n: int) -> Query: |
| 48 | + """Limit the result set to at most _n_ matches. |
| 49 | +
|
| 50 | + `limit()` is an alias of `take()`. |
| 51 | +
|
| 52 | + Raises: |
| 53 | + ValueError: If _n_ < 0. |
| 54 | + """ |
| 55 | + return self.take(n) |
| 56 | + |
| 57 | + def head(self, n: int) -> Query: |
| 58 | + """Take the first _n_ matches. |
| 59 | +
|
| 60 | + `head()` is an alias for `take()`. |
| 61 | +
|
| 62 | + Raises: |
| 63 | + ValueError: If _n_ < 0. |
| 64 | + """ |
| 65 | + return self.take(n) |
| 66 | + |
| 67 | + def drop(self, n: int) -> Query: |
| 68 | + """Skip up to _n_ matches from the result set. |
| 69 | +
|
| 70 | + Raises: |
| 71 | + ValueError: If _n_ < 0. |
| 72 | + """ |
| 73 | + if n < 0: |
| 74 | + raise ValueError("can't drop a negative number of matches") |
| 75 | + |
| 76 | + if n > 0: |
| 77 | + next(itertools.islice(self._it, n, n), None) |
| 78 | + |
| 79 | + return self |
| 80 | + |
| 81 | + def skip(self, n: int) -> Query: |
| 82 | + """Skip up to _n_ matches from the result set. |
| 83 | +
|
| 84 | + Raises: |
| 85 | + ValueError: If _n_ < 0. |
| 86 | + """ |
| 87 | + return self.drop(n) |
| 88 | + |
| 89 | + def tail(self, n: int) -> Query: |
| 90 | + """Drop matches up to the last _n_ matches. |
| 91 | +
|
| 92 | + Raises: |
| 93 | + ValueError: If _n_ < 0. |
| 94 | + """ |
| 95 | + if n < 0: |
| 96 | + raise ValueError("can't select a negative number of matches") |
| 97 | + |
| 98 | + self._it = iter(collections.deque(self._it, maxlen=n)) |
| 99 | + return self |
| 100 | + |
| 101 | + def values(self) -> Iterable[object]: |
| 102 | + """Return an iterable of objects associated with each match.""" |
| 103 | + return (m.obj for m in self._it) |
| 104 | + |
| 105 | + def locations(self) -> Iterable[str]: |
| 106 | + """Return an iterable of normalized paths for each match.""" |
| 107 | + return (m.path for m in self._it) |
| 108 | + |
| 109 | + def items(self) -> Iterable[Tuple[str, object]]: |
| 110 | + """Return an iterable of (object, normalized path) tuples for each match.""" |
| 111 | + return ((m.path, m.obj) for m in self._it) |
| 112 | + |
| 113 | + def first(self) -> Optional[JSONPathMatch]: |
| 114 | + """Return the first `JSONPathMatch` or `None` if there were no matches.""" |
| 115 | + try: |
| 116 | + return next(self._it) |
| 117 | + except StopIteration: |
| 118 | + return None |
| 119 | + |
| 120 | + def last(self) -> Optional[JSONPathMatch]: |
| 121 | + """Return the last `JSONPathMatch` or `None` if there were no matches.""" |
| 122 | + try: |
| 123 | + return next(iter(self.tail(1))) |
| 124 | + except StopIteration: |
| 125 | + return None |
0 commit comments