Skip to content

Commit

Permalink
Implement Orange.widget.utils.signals.lazy_table_transform
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Aug 20, 2023
1 parent 2fc2717 commit 3f4df37
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Orange/widgets/utils/signals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import Optional

from orangewidget.utils.signals import (
Input, Output, Single, Multiple, Default, NonDefault, Explicit, Dynamic,
InputSignal, OutputSignal, WidgetSignalsMixin
InputSignal, OutputSignal, WidgetSignalsMixin, LazyValue
)

from Orange.data import Table, Domain

__all__ = [
"Input", "Output", "InputSignal", "OutputSignal",
"Single", "Multiple", "Default", "NonDefault", "Explicit", "Dynamic",
Expand All @@ -12,3 +16,11 @@

class AttributeList(list):
"""Signal type for lists of attributes (variables)"""


def lazy_table_transform(domain: Domain,
data: Optional[Table]) -> LazyValue[Table]:
if data is None:
return None

Check warning on line 24 in Orange/widgets/utils/signals.py

View check run for this annotation

Codecov / codecov/patch

Orange/widgets/utils/signals.py#L24

Added line #L24 was not covered by tests
return LazyValue[Table](lambda: data.transform(domain),
domain=domain, length=len(data))
27 changes: 27 additions & 0 deletions Orange/widgets/utils/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from unittest.mock import Mock

from Orange.widgets.utils.signals import lazy_table_transform


class TestSignals(unittest.TestCase):
def test_lazy_table_transform(self):
data = Mock()
data.__len__ = lambda _: 42
data.transform = Mock()

domain = Mock()

lazy_trans = lazy_table_transform(domain, data)

data.transform.assert_not_called()
self.assertEqual(lazy_trans.length, 42)
self.assertIs(lazy_trans.domain, domain)
self.assertFalse(lazy_trans.is_cached)

self.assertIs(lazy_trans.get_value(), data.transform.return_value)
data.transform.assert_called_once()


if __name__ == '__main__':
unittest.main()

0 comments on commit 3f4df37

Please sign in to comment.