> pip install qdict
Executes a query on an Iterable (List[dict], dict, List[Tuple[key, value]]) and applies the filter.
The filter is applied horizontally following the iteration of the query keys, that is, when the query has a key and this key is not an operator and its value is a dictionary, this dictionary is isolated from within the query and the object to be checked .
Attention: If the object does not have the key specified in the query, the object will not be returned in the final results.
All operators respect the level of objects.
$or : list of queries.
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 : negative expression
{ "a" : { "$not" : 1 }}
$custom : Defines a method dynamically. (func, keyname1, keyname2, ..., keynameN). Each keyname will be a parameter with the key value or 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 : checks if the object contains the key
$contains : If the list (object) contains the item (query)
$in : If the value (object) is contained in the list (query)
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}]