-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_query.py
41 lines (34 loc) · 1.32 KB
/
database_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from utilities import make_args_hashable
from functools import lru_cache
import json
class DatabaseQuery:
@make_args_hashable
@lru_cache()
def filter_by_fields(fields, db):
return {key: dict(db[key]) for key in db if DatabaseQuery._dict_contain_fields(fields, db[key])}
@make_args_hashable
@lru_cache()
def filter_by_queries(queries, db):
for query in queries:
db = DatabaseQuery._filter_by_query(json.loads(query), db)
return db
@make_args_hashable
@lru_cache()
def filter_by_names(names, db):
return {key: dict(db[key]) for key in db if key in names}
@make_args_hashable
@lru_cache()
def _dict_contain_fields(fields, db):
return all([field in db for field in fields])
@make_args_hashable
@lru_cache()
def _dict_in_dict(inner_db, outer_db):
return all([(key, value) in outer_db.items() for (key, value) in inner_db.items()])
@make_args_hashable
@lru_cache()
def _filter_by_query(query, db):
return {key: dict(db[key]) for key in db if DatabaseQuery._dict_in_dict(query, db[key])}
@make_args_hashable
@lru_cache()
def _filter_by_query_not(query, db):
return {key: dict(db[key]) for key in db if not DatabaseQuery._dict_in_dict(query, db[key])}