qdict
1.0.0
> pip install qdict
對 Iterable (List[dict], dict, List[Tuple[key, 值]]) 執行查詢並套用篩選器。
過濾器是在查詢鍵迭代之後水平應用的,即當查詢有一個鍵並且該鍵不是運算符且其值是字典時,該字典與查詢和要檢查的物件內部隔離。
注意:如果該物件沒有查詢中指定的key,則最終結果中不會傳回該物件。
所有操作員都尊重物件的等級。
$or :查詢清單。
from qdict import find
obj = [{ "a" : 1 , "b" : { "c" : "Positive" }}, { "a" : 1 , "b" : { "c" : "Negative" }},
{ "a" : 1 , "b" : { "c" : "Undefined" }}, { "a" : 1 }]
result = find ( obj , {
"$or" : [{ "b" : { "c" : "Positive" }},
{ "$not" : { "$has" : "b" }}]
})
r = list ( result )
print ( r ) # [{'a': 1, 'b': {'c': 'Positive'}}, {'a': 1}]
$not : 否定表達式
{ "a" : { "$not" : 1 }}
$custom :動態定義方法。 (func, 鍵名1, 鍵名2, ..., 鍵名N)。每個鍵名將是一個帶有鍵值或 None 的參數
from qdict import find
obj = [
{ "a" : 1 , "b" : True , "c" : { "a" : 1 }, "d" : { "a" : 1 }},
{ "a" : 3 , "b" : False , "c" : { "a" : 6 }}
]
def pair ( num ):
return num % 2 == 0
query = {
"c" : { "$custom" : ( pair , "a" )}
}
print ( list ( find ( obj , query ))) # [{"a": 3, "b": False, "c": {"a": 6}}]
$has :檢查物件是否包含密鑰
$contains :如果清單(物件)包含項目(查詢)
$in :如果值(物件)包含在清單(查詢)中
from qdict import find
# simple search
obj = [{ "a" : 1 , "b" : False }, { "a" : 2 , "b" : True }, { "b" : True }]
result = find ( obj , { "b" : True })
print ( list ( result )) # [{'a': 2, 'b': True}, {'a': 3, 'b': True}]
result = find ( obj , { "$has" : "a" })
print ( list ( result )) # [{'a': 1, 'b': False}, {'a': 2, 'b': True}]
result = find ( obj , { "$not" : { "$has" : "a" }})
print ( list ( result )) # [{'b': True}]
# search with subkeys
obj = [{ "a" : 1 , "b" : { "c" : "Positive" }}, { "a" : 1 , "b" : { "c" : "Negative" }},
{ "a" : 1 , "b" : {}}]
result = find ( obj , { "b" : { "c" : "Negative" }})
print ( list ( result )) # [{'a': 1, 'b': {'c': 'Negative'}}]
# $or
obj = [{ "a" : 1 , "b" : { "c" : "Positive" }}, { "a" : 1 , "b" : { "c" : "Negative" }},
{ "a" : 1 , "b" : { "c" : "Undefined" }}, { "a" : 1 }]
result = find ( obj , {
"$or" : [{ "b" : { "c" : "Positive" }},
{ "$not" : { "$has" : "b" }}]
})
print ( list ( result )) # [{'a': 1, 'b': {'c': 'Positive'}}, {'a': 1}]