Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable dunder operators for filters (column__<op>=<value>) #396

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dataset/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,11 @@ def _args_to_clause(self, args, clauses=()):
clauses = list(clauses)
for column, value in args.items():
column = self._get_column_name(column)
if not self.has_column(column):
if '__' in column:
# enable Django-style column__<op>=value syntax
column, op = column.split('__', 1)
clauses.append(self._generate_clause(column, op, value))
elif not self.has_column(column):
clauses.append(false())
elif isinstance(value, (list, tuple, set)):
clauses.append(self._generate_clause(column, "in", value))
Expand Down
10 changes: 8 additions & 2 deletions docs/queries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Advanced filters
================

``dataset`` provides two methods for running queries: :py:meth:`table.find() <dataset.Table.find>`
and :py:meth:`db.query() <dataset.Database.query>`. The table find helper method provides
and :py:meth:`db.query() <dataset.Database.query>`. The table find helper method provides
limited, but simple filtering options::

results = table.find(column={operator: value})
Expand All @@ -23,6 +23,12 @@ A special form is using keyword searches on specific columns::
# equal to:
results = table.find(value={'in': ('foo', 'bar')})

To AND-combine multiple operators on the same column, you may specify
the operator using the `column__operator=value` format::

# height >= 20 AND height <= 50
table.find({'height__gte': 20, 'height__lte: 50'}

The following comparison operators are supported:

============== ============================================================
Expand Down Expand Up @@ -69,4 +75,4 @@ Finally, you should consider falling back to SQLAlchemy_ core to construct
queries if you are looking for a programmatic, composable method of generating
SQL in Python.

.. _SQLALchemy: https://docs.sqlalchemy.org/
.. _SQLALchemy: https://docs.sqlalchemy.org/
20 changes: 20 additions & 0 deletions test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ def test_find_dsl(self):
ds = list(self.tbl.find(place={"ilike": "%LwAy"}))
assert len(ds) == 3, ds

def test_find_dsl_dunder_op(self):
ds = list(self.tbl.find(place__like="%lw%"))
assert len(ds) == 3, ds
ds = list(self.tbl.find(temperature__gt=5))
assert len(ds) == 2, ds
ds = list(self.tbl.find(temperature__gte=5))
assert len(ds) == 3, ds
ds = list(self.tbl.find(temperature__lt=0))
assert len(ds) == 1, ds
ds = list(self.tbl.find(temperature__lte=0))
assert len(ds) == 2, ds
ds = list(self.tbl.find(temperature__not=-1))
assert len(ds) == 5, ds
ds = list(self.tbl.find(temperature__between=[5, 8]))
assert len(ds) == 3, ds
ds = list(self.tbl.find(place="G€lway"))
assert len(ds) == 3, ds
ds = list(self.tbl.find(place__ilike="%LwAy"))
assert len(ds) == 3, ds

def test_offset(self):
ds = list(self.tbl.find(place=TEST_CITY_1, _offset=1))
assert len(ds) == 2, ds
Expand Down