Skip to content

Commit

Permalink
implemented TextSelection.test_data() and TextSelections.test_data()
Browse files Browse the repository at this point in the history
Using these new methods is more efficient than doing .annotations().test_data()
  • Loading branch information
proycon committed Oct 16, 2023
1 parent 6ec338a commit abd1011
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/textselection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ impl PyTextSelection {
})
}

#[pyo3(signature = (**kwargs))]
fn test_data(&self, kwargs: Option<&PyDict>) -> PyResult<bool> {
let iterparams = IterParams::new(kwargs)?;
self.map(|textselection| {
let iter = textselection.annotations().data_unchecked();
Ok(iterparams
.evaluate_data(iter, textselection.rootstore())?
.test())
})
}

#[pyo3(signature = (operator, **kwargs))]
fn related_text(
&self,
Expand Down Expand Up @@ -538,6 +549,20 @@ impl PyTextSelections {
})
}

#[pyo3(signature = (**kwargs))]
fn test_data(&self, kwargs: Option<&PyDict>) -> PyResult<bool> {
let iterparams = IterParams::new(kwargs)?;
self.map(|textselections, store| {
let iter = stam::TextSelectionsIter::from_handles(
textselections.iter().copied().collect(), //MAYBE TODO: work away the extra copy
store,
)
.annotations_unchecked()
.data_unchecked();
Ok(iterparams.evaluate_data(iter, store)?.test())
})
}

#[pyo3(signature = (operator, **kwargs))]
fn related_text(
&self,
Expand Down
16 changes: 16 additions & 0 deletions stam.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,14 @@ class TextSelections:
The annotations can be filtered using keyword arguments. See :meth:`Annotation.annotations`.
"""

def test_data(self, **kwargs) -> bool:
"""
Tests whether there are any annotations that reference any of the text selections in the iterator, with data that passes the provided filters.
The result is functionally equivalent to doing `.annotations().test_data()`, but this shortcut method is implemented much more efficiently and therefore recommended.
The data can be filtered using keyword arguments. See :meth:`Annotations.data`.
"""

def related_text(self, operator: TextSelectionOperator, **kwargs) -> TextSelections:
"""
Applies a :class:`TextSelectionOperator` to find all other
Expand Down Expand Up @@ -1321,6 +1329,14 @@ class TextSelection:
The annotations can be filtered using keyword arguments. See :meth:`Annotation.annotations`.
"""

def test_data(self, **kwargs) -> bool:
"""
Tests whether there are any annotations that reference this text selection with data that passes the provided filters.
The result is functionally equivalent to doing `.annotations().test_data()`, but this shortcut method is implemented much more efficiently and therefore recommended.
The data can be filtered using keyword arguments. See :meth:`Annotations.data`.
"""

def related_text(self, operator: TextSelectionOperator, **kwargs) -> TextSelections:
"""
Applies a :class:`TextSelectionOperator` to find all other
Expand Down

0 comments on commit abd1011

Please sign in to comment.