• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SlowAPI / fast-json-pointer / 3798344209

pending completion
3798344209

push

github

Tristan Sweeney
Clean up a bit

6 of 6 new or added lines in 2 files covered. (100.0%)

285 of 294 relevant lines covered (96.94%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/fast_json_pointer/verbs.py
1
from . import low_verbs
1✔
2
from .jsontypes import JsonType
1✔
3
from .pointer import JsonPointer, RelativeJsonPointer
1✔
4
from .resolver import JsonResolver, Operation, compile
1✔
5

6

7
def _compile(
1✔
8
    pointer: str | JsonPointer, rel: str | RelativeJsonPointer | None = None
9
) -> JsonResolver:
10
    if rel is not None:
1✔
11
        return JsonResolver.compile(pointer, rel)
1✔
12
    else:
13
        return JsonResolver.compile(pointer)
1✔
14

15

16
def get(
1✔
17
    doc: JsonType,
18
    pointer: str | JsonPointer,
19
    *,
20
    rel: str | RelativeJsonPointer | None = None,
21
) -> JsonType:
22
    """
23

24
    >>> get({}, "")
25
    {}
26
    >>> get({'x': 5}, "/x")
27
    5
28
    >>> get({'x': {'': 3}}, "/x/")
29
    3
30
    >>> get({'x': {'': 3, 'z': 12}}, "/x/", rel="1/z")
31
    12
32
    >>> get({'x': {'': 3}, 'z': 12}, "/x/", rel="1#")
33
    'x'
34
    >>> get([{'x': {'': 3}}, 4], "/0/x", rel="1#")
35
    '0'
36
    >>> get([{'x': {'': 3}}, 4], JsonPointer.parse("/0/x"))
37
    {'': 3}
38

39
    Trying to get fields that don't exist is a bad idea...
40

41
    >>> get([{'x': {'': 3}}, 4], "/0/x", rel="0//does-not-exist")
42
    Traceback (most recent call last):
43
    fast_json_pointer.exceptions.ResolutionException: ...
44
    >>> get([{'x': {'': 3}}, 4], "/3")
45
    Traceback (most recent call last):
46
    fast_json_pointer.exceptions.ResolutionException: ...
47
    >>> get([{'x': {'': 3}}, 4], "/0/z")
48
    Traceback (most recent call last):
49
    fast_json_pointer.exceptions.ResolutionException: ...
50
    """
51
    path = _compile(pointer, rel)
1✔
52
    return low_verbs.get(doc, path)
1✔
53

54

55
def add(
1✔
56
    doc: JsonType,
57
    pointer: str | JsonPointer,
58
    value: JsonType,
59
    *,
60
    rel: str | RelativeJsonPointer | None = None,
61
) -> None:
62
    """
63
    >>> obj = {}
64
    >>> add(obj, "/x", 2)
65
    >>> obj
66
    {'x': 2}
67

68
    >>> obj = {'x': 2}
69
    >>> add(obj, "", 'foo', rel="0/y")
70
    >>> obj
71
    {'x': 2, 'y': 'foo'}
72

73
    >>> obj = {'x': 2}
74
    >>> add(obj, "/x", 'foo', rel="1/x")
75
    >>> obj
76
    {'x': 'foo'}
77

78
    >>> obj = {'x': [0]}
79
    >>> add(obj, "/x", 'foo', rel="0/1")
80
    >>> obj
81
    {'x': [0, 'foo']}
82

83
    >>> obj = {'x': [0]}
84
    >>> add(obj, "/x", 'foo', rel="0/-")
85
    >>> obj
86
    {'x': [0, 'foo']}
87
    """
88
    path = _compile(pointer, rel)
1✔
89
    return low_verbs.add(doc, path, value)
1✔
90

91

92
def remove(
1✔
93
    doc: JsonType,
94
    pointer: str | JsonPointer,
95
    *,
96
    rel: str | RelativeJsonPointer | None = None,
97
) -> JsonType:
98
    """
99
    >>> obj = {'x': 2}
100
    >>> remove(obj, "/x")
101
    2
102
    >>> obj
103
    {}
104

105
    >>> obj = {'x': [0, 1, 2]}
106
    >>> remove(obj, "/x", rel="0/2")
107
    2
108
    >>> obj
109
    {'x': [0, 1]}
110
    """
111
    path = _compile(pointer, rel)
1✔
112
    return low_verbs.remove(doc, path)
1✔
113

114

115
def replace(
1✔
116
    doc: JsonType,
117
    pointer: str | JsonPointer,
118
    value: JsonType,
119
    *,
120
    rel: str | RelativeJsonPointer | None = None,
121
) -> JsonType:
122
    """
123
    >>> obj = {'x': 2}
124
    >>> replace(obj, "/x", ['foo'])
125
    2
126
    >>> obj
127
    {'x': ['foo']}
128

129
    >>> obj = {'x': 2}
130
    >>> replace(obj, "", ['foo'], rel="0/x")
131
    2
132
    >>> obj
133
    {'x': ['foo']}
134

135
    >>> obj = [0, 1, 2]
136
    >>> replace(obj, "/2", 'foo')
137
    2
138
    >>> obj
139
    [0, 1, 'foo']
140
    """
141
    path = _compile(pointer, rel)
1✔
142
    return low_verbs.replace(doc, path, value)
1✔
143

144

145
def move(
1✔
146
    doc: JsonType,
147
    from_: str | JsonPointer,
148
    pointer: str | JsonPointer,
149
    *,
150
    rel: str | RelativeJsonPointer | None = None,
151
    from_rel: str | RelativeJsonPointer | None = None,
152
) -> None:
153
    """
154
    >>> obj = {'x': 2}
155
    >>> move(obj, "/x", "/y")
156
    2
157
    >>> obj
158
    {'y': 2}
159
    """
160
    path = _compile(pointer, rel)
1✔
161
    from_path = _compile(from_, from_rel)
1✔
162
    return low_verbs.move(doc, path, from_path)
1✔
163

164

165
def copy(
1✔
166
    doc: JsonType,
167
    from_: str | JsonPointer,
168
    pointer: str | JsonPointer,
169
    *,
170
    rel: str | RelativeJsonPointer | None = None,
171
    from_rel: str | RelativeJsonPointer | None = None,
172
) -> None:
173
    """
174
    >>> obj = {'x': 2}
175
    >>> copy(obj, "/x", "/y")
176
    2
177
    >>> obj
178
    {'x': 2, 'y': 2}
179
    """
180
    path = _compile(pointer, rel)
1✔
181
    from_path = _compile(from_, from_rel)
1✔
182
    return low_verbs.copy(doc, path, from_path)
1✔
183

184

185
def test(
1✔
186
    doc: JsonType,
187
    pointer: str | JsonPointer,
188
    value: JsonType,
189
    *,
190
    rel: str | RelativeJsonPointer | None = None,
191
) -> bool:
192
    """
193
    >>> obj = {'x': 2}
194
    >>> test(obj, "/x", 2)
195
    True
196
    """
197
    path = _compile(pointer, rel)
1✔
198
    return low_verbs.test(doc, path, value)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc