Skip to content

Commit

Permalink
MNT: rename FilterConditional to SearchTerm
Browse files Browse the repository at this point in the history
  • Loading branch information
shilorigins committed Sep 10, 2024
1 parent 65c99ad commit caa5162
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 52 deletions.
6 changes: 3 additions & 3 deletions superscore/backends/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from superscore.model import Entry, Root

FilterConditional = namedtuple('FilterConditional', ('attr', 'operator', 'value'))
SearchTerm = namedtuple('SearchTerm', ('attr', 'operator', 'value'))


class _Backend:
Expand Down Expand Up @@ -44,8 +44,8 @@ def update_entry(self, entry: Entry) -> None:
"""
raise NotImplementedError

def search(self, *conditionals) -> Generator[Entry, None, None]:
"""Yield Entry objects matching all ``conditionals``"""
def search(self, *search_terms) -> Generator[Entry, None, None]:
"""Yield Entry objects matching all ``search_terms``"""
raise NotImplementedError

@property
Expand Down
6 changes: 3 additions & 3 deletions superscore/backends/filestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def delete_entry(self, entry: Entry) -> None:
with self._load_and_store_context() as db:
db.pop(entry.uuid, None)

def search(self, *conditionals) -> Generator[Entry, None, None]:
def search(self, *search_terms) -> Generator[Entry, None, None]:
"""
Search for an entry that matches ``search_kwargs``.
Keys are attributes on `Entry` subclasses
Expand All @@ -294,9 +294,9 @@ def search(self, *conditionals) -> Generator[Entry, None, None]:
"""
with self._load_and_store_context() as db:
for entry in db.values():
# TODO: search for child pvs?
conditions = []
for attr, op, value in conditionals:
# TODO: search for child pvs?
for attr, op, value in search_terms:
comparator = self.getComparator(op, type(value))
try:
# check entry attribute by name
Expand Down
20 changes: 10 additions & 10 deletions superscore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from uuid import UUID

from superscore.backends import get_backend
from superscore.backends.core import FilterConditional, _Backend
from superscore.backends.core import SearchTerm, _Backend
from superscore.control_layers import ControlLayer, EpicsData
from superscore.control_layers.status import TaskStatus
from superscore.errors import CommunicationError
Expand Down Expand Up @@ -148,19 +148,19 @@ def find_config() -> Path:
raise OSError("No superscore configuration file found. Check SUPERSCORE_CFG.")

def search(self, *post) -> Generator[Entry, None, None]:
"""Search by FilterConditional. Can search by any field, plus special flags"""
new_conditionals = []
for conditional in post:
if conditional.operator == 'like_with_tols':
target, rel_tol, abs_tol = conditional.value
"""Search by SearchTerm. Can search by any field, plus special flags"""
new_search_terms = []
for search_term in post:
if search_term.operator == 'like_with_tols':
target, rel_tol, abs_tol = search_term.value
lower = target - target * rel_tol - abs_tol
upper = target + target * rel_tol + abs_tol
new_conditionals.append(
FilterConditional(conditional.attr, 'in_range', (lower, upper))
new_search_terms.append(
SearchTerm(search_term.attr, 'in_range', (lower, upper))
)
else:
new_conditionals.append(conditional)
return self.backend.search(*new_conditionals)
new_search_terms.append(search_term)
return self.backend.search(*new_search_terms)

def save(self, entry: Entry):
"""Save information in ``entry`` to database"""
Expand Down
38 changes: 19 additions & 19 deletions superscore/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from superscore.backends.core import FilterConditional, _Backend
from superscore.backends.core import SearchTerm, _Backend
from superscore.errors import (BackendError, EntryExistsError,
EntryNotFoundError)
from superscore.model import Collection, Parameter, Snapshot
Expand Down Expand Up @@ -74,74 +74,74 @@ def test_delete_entry(backends: _Backend):
def test_search_entry(backends: _Backend):
# Given an entry we know is in the backend
results = backends.search(
FilterConditional('description', 'eq', 'collection 1 defining some motor fields')
SearchTerm('description', 'eq', 'collection 1 defining some motor fields')
)
assert len(list(results)) == 1
# Search by field name
results = backends.search(
FilterConditional('uuid', 'eq', UUID('ffd668d3-57d9-404e-8366-0778af7aee61'))
SearchTerm('uuid', 'eq', UUID('ffd668d3-57d9-404e-8366-0778af7aee61'))
)
assert len(list(results)) == 1
# Search by field name
results = backends.search(
FilterConditional('data', 'eq', 2)
SearchTerm('data', 'eq', 2)
)
assert len(list(results)) == 3
# Search by field name
results = backends.search(
FilterConditional('uuid', 'eq', UUID('ecb42cdb-b703-4562-86e1-45bd67a2ab1a')),
FilterConditional('data', 'eq', 2)
SearchTerm('uuid', 'eq', UUID('ecb42cdb-b703-4562-86e1-45bd67a2ab1a')),
SearchTerm('data', 'eq', 2)
)
assert len(list(results)) == 1

results = backends.search(
FilterConditional('entry_type', 'eq', Snapshot)
SearchTerm('entry_type', 'eq', Snapshot)
)
assert len(list(results)) == 1

results = backends.search(
FilterConditional('entry_type', 'in', (Snapshot, Collection))
SearchTerm('entry_type', 'in', (Snapshot, Collection))
)
assert len(list(results)) == 2

results = backends.search(
FilterConditional('data', 'lt', 3)
SearchTerm('data', 'lt', 3)
)
assert len(list(results)) == 3

results = backends.search(
FilterConditional('data', 'gt', 3)
SearchTerm('data', 'gt', 3)
)
assert len(list(results)) == 1


@pytest.mark.parametrize('backends', [0], indirect=True)
def test_fuzzy_search(backends: _Backend):
results = list(backends.search(
FilterConditional('description', 'like', 'motor'))
SearchTerm('description', 'like', 'motor'))
)
assert len(results) == 4

results = list(backends.search(
FilterConditional('data', 'in_range', (6, 6))
SearchTerm('data', 'in_range', (6, 6))
))
assert len(results) == 1

results = list(backends.search(
FilterConditional('data', 'in_range', (4, 4))
SearchTerm('data', 'in_range', (4, 4))
))
assert len(results) == 0

results = list(backends.search(
FilterConditional('data', 'in_range', (2, 6))
SearchTerm('data', 'in_range', (2, 6))
))
assert len(results) == 4


@pytest.mark.parametrize('backends', [0], indirect=True)
def test_tag_search(backends: _Backend):
results = list(backends.search(
FilterConditional('tags', 'gt', set())
SearchTerm('tags', 'gt', set())
))
assert len(results) == 2 # only the Collection and Snapshot have .tags

Expand All @@ -155,12 +155,12 @@ class Tag(Flag):
backends.update_entry(results[1])

results = list(backends.search(
FilterConditional('tags', 'gt', {Tag.T1})
SearchTerm('tags', 'gt', {Tag.T1})
))
assert len(results) == 2

results = list(backends.search(
FilterConditional('tags', 'gt', {Tag.T1, Tag.T2})
SearchTerm('tags', 'gt', {Tag.T1, Tag.T2})
))
assert len(results) == 1

Expand All @@ -169,14 +169,14 @@ class Tag(Flag):
def test_update_entry(backends: _Backend):
# grab an entry from the database and modify it.
entry = list(backends.search(
FilterConditional('description', 'eq', 'collection 1 defining some motor fields')
SearchTerm('description', 'eq', 'collection 1 defining some motor fields')
))[0]
old_uuid = entry.uuid

entry.description = 'new_description'
backends.update_entry(entry)
new_entry = list(backends.search(
FilterConditional('description', 'eq', 'new_description')
SearchTerm('description', 'eq', 'new_description')
))[0]
new_uuid = new_entry.uuid

Expand Down
6 changes: 3 additions & 3 deletions superscore/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from superscore.backends.core import FilterConditional
from superscore.backends.core import SearchTerm
from superscore.backends.filestore import FilestoreBackend
from superscore.client import Client
from superscore.control_layers import EpicsData
Expand Down Expand Up @@ -141,11 +141,11 @@ def test_find_config(sscore_cfg: str):

def test_search(sample_client):
results = list(sample_client.search(
FilterConditional('data', 'like_with_tols', (4, 0, 0))
SearchTerm('data', 'like_with_tols', (4, 0, 0))
))
assert len(results) == 0

results = list(sample_client.search(
FilterConditional('data', 'like_with_tols', (4, .5, 1))
SearchTerm('data', 'like_with_tols', (4, .5, 1))
))
assert len(results) == 4
28 changes: 16 additions & 12 deletions superscore/widgets/page/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dateutil import tz
from qtpy import QtCore, QtWidgets

from superscore.backends.core import FilterConditional
from superscore.backends.core import SearchTerm
from superscore.client import Client
from superscore.model import Collection, Entry, Readback, Setpoint, Snapshot
from superscore.widgets import ICON_MAP
Expand Down Expand Up @@ -96,7 +96,7 @@ def setup_ui(self) -> None:
self.name_subfilter_line_edit.textChanged.connect(self.subfilter_results)

def _gather_search_terms(self) -> Dict[str, Any]:
conditionals = []
search_terms = []

# type
entry_type_list = [Snapshot, Collection, Setpoint, Readback]
Expand All @@ -107,42 +107,46 @@ def _gather_search_terms(self) -> Dict[str, Any]:
if not checkbox.isChecked():
entry_type_list.remove(entry_type)

conditionals.append(FilterConditional('entry_type', 'eq', tuple(entry_type_list)))
search_terms.append(SearchTerm('entry_type', 'eq', tuple(entry_type_list)))

# name
name = self.name_line_edit.text()
if name:
conditionals.append(FilterConditional('title', 'in', tuple(n.strip() for n in name.split(','))))
search_terms.append(
SearchTerm('title', 'in', tuple(n.strip() for n in name.split(',')))
)

# description
desc = self.desc_line_edit.text()
if desc:
conditionals.append(FilterConditional('description', 'like', desc))
search_terms.append(SearchTerm('description', 'like', desc))

# TODO: sort out PVs
pvs = self.pv_line_edit.text()
if pvs:
conditionals.append(FilterConditional('pvs', 'in', tuple(pv.strip() for pv in pvs.split(','))))
search_terms.append(
SearchTerm('pvs', 'in', tuple(pv.strip() for pv in pvs.split(',')))
)

# time
start_dt = self.start_dt_edit.dateTime().toPyDateTime()
start_utc = start_dt.astimezone(tz.UTC)
end_dt = self.end_dt_edit.dateTime().toPyDateTime()
end_utc = end_dt.astimezone(tz.UTC)
conditionals.append(
FilterConditional('creation_time', 'in_range', (start_utc, end_utc))
search_terms.append(
SearchTerm('creation_time', 'in_range', (start_utc, end_utc))
)

logger.debug(f'gathered search terms: {conditionals}')
return conditionals
logger.debug(f'gathered search terms: {search_terms}')
return search_terms

def show_current_filter(self) -> None:
"""
Gather filter options and update source model with valid entries
"""
# gather filter details
conditionals = self._gather_search_terms()
entries = self.client.search(*conditionals)
search_terms = self._gather_search_terms()
entries = self.client.search(*search_terms)

# update source table model
self.model.modelAboutToBeReset.emit()
Expand Down
4 changes: 2 additions & 2 deletions superscore/widgets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import qtawesome as qta
from qtpy import QtCore, QtGui, QtWidgets

from superscore.backends.core import FilterConditional
from superscore.backends.core import SearchTerm
from superscore.client import Client
from superscore.control_layers import EpicsData
from superscore.model import (Collection, Entry, Nestable, Parameter, Readback,
Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(
def fill_uuids(self, client: Optional[Client] = None) -> None:
"""Fill this item's data if it is a uuid, using ``client``"""
if isinstance(self._data, UUID) and client is not None:
self._data = list(client.search(FilterConditional('uuid', 'eq', self._data)))[0]
self._data = list(client.search(SearchTerm('uuid', 'eq', self._data)))[0]

def data(self, column: int) -> Any:
"""
Expand Down

0 comments on commit caa5162

Please sign in to comment.