qdict
1.0.0
> pip install qdict
对 Iterable (List[dict], dict, List[Tuple[key, value]]) 执行查询并应用过滤器。
过滤器是在查询键迭代之后水平应用的,即当查询有一个键并且该键不是运算符且其值是字典时,该字典与查询和要检查的对象内部隔离。
注意:如果该对象没有查询中指定的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}]