22Author : zhangxianbing1
33Date : 2020-12-27 09:22:14
44LastEditors : zhangxianbing1
5- LastEditTime : 2020-12-30 17:28:02
5+ LastEditTime : 2020-12-30 17:39:37
66Description : JSONPath
77"""
88__version__ = "1.0.0"
3434REP_FILTER = re .compile (r"\[(\??\(.*?\))\]" )
3535REP_SORT = re .compile (r"\[[\\|/](.*?),\]" )
3636
37- LOG = logging .getLogger (__name__ )
38- LOG .setLevel (logging .DEBUG )
3937
4038# pylint: disable=invalid-name,missing-function-docstring,missing-class-docstring
4139
@@ -56,10 +54,8 @@ class JSONPath:
5654 subx = defaultdict (list )
5755 ops : list
5856 oplen : int
59- debug : int
60-
61- def __init__ (self , expr : str , * , result_type = "VALUE" , debug = 0 ):
6257
58+ def __init__ (self , expr : str , * , result_type = "VALUE" ):
6359 if result_type not in RESULT_TYPE :
6460 raise ValueError (f"result_type must be one of { tuple (RESULT_TYPE .keys ())} " )
6561 self .result_type = result_type
@@ -70,10 +66,9 @@ def __init__(self, expr: str, *, result_type="VALUE", debug=0):
7066 self .ops = expr .split (SEP )
7167 self .oplen = len (self .ops )
7268
73- self .debug = debug
74-
7569 def _parse_expr (self , expr ):
76- LOG .debug ("before expr: %s" , expr )
70+ if __debug__ :
71+ print (f"before expr: { expr } " )
7772
7873 expr = REP_PICKUP_QUOTE .sub (self ._f_pickup_quote , expr )
7974 expr = REP_PICKUP_BRACKET .sub (self ._f_pickup_bracket , expr )
@@ -84,7 +79,8 @@ def _parse_expr(self, expr):
8479 if expr .startswith ("$;" ):
8580 expr = expr [2 :]
8681
87- LOG .debug ("before expr: %s" , expr )
82+ if __debug__ :
83+ print (f"after expr: { expr } " )
8884 return expr
8985
9086 def _f_pickup_quote (self , m ):
@@ -178,4 +174,4 @@ def _trace(self, obj, idx, op=None):
178174 with open ("test/data/2.json" , "rb" ) as f :
179175 d = json .load (f )
180176 # JSONPath("$.book[1].title").parse(d)
181- JSONPath ("$.* " ).parse (d )
177+ JSONPath ("$..price " ).parse (d )
0 commit comments