Skip to content

Commit 4cb1120

Browse files
Optimize by way of reduce.
1 parent b4c141d commit 4cb1120

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

dj_gui_api_server/DJConnector.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
from .dj_connector_exceptions import InvalidDeleteRequest, InvalidRestriction, \
77
UnsupportedTableType
8+
from functools import reduce
89

910
DAY = 24 * 60 * 60
1011

@@ -124,32 +125,23 @@ def fetch_tuples(jwt_payload: dict, schema_name: str, table_name: str,
124125
:return: Records in dict form and the total number of records that can be paged
125126
:rtype: tuple
126127
"""
127-
def resolve_expression(all_restrictions: list,
128-
query: QueryExpression) -> QueryExpression:
129-
if not all_restrictions:
130-
return query & dict()
128+
def filter_to_restriction(filter_card: dict) -> str:
129+
if filter_card['operation'] in ('>', '<', '>=', '<='):
130+
operation = filter_card['operation']
131+
elif filter_card['value'] is None:
132+
operation = (' IS ' if filter_card['operation'] == '='
133+
else ' IS NOT ')
131134
else:
132-
current_restriction = all_restrictions[0]
133-
if current_restriction['operation'] in ('>', '<', '>=', '<='):
134-
operation = current_restriction['operation']
135-
elif current_restriction['value'] is None:
136-
operation = (' IS ' if current_restriction['operation'] == '='
137-
else ' IS NOT ')
138-
else:
139-
operation = current_restriction['operation']
135+
operation = filter_card['operation']
140136

141-
if (isinstance(current_restriction['value'], str) and
142-
not current_restriction['value'].isnumeric()):
143-
value = f"'{current_restriction['value']}'"
144-
else:
145-
value = ('NULL' if current_restriction['value'] is None
146-
else current_restriction['value'])
137+
if (isinstance(filter_card['value'], str) and
138+
not filter_card['value'].isnumeric()):
139+
value = f"'{filter_card['value']}'"
140+
else:
141+
value = ('NULL' if filter_card['value'] is None
142+
else filter_card['value'])
147143

148-
current_restriction = f"""{current_restriction[
149-
'attributeName']}{operation}{value}"""
150-
return (query & current_restriction if len(all_restrictions) == 1
151-
else resolve_expression(all_restrictions[1:],
152-
query & current_restriction))
144+
return f"{filter_card['attributeName']}{operation}{value}"
153145

154146
DJConnector.set_datajoint_config(jwt_payload)
155147

@@ -160,7 +152,8 @@ def resolve_expression(all_restrictions: list,
160152

161153
# Fetch tuples without blobs as dict to be used to create a
162154
# list of tuples for returning
163-
query = resolve_expression(restriction, table)
155+
query = reduce(lambda q1, q2: q1 & q2, [table()] + [filter_to_restriction(f)
156+
for f in restriction])
164157
non_blobs_rows = query.fetch(*table.heading.non_blobs, as_dict=True, limit=limit,
165158
offset=(page-1)*limit, order_by=order)
166159

0 commit comments

Comments
 (0)