Skip to content

Commit 8dcf854

Browse files
committed
feat: add search and complie interfaces
1 parent 88a43cf commit 8dcf854

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.vscode/
22
.idea/
3-
3+
tmp.py
44
### Python template
55
# Byte-compiled / optimized / DLL files
66
__pycache__/

jsonpath/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author : zhangxianbing
33
Date : 2020-12-27 09:22:14
44
LastEditors : zhangxianbing
5-
LastEditTime : 2021-06-04 09:07:14
5+
LastEditTime : 2021-06-04 10:10:10
66
Description : JSONPath
77
"""
88
__version__ = "1.0.5"
@@ -101,6 +101,9 @@ def parse(self, obj, result_type="VALUE"):
101101

102102
return self.result
103103

104+
def search(self, obj, result_type="VALUE"):
105+
return self.parse(obj, result_type)
106+
104107
def _parse_expr(self, expr):
105108
logger.debug(f"before expr : {expr}")
106109
# pick up special patterns
@@ -308,6 +311,21 @@ def _trace(self, obj, i: int, path):
308311
return
309312

310313

314+
def compile(expr):
315+
return JSONPath(expr)
316+
317+
318+
# global cache
319+
_jsonpath_cache = {}
320+
321+
322+
def search(expr, data):
323+
global _jsonpath_cache
324+
if expr not in _jsonpath_cache:
325+
_jsonpath_cache[expr] = JSONPath(expr)
326+
return _jsonpath_cache[expr].parse(data)
327+
328+
311329
if __name__ == "__main__":
312330
with open("test/data/2.json", "rb") as f:
313331
d = json.load(f)

0 commit comments

Comments
 (0)