Skip to content

Commit eba8f13

Browse files
authored
Merge pull request #6 from ctomkow/rename_api_calls
api method renaming
2 parents 5c1a2ea + fa8bbaf commit eba8f13

File tree

4 files changed

+48
-52
lines changed

4 files changed

+48
-52
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,46 @@ data = [
4242
]
4343

4444

45-
parser.key(data, 'chain')
45+
parser.find_key(data, 'chain')
4646
['B', 'A']
4747

48-
parser.key(data, 'key')
48+
parser.find_key(data, 'key')
4949
[{'chain': 'B'}, {'chain': 'A', 'rope': 5, 'string': 1.2, 'cable': False}, 2, 1]
5050

5151

52-
parser.key_chain(data, ['my', 'key', 'chain'])
52+
parser.find_key_chain(data, ['my', 'key', 'chain'])
5353
['A']
5454

55-
parser.key_chain(data, ['key'])
55+
parser.find_key_chain(data, ['key'])
5656
[1, 2]
5757

58-
parser.key_chain(data, ['*', 'key', 'chain'])
58+
parser.find_key_chain(data, ['*', 'key', 'chain'])
5959
['A', 'B']
6060

61-
parser.key_chain(data, ['*', 'key', '*'])
61+
parser.find_key_chain(data, ['*', 'key', '*'])
6262
['A', 5, 1.2, False, 'B']
6363

6464

65-
parser.key_value(data, 'cable', False)
65+
parser.find_key_value(data, 'cable', False)
6666
[{'chain': 'A', 'rope': 5, 'string': 1.2, 'cable': False}]
6767

68-
parser.key_value(data, 'chain', 'B')
68+
parser.find_key_value(data, 'chain', 'B')
6969
[{'chain': 'B'}]
7070
```
7171
### API
72-
`key(data: dict | list, key: str) -> list`
72+
`find_key(data: dict | list, key: str) -> list`
7373

7474
- Provide JSON data as a dictionary or a list, as well as the key as a string
7575
- Returns a list of values that match the corresponding key.
7676

77-
`key_chain(data: dict | list, keys: list) -> list`
77+
`find_key_chain(data: dict | list, keys: list) -> list`
7878

7979
- Provide JSON data as a dictionary or a list, as well as a list of keys as strings.
8080
- Returns a list of values that match the corresponding key chain.
8181

8282
> Wildcard **'*'** can be used as key(s) to match any.
8383
84-
`key_value(data: dict | list, key: str, value: str | int | float | bool) -> list`
84+
`find_key_value(data: dict | list, key: str, value: str | int | float | bool) -> list`
8585

8686
- Provide JSON data as a dictionary or a list, a key as a string,
8787
and a value as a string, integer, float, or boolean.

jsonparse/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.0
1+
0.8.0

jsonparse/parser.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Craig Tomkow
22
#
3-
# Dynamically parse JSON objects via two main methods...
4-
#
5-
# key: find all values of one key (stack based)
6-
# key_chain: find all values of an ordered key chain (queue based)
7-
# key_value: find all key:value pairs and return the parent set(s)
3+
# Dynamically parse JSON objects
84

95
# python imports
106
from typing import Union
@@ -19,13 +15,13 @@ class Parser:
1915
__init__(stack_trace, queue_trace):
2016
Returns the Parser object.
2117
22-
key(data, key):
18+
find_key(data, key):
2319
Returns a list of values that have the corresponding key.
2420
25-
key_chain(data, keys):
21+
find_key_chain(data, keys):
2622
Returns a list of values that have the corresponding key chain.
2723
28-
key_value(data, key, value):
24+
find_key_value(data, key, value):
2925
Returns a list of set(s) that contain the key value pair.
3026
"""
3127

@@ -49,7 +45,7 @@ def __init__(self,
4945
self.queue_ref = self._queue_init()
5046

5147
# depth first search for all keys using a STACK
52-
def key(self, data: Union[dict, list], key: str) -> list:
48+
def find_key(self, data: Union[dict, list], key: str) -> list:
5349
"""
5450
Search JSON data that consists of key:value pairs for all instances of
5551
provided key. The data can have complex nested dictionaries and lists.
@@ -92,7 +88,7 @@ def key(self, data: Union[dict, list], key: str) -> list:
9288
return value_list
9389

9490
# breadth first search for ordered series of keys using a QUEUE
95-
def key_chain(self, data: Union[dict, list], keys: list) -> list:
91+
def find_key_chain(self, data: Union[dict, list], keys: list) -> list:
9692
"""
9793
Search JSON data that consists of key:value pairs for the first
9894
instance of provided key chain. The data can have complex nested
@@ -147,10 +143,10 @@ def key_chain(self, data: Union[dict, list], keys: list) -> list:
147143

148144
return self.queue_ref
149145

150-
def key_value(self,
151-
data: Union[dict, list],
152-
key: str,
153-
value: Union[str, int, float, bool]) -> list:
146+
def find_key_value(self,
147+
data: Union[dict, list],
148+
key: str,
149+
value: Union[str, int, float, bool]) -> list:
154150
"""
155151
Search JSON data that consists of key:value pairs for all instances of
156152
provided key and value pair. The parent set that contains the key:value

tests/test_parser.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,54 +90,54 @@ def complex_json(self):
9090
}
9191
]
9292

93-
def test_key(self, parser, complex_json):
93+
def test_find_key(self, parser, complex_json):
9494

95-
result = parser.key(complex_json, "id")
95+
result = parser.find_key(complex_json, "id")
9696
assert result == [
9797
'0003', '5004', '5003', '5002', '5001', '1002',
9898
'1001', '0002', '5005', '5004', '5003', '5002', '5001', '1001',
9999
'0001', '5007', '5006', '5005', '5004', '5003', '5002', '5001',
100100
'1004', '1003', '1002', '1001'
101101
]
102102

103-
def test_key_not_found(self, parser, complex_json):
103+
def test_find_key_not_found(self, parser, complex_json):
104104

105-
result = parser.key(complex_json, "key_not_in_data")
105+
result = parser.find_key(complex_json, "key_not_in_data")
106106
assert result == []
107107

108-
def test_key_empty_key(self, parser, complex_json):
108+
def test_find_key_empty_key(self, parser, complex_json):
109109

110110
try:
111-
parser.key(
111+
parser.find_key(
112112
complex_json,
113113
""
114114
)
115115
except ValueError:
116116
assert True
117117

118-
def test_key_not_str_key(self, parser, complex_json):
118+
def test_find_key_not_str_key(self, parser, complex_json):
119119

120120
try:
121-
parser.key(
121+
parser.find_key(
122122
complex_json,
123123
5
124124
)
125125
except TypeError:
126126
assert True
127127

128-
def test_key_not_list_or_dict_data(self, parser):
128+
def test_find_key_not_list_or_dict_data(self, parser):
129129

130130
try:
131-
parser.key(
131+
parser.find_key(
132132
"string of data",
133133
"string"
134134
)
135135
except TypeError:
136136
assert True
137137

138-
def test_key_chain(self, parser, complex_json):
138+
def test_find_key_chain(self, parser, complex_json):
139139

140-
result = parser.key_chain(
140+
result = parser.find_key_chain(
141141
complex_json,
142142
[
143143
"batters",
@@ -149,30 +149,30 @@ def test_key_chain(self, parser, complex_json):
149149
'Regular', 'Chocolate', 'Blueberry', "Devil's Food",
150150
'Regular', 'Regular', 'Chocolate']
151151

152-
def test_key_chain_empty_key(self, parser, complex_json):
152+
def test_find_key_chain_empty_key(self, parser, complex_json):
153153

154154
try:
155-
parser.key_chain(
155+
parser.find_key_chain(
156156
complex_json,
157157
[""]
158158
)
159159
except ValueError:
160160
assert True
161161

162-
def test_key_chain_not_str_key(self, parser, complex_json):
162+
def test_find_key_chain_not_str_key(self, parser, complex_json):
163163

164164
try:
165-
parser.key_chain(
165+
parser.find_key_chain(
166166
complex_json,
167167
[5]
168168
)
169169
except TypeError:
170170
assert True
171171

172-
def test_key_chain_not_list_or_dict_data(self, parser):
172+
def test_find_key_chain_not_list_or_dict_data(self, parser):
173173

174174
try:
175-
parser.key_chain(
175+
parser.find_key_chain(
176176
"string of data",
177177
[
178178
"string",
@@ -183,17 +183,17 @@ def test_key_chain_not_list_or_dict_data(self, parser):
183183
except TypeError:
184184
assert True
185185

186-
def test_key_chain_key_not_found(self, parser, complex_json):
186+
def test_find_key_chain_key_not_found(self, parser, complex_json):
187187

188-
result = parser.key_chain(
188+
result = parser.find_key_chain(
189189
complex_json,
190190
["key_not_in_data"]
191191
)
192192
assert result == []
193193

194-
def test_key_chain_wildcard(self, parser, complex_json):
194+
def test_find_key_chain_wildcard(self, parser, complex_json):
195195

196-
result = parser.key_chain(
196+
result = parser.find_key_chain(
197197
complex_json,
198198
[
199199
"*",
@@ -204,9 +204,9 @@ def test_key_chain_wildcard(self, parser, complex_json):
204204
"5007", "5001", "5002", "5003", "5004", "5005",
205205
"5001", "5002", "5003", "5004"]
206206

207-
def test_key_value(self, parser, complex_json):
207+
def test_find_key_value(self, parser, complex_json):
208208

209-
result = parser.key_value(
209+
result = parser.find_key_value(
210210
complex_json,
211211
"id",
212212
"1001"
@@ -216,9 +216,9 @@ def test_key_value(self, parser, complex_json):
216216
{'id': '1001', 'type': 'Regular'},
217217
{'id': '1001', 'type': 'Regular'}]
218218

219-
def test_key_value_not_found(self, parser, complex_json):
219+
def test_find_key_value_not_found(self, parser, complex_json):
220220

221-
result = parser.key_value(
221+
result = parser.find_key_value(
222222
complex_json,
223223
"id",
224224
5.4

0 commit comments

Comments
 (0)