let say I have dict { k1 : v1, k2 : v2 }, how do I build query from this. I can not do directly :
.search( (Query().k1 == v1) & (Query().k2 == v2))
because the dict may also be :
{k2:v2} OR {k1:v1, k3:v3} OR ........
The search() function on an instance of TinyDB actually takes a predicate as an argument, and instances of Query create these predicates behind the scenes. To run a more advanced search, you can supply TinyDB with your own predicate.
from tinydb import Query,TinyDB
def predicate(obj,requirements):
for k,v in requirements.items():
if k not in obj or obj[k]!=v:
return False
return True
tiny = TinyDB('db.json')
requirements={
'a':1,
'b':3
}
print(tiny.search(lambda obj: predicate(obj,requirements)))