Repository navigation
Expand file tree
/
Copy pathfluent_api.py
More file actions
161 lines (119 loc) · 4.68 KB
/
Copy pathfluent_api.py
File metadata and controls
161 lines (119 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""A fluent API for managing JSONPathMatch iterators."""
from __future__ import annotations
import collections
import itertools
from typing import TYPE_CHECKING
from typing import Iterable
from typing import Iterator
from typing import Optional
from typing import Tuple
if TYPE_CHECKING:
from jsonpath import JSONPathMatch
from jsonpath import JSONPointer
class Query:
"""A fluent API for managing `JSONPathMatch` iterators.
Usually you'll want to use `jsonpath.query()` or `JSONPathEnvironment.query()`
to create instances of `Query` rather than instantiating `Query` directly.
Arguments:
it: A `JSONPathMatch` iterable, as you'd get from `jsonpath.finditer()` or
`JSONPathEnvironment.finditer()`.
**New in version 1.1.0**
"""
def __init__(self, it: Iterable[JSONPathMatch]) -> None:
self._it = iter(it)
def __iter__(self) -> Iterator[JSONPathMatch]:
return self._it
def limit(self, n: int) -> Query:
"""Limit the query iterator to at most _n_ matches.
Raises:
ValueError: If _n_ < 0.
"""
if n < 0:
raise ValueError("can't limit by a negative number of matches")
self._it = itertools.islice(self._it, n)
return self
def head(self, n: int) -> Query:
"""Limit the query iterator to at most the first _n_ matches.
`head()` is an alias for `limit()`.
Raises:
ValueError: If _n_ < 0.
"""
return self.limit(n)
def first(self, n: int) -> Query:
"""Limit the query iterator to at most the first _n_ matches.
`first()` is an alias for `limit()`.
Raises:
ValueError: If _n_ < 0.
"""
return self.limit(n)
def drop(self, n: int) -> Query:
"""Skip up to _n_ matches from the query iterator.
Raises:
ValueError: If _n_ < 0.
"""
if n < 0:
raise ValueError("can't drop a negative number of matches")
if n > 0:
next(itertools.islice(self._it, n, n), None)
return self
def skip(self, n: int) -> Query:
"""Skip up to _n_ matches from the query iterator.
Raises:
ValueError: If _n_ < 0.
"""
return self.drop(n)
def tail(self, n: int) -> Query:
"""Drop matches up to the last _n_ matches from the iterator.
Raises:
ValueError: If _n_ < 0.
"""
if n < 0:
raise ValueError("can't select a negative number of matches")
self._it = iter(collections.deque(self._it, maxlen=n))
return self
def last(self, n: int) -> Query:
"""Drop up to the last _n_ matches from the iterator.
`last()` is an alias for `tail()`.
Raises:
ValueError: If _n_ < 0.
"""
return self.tail(n)
def values(self) -> Iterable[object]:
"""Return an iterable of objects associated with each match."""
return (m.obj for m in self._it)
def locations(self) -> Iterable[str]:
"""Return an iterable of normalized paths, one for each match."""
return (m.path for m in self._it)
def items(self) -> Iterable[Tuple[str, object]]:
"""Return an iterable of (object, path) tuples, one for each match."""
return ((m.path, m.obj) for m in self._it)
def pointers(self) -> Iterable[JSONPointer]:
"""Return an iterable of JSONPointers, one for each match."""
return (m.pointer() for m in self._it)
def first_one(self) -> Optional[JSONPathMatch]:
"""Return the first `JSONPathMatch` or `None` if there were no matches."""
try:
return next(self._it)
except StopIteration:
return None
def one(self) -> Optional[JSONPathMatch]:
"""Return the first `JSONPathMatch` or `None` if there were no matches.
`one()` is an alias for `first_one()`.
"""
return self.first_one()
def last_one(self) -> Optional[JSONPathMatch]:
"""Return the last `JSONPathMatch` or `None` if there were no matches."""
try:
return next(iter(self.tail(1)))
except StopIteration:
return None
def tee(self, n: int = 2) -> Tuple[Query, ...]:
"""Return _n_ independent queries by teeing this query's iterator.
It is not safe to use a `Query` instance after calling `tee()`.
"""
return tuple(Query(it) for it in itertools.tee(self._it, n))
def take(self, n: int) -> Query:
"""Return a new query iterating over the next _n_ matches.
It is safe to continue using this query after calling take.
"""
return Query(list(itertools.islice(self._it, n)))