Skip to content

Commit da2a42f

Browse files
committed
tests n docs
1 parent 9ba5122 commit da2a42f

File tree

5 files changed

+396
-84
lines changed

5 files changed

+396
-84
lines changed

.github/workflows/jsonparse-buildtest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
pip install .[buildtest27]
2525
- name: Test with pytest and generate codecov
2626
run: |
27-
pytest --cov=jsonparse --ignore=tests/test_webapi.py
27+
pytest --cov=jsonparse --ignore=tests/test_webapi.py --ignore=tests/test_parser.py
2828
- uses: codecov/codecov-action@v3
2929
with:
3030
token: ${{ secrets.CODECOV_TOKEN }}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
> **jsonparse** is a simple JSON parsing library. Extract what's needed from key:value pairs.
1212
1313
## What's New
14+
- Python 2.7 compat. :sweat_smile: :relieved:
1415
- A new function, [find_value](#find_value), has been added. This function will return all keys of the matched value. :grinning:
1516
- [CLI tool](#CLI-tool). Parse json text files or stdin via the command line :tada:
1617

@@ -221,6 +222,10 @@ find_value(data, False)
221222
['cable']
222223
```
223224

225+
# Python 2.7 Usage
226+
227+
- 2.7 does not guarantee ordering of dictionary's. If ordering matters, use [OrderedDict](https://docs.python.org/2.7/library/collections.html) for all dictionary's in the data.
228+
224229
# Web API
225230

226231
## Documentation

src/jsonparse/parser.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, stack_trace=False, queue_trace=False):
5252
self.queue_ref = self._queue_init()
5353

5454
def find_key(self, data, key):
55-
# type: (Union[dict, list], str) -> list
55+
# type: (Union[dict, list, OrderedDict], str) -> list
5656
if not self._valid_key_input(data, key):
5757
raise
5858

@@ -80,7 +80,7 @@ def find_key(self, data, key):
8080
return value_list
8181

8282
def find_keys(self, data, keys, group=True):
83-
# type: (Union[dict, list], list, bool) -> list
83+
# type: (Union[dict, list, OrderedDict], list, bool) -> list
8484
if not self._valid_keys_input(data, keys, group):
8585
raise
8686

@@ -110,7 +110,7 @@ def find_keys(self, data, keys, group=True):
110110
return value_list
111111

112112
def find_key_chain(self, data, keys):
113-
# type: (Union[dict, list], list) -> list
113+
# type: (Union[dict, list, OrderedDict], list) -> list
114114
if not self._valid_key_chain_input(data, keys):
115115
raise
116116

@@ -148,7 +148,7 @@ def find_key_chain(self, data, keys):
148148
return self.queue_ref
149149

150150
def find_key_value(self, data, key, value):
151-
# type: (Union[dict, list], str, Union[str, int, float, bool, None]) -> list
151+
# type: (Union[dict, list, OrderedDict], str, Union[str, int, float, bool, None]) -> list
152152
if not self._valid_key_value_input(data, key, value):
153153
raise
154154

@@ -174,7 +174,7 @@ def find_key_value(self, data, key, value):
174174
return value_list
175175

176176
def find_value(self, data, value):
177-
# type: (Union[dict, list], Union[str, int, float, bool, None]) -> list
177+
# type: (Union[dict, list, OrderedDict], Union[str, int, float, bool, None]) -> list
178178
if not self._valid_value_input(data, value):
179179
raise
180180

@@ -208,18 +208,18 @@ def _stack_init(self):
208208
return stack
209209

210210
def _stack_push(self, elem):
211-
# type: (Union[dict, list]) -> None
211+
# type: (Union[dict, list, OrderedDict]) -> None
212212
self.stack_ref.append(elem)
213213

214214
def _stack_pop(self):
215-
# type: () -> Union[dict, list]
215+
# type: () -> Union[dict, list, OrderedDict]
216216
try:
217217
return self.stack_ref.pop()
218218
except IndexError:
219219
raise
220220

221221
def _stack_peak(self):
222-
# type: () -> Union[dict, list]
222+
# type: () -> Union[dict, list, OrderedDict]
223223
try:
224224
return self.stack_ref[-1:][0]
225225
except IndexError:
@@ -242,7 +242,7 @@ def _stack_push_list_elem(self, elem):
242242
self._stack_trace()
243243

244244
def _stack_all_key_values_in_dict(self, key, elem):
245-
# type: (str, dict) -> list
245+
# type: (str, Union[dict, OrderedDict]) -> list
246246
value_list = []
247247

248248
if not isinstance(elem, (dict, OrderedDict)):
@@ -262,7 +262,7 @@ def _stack_all_key_values_in_dict(self, key, elem):
262262
return value_list
263263

264264
def _stack_all_keys_values_in_dict(self, keys, elem):
265-
# type: (list, dict) -> list
265+
# type: (list, Union[dict, OrderedDict]) -> list
266266
value_list = []
267267

268268
if not isinstance(elem, (dict, OrderedDict)):
@@ -285,7 +285,7 @@ def _stack_all_keys_values_in_dict(self, keys, elem):
285285
return value_list
286286

287287
def _stack_all_key_and_value_in_dict(self, key, value, elem):
288-
# type: (str, Union[str, int, float, bool, None], dict) -> bool
288+
# type: (str, Union[str, int, float, bool, None], Union[dict, OrderedDict]) -> bool
289289
if not isinstance(elem, (dict, OrderedDict)):
290290
raise TypeError
291291
elif type(key) is not str:
@@ -305,7 +305,7 @@ def _stack_all_key_and_value_in_dict(self, key, value, elem):
305305
return False
306306

307307
def _stack_all_value_in_dict(self, value, elem):
308-
# type: (Union[str, int, float, bool, None], dict) -> str
308+
# type: (Union[str, int, float, bool, None], Union[dict, OrderedDict]) -> str
309309
if not isinstance(elem, (dict, OrderedDict)):
310310
raise TypeError
311311
elif not isinstance(value, (str, int, float, bool, type(None))):
@@ -339,18 +339,18 @@ def _queue_init(self):
339339
return queue
340340

341341
def _queue_push(self, elem):
342-
# type: (Union[dict, list]) -> None
342+
# type: (Union[dict, list, OrderedDict]) -> None
343343
self.queue_ref.append(elem)
344344

345345
def _queue_pop(self):
346-
# type: () -> Union[dict, list]
346+
# type: () -> Union[dict, list, OrderedDict]
347347
try:
348348
return self.queue_ref.pop(0)
349349
except IndexError:
350350
raise
351351

352352
def _queue_peak(self):
353-
# type: () -> Union[dict, list]
353+
# type: () -> Union[dict, list, OrderedDict]
354354
try:
355355
return self.queue_ref[0]
356356
except IndexError:
@@ -373,7 +373,7 @@ def _queue_push_list_elem(self, elem):
373373
self._queue_trace()
374374

375375
def _queue_all_key_values_in_dict(self, key, elem):
376-
# type: (str, dict) -> bool
376+
# type: (str, Union[dict, OrderedDict]) -> bool
377377
found = False
378378
if not isinstance(elem, (dict, OrderedDict)):
379379
raise TypeError

tests/test_parser.py

Lines changed: 76 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# local imports
66
from jsonparse.parser import Parser
7-
from collections import OrderedDict, deque
7+
from collections import OrderedDict
88

99
# 3rd part imports
1010
import pytest
@@ -21,72 +21,81 @@ def parser(self):
2121
def complex_json(self):
2222

2323
return [
24-
OrderedDict([
25-
("id", "0001"),
26-
("type", "donut"),
27-
("exists", True),
28-
("ppu", 0.55),
29-
("batters", OrderedDict([
30-
("batter", [
31-
OrderedDict([("id", "1001"), ("type", "Reg")]),
32-
OrderedDict([("id", "1002"), ("type", "Chocolate")]),
33-
OrderedDict([("id", "1003"), ("type", "Blueberry")]),
34-
OrderedDict([("id", "1004"), ("type", "Devil's Food")]),
35-
OrderedDict([("start", 5), ("end", 8)])
36-
])
37-
])),
38-
("topping", [
39-
OrderedDict([("id", "5001"), ("ty", "None")]),
40-
OrderedDict([("id", "5002"), ("type", "Glazed")]),
41-
OrderedDict([("id", "5003"), ("type", "Sugar")]),
42-
OrderedDict([("id", "5004"), ("type", "Powdered Sugar")]),
43-
OrderedDict([("id", "5005"), ("type", "Chocolate with Sprinkles")]),
44-
OrderedDict([("id", "5006"), ("type", "Chocolate")]),
45-
OrderedDict([("id", "5007"), ("type", "Maple")])
46-
]),
47-
("start", 22),
48-
("end", 99)
49-
]),
50-
OrderedDict([
51-
("id", "0002"),
52-
("type", "donut"),
53-
("exists", False),
54-
("ppu", 42),
55-
("batters", OrderedDict([
56-
("batter", [
57-
OrderedDict([("id", "1001"), ("type", "Rul")])
58-
])
59-
])),
60-
("top_stuff", [
61-
OrderedDict([("id", "5001"), ("typ", "None")]),
62-
OrderedDict([("id", "5002"), ("type", "Glazed")]),
63-
OrderedDict([("id", "5003"), ("type", "Sugar")]),
64-
OrderedDict([("id", "5004"), ("type", "Chocolate")]),
65-
OrderedDict([("id", "5005"), ("type", "Maple")])
66-
]),
67-
("start", 1),
68-
("end", 9)
69-
]),
70-
OrderedDict([
71-
("id", "0003"),
72-
("type", "donut"),
73-
("exists", None),
74-
("ppu", 7),
75-
("batters", OrderedDict([
76-
("batter", [
77-
OrderedDict([("id", "1001"), ("type", "Lar")]),
78-
OrderedDict([("id", "1002"), ("type", "Chocolate")])
79-
])
80-
])),
81-
("on_top_thing", [
82-
OrderedDict([("id", "5001"), ("type", "None")]),
83-
OrderedDict([("id", "5002"), ("type", "Glazed")]),
84-
OrderedDict([("id", "5003"), ("type", "Chocolate")]),
85-
OrderedDict([("id", "5004"), ("type", "Maple")])
86-
]),
87-
("start", 4),
88-
("end", 7)
89-
])
24+
{
25+
"id": "0001",
26+
"type": "donut",
27+
"exists": True,
28+
"ppu": 0.55,
29+
"batters":
30+
{
31+
"batter":
32+
[
33+
{"id": "1001", "type": "Reg"},
34+
{"id": "1002", "type": "Chocolate"},
35+
{"id": "1003", "type": "Blueberry"},
36+
{"id": "1004", "type": "Devil's Food"},
37+
{"start": 5, "end": 8}
38+
]
39+
},
40+
"topping":
41+
[
42+
{"id": "5001", "ty": "None"},
43+
{"id": "5002", "type": "Glazed"},
44+
{"id": "5003", "type": "Sugar"},
45+
{"id": "5004", "type": "Powdered Sugar"},
46+
{"id": "5005", "type": "Chocolate with Sprinkles"},
47+
{"id": "5006", "type": "Chocolate"},
48+
{"id": "5007", "type": "Maple"}
49+
],
50+
"start": 22,
51+
"end": 99
52+
},
53+
{
54+
"id": "0002",
55+
"type": "donut",
56+
"exists": False,
57+
"ppu": 42,
58+
"batters":
59+
{
60+
"batter":
61+
[
62+
{"id": "1001", "type": "Rul"}
63+
]
64+
},
65+
"top_stuff":
66+
[
67+
{"id": "5001", "typ": "None"},
68+
{"id": "5002", "type": "Glazed"},
69+
{"id": "5003", "type": "Sugar"},
70+
{"id": "5004", "type": "Chocolate"},
71+
{"id": "5005", "type": "Maple"}
72+
],
73+
"start": 1,
74+
"end": 9
75+
},
76+
{
77+
"id": "0003",
78+
"type": "donut",
79+
"exists": None,
80+
"ppu": 7,
81+
"batters":
82+
{
83+
"batter":
84+
[
85+
{"id": "1001", "type": "Lar"},
86+
{"id": "1002", "type": "Chocolate"}
87+
]
88+
},
89+
"on_top_thing":
90+
[
91+
{"id": "5001", "type": "None"},
92+
{"id": "5002", "type": "Glazed"},
93+
{"id": "5003", "type": "Chocolate"},
94+
{"id": "5004", "type": "Maple"}
95+
],
96+
"start": 4,
97+
"end": 7
98+
}
9099
]
91100

92101
def test_find_key(self, parser, complex_json):

0 commit comments

Comments
 (0)