Package: pyspecter
pyspecterA Python library to query nested structure, inspired byspecterIf you are dealing with nested python structure and it require complex rule to search the data underneath like:Key start with a patternValue to be filterConditional path to walk into...this is the right library fit the use case as it extract theNavigationrule to a list, saving your a lot of trouble writting your own logic to navigate the nested data.Get startedExamplesm = {"A":{"B1":[10,20],"B2":2,"B3":3}
     ,"C":{"B1":[1,2],"B2":[3,4]}
     ,"D":[1,2,3,4]
     ,"E":[None,2,3,4]
     ,"F":{"G":1}
     ,"H":["A1","A2","A3"]}Navigate to specific itemFIRST/LASTassert query(m, ["D", S.FIRST]) == 1
assert query(m, ["A", S.FIRST]) == ("B1", [10, 20]) # first element from dict.items()

assert query(m, ["D", S.LAST]) == 4
assert query(m, ["A", S.LAST]) == ("B3", 3) # last element from dict.items()NthNavigate to the Nth elementassert query(m, ["D", (S.NTH, 1)]) == 2
assert query(m, ["D",(S.NTH, 1, 2)]) == [ 2, 3 ]Operation on dictNavigate tovaluesorkeysof current positionassert query(m, ["C", S.MVALS]) == [[1, 2], [3, 4]]
assert query(m, ["C", S.MKEYS]) == ['B1', 'B2']Navigate to a sub map of current positionassert query(m, ["A", (S.SUB_MAP, "B1")]) == {"B1":[10,20]}Annotate with index with current positionassert query(m, ["A", S.INDEXED_VALS]) == [(0, ("B1", [10,20])), (1, ("B2", 2)), (2, ("B3", 3))]
assert query(m, ["C", "B1", S.INDEXED_VALS]) == [(0, 1), (1, 2)]Filteringfilter elements by supplying a functionassert query(m, ["A", "B1", (S.FILTER, lambda x: x > 10)]) == [20]
assert query(m, ["C", (S.FILTER, lambda k, v: k.endswith("2"))]) == [[3, 4]]Navigate to map which its key or value satisify a custom functionassert query(m, ["A", (S.MKEY_IF, lambda x:x.endswith("1"))]) == [[10, 20]]
assert query(m, ["A", (S.MVAL_IF, lambda x:x==[10,20])]) == [[10, 20]]Branching pathsBranching by multiple pathsmpath = {"A":{"B":1,
              "C":[2,3,4,5]}}
assert query(mpath,["A",
                    (S.MULTI_PATH,["B"]
                                 ,["C", S.LAST])]) == [1,5]Conditional NavigationNavigate to a specifie pathassert query(m, ["D", (S.NTH_PATH, 2)]) == [3]Navigate to a position if and only if the path existsassert query(m, [(S.MUST,"F","G")]) == 1
assert query(m, [(S.MUST,"F","G","NOT_EXISTS")]) == NoneNavigate to a rangeassert query(m, ["D",(S.SRANGE,2,3)]) == [3]Navigate to a2nd pathif1st pathexists, else returnNoneassert query(m,[(S.IF_PATH,["C","B1"],["E"])]) == [None,2,3,4]
assert query(m,[(S.IF_PATH,["C","B1"],["NOT_EXISTS"])]) == NoneNavigate to a2nd pathif1st pathexists, else navigate to3rd pathassert query(m,[(S.IF_PATH,["C","B3"],["E"],["F"])]) == {'G': 1}Navigate to values of dict if key satisfy a regex expression:assert query(m, ["A",(S.REGEX,r"B[23]")]) == [ 2,3 ]Navigate to values of list if elements satisfy a regex expression:assert query(m, ["H",(S.REGEX,r"\S1")]) == [ "A1" ]Navigate with optional path nodeassert query(m, [(S.MAYBE,"F","G")]) == 1Handling None valueReturn default value if current position isNoneassert query(None,[(S.NONE_VAL,10)]) == 10If current position is not a None,then return the value of current positionassert query(5,[(S.NONE_VAL,10)]) == 5Operation on resultsuser can operate on the query result byREUDCE -> performreduceon thelistordictresult ,from left to right.m2 = {"A":{"B":["C1","C2","C3"]}}
assert query(m2, ["A","B",(H.REDUCE,lambda acc,x:acc+"|"+x, ">>>")]) == ">>>|C1|C2|C3"
assert query(m2, ["A","B",(H.REDUCE,lambda acc,x:acc+"|"+x)]) == "C1|C2|C3"

m2 = {"A":{"B":1,"D":2}}
assert query(m2, ["A",(H.REDUCE,lambda acc,x:acc+"|"+str(x[1]),">>")]) == ">>|1|2"
assert query(m2, ["A",(H.REDUCE,lambda acc,x:acc+"|"+str(x[0]),">>")]) == ">>|B|D"MAP -> performn atransformationon thelistordictresultm2 = {"A":{"B":["C1","C2","C3"]}}
assert query(m2, ["A","B",(H.MAP,lambda x:x+"!")]) == ["C1!","C2!","C3!"]

m2 = {"A":{"B":"C1","D":"C2"}}
assert query(m2, ["A",(H.MAP,(lambda k,v: v+"!"))]) == ["C1!","C2!"]SUM ->sum()the result list, with optional custom function passed before summing.m2 = {"A":{"B":[1,2]}}
assert query(m2, ["A","B",H.SUM]) == 3

m2 = {"A":{"B":"1","D":"2"}}
assert query(m2, ["A",S.MVALS,(H.SUM,lambda x: int(x))]) == 3MAX/MIN -> Using built-inmax()/min()on thelistresult, with optioanl custom function passed before max/minm2 = {"A":{"B":[3,5,2]}}
assert query(m2, ["A","B",H.MIN]) == 2
assert query(m2, ["A","B",(H.MIN,str)]) == "2"
assert query(m2, ["A","B",H.MAX]) == 5
assert query(m2, ["A","B",(H.MAX, str)]) == "5"ORDER -> usingsorted()to sort the result list, with optional custom function passed before sorting.m2 = {"A":{"B":[3,5,2]}}
m2 = {"A":{"B":[3,5,2]}}
assert query(m2, ["A","B",H.ORDER]) == [2,3,5]
assert query(m2, ["A","B",(H.ORDER, str)]) == ["2","3","5"]