Skip to content

Commit

Permalink
Remove temporary True/False handling from _execute_query()
Browse files Browse the repository at this point in the history
method. The new where-clause implementation now implements
full predicate handling.

Update CHANGELOG with new where-clause predicate handling.
  • Loading branch information
shawnbrown committed Jul 8, 2018
1 parent 993f55a commit 418ba3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ COMING SOON (0.9.2)
-------------------

* Improved data handling features and Python 3.7 support.
* Changed Selector class keyword filtering to support predicate matching.
* Changed Query class:
* Added flatten() method to serialize dictionary results.
* Added to_csv() method to quickly save results as a CSV file.
Expand Down
14 changes: 0 additions & 14 deletions datatest/_query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,13 +1028,6 @@ def to_csv(self, file, fieldnames=None, **fmtparams):
])


# Temporary functions for "where" keyword args.
# NOTE!!!: Remove these after implementing full
# predicate support for keyword filtering.
_is_truthy = lambda x: bool(x)
_is_falsy = lambda x: not bool(x)


class Selector(object):
"""A class to quickly load and select tabular data. The given
*objs*, *\*args*, and *\*\*kwds*, can be any values supported
Expand Down Expand Up @@ -1207,13 +1200,6 @@ def __call__(self, columns, **where):
def _execute_query(self, select_clause, trailing_clause=None, **kwds_filter):
"""Execute query and return cursor object."""
try:
for key, val in kwds_filter.items(): # Temporary partial keyword
if val is True: # handling to use until full
val = _is_truthy # predicate support is ready.
elif val is False:
val = _is_falsy
kwds_filter[key] = val

# Build select-query.
stmnt = 'SELECT {0} FROM {1}'.format(select_clause, self._table)
where_clause, params = self._build_where_clause(kwds_filter)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,15 @@ def test_build_where_clause(self):
self.assertEqual(result[1], [])
self.assertEqual(len(select._user_function_dict), prev_len + 1)

# Predicate (a boolean)
prev_len = len(select._user_function_dict)
predicate = True
result = select._build_where_clause({'A': predicate})
self.assertEqual(len(result), 2)
self.assertRegex(result[0], r'FUNC\d+\(A\)')
self.assertEqual(result[1], [])
self.assertEqual(len(select._user_function_dict), prev_len + 1)

def test_execute_query(self):
data = [['A', 'B'], ['x', 101], ['y', 202], ['z', 303]]
source = Selector(data)
Expand Down

0 comments on commit 418ba3d

Please sign in to comment.