Skip to content

Commit

Permalink
fixed operator
Browse files Browse the repository at this point in the history
  • Loading branch information
semiversus committed Jun 1, 2021
1 parent 993b340 commit bd9eb2d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 44 deletions.
18 changes: 8 additions & 10 deletions broqer/op/filter_.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from typing import Any, Callable

from broqer import NONE, Publisher
from broqer.operator import Operator, ClassOperatorMeta
from broqer.operator import Operator


class Filter(Operator):
Expand Down Expand Up @@ -77,16 +77,15 @@ def emit(self, value: Any, who: Publisher) -> None:
return None


class EvalTrue(Operator, metaclass=ClassOperatorMeta):
class EvalTrue(Operator):
""" Emits all values which evaluates for True.
This operator can be used in the pipline style (v | EvalTrue) or as
This operator can be used in the pipline style (v | EvalTrue()) or as
standalone operation (EvalTrue(v)).
"""
def __init__(self, publisher: Publisher = None) -> None:
Operator.__init__(self)
if publisher is not None:
self.originator = publisher
self._originator = publisher

def get(self) -> Any:
if self._subscriptions:
Expand All @@ -110,15 +109,14 @@ def emit(self, value: Any, who: Publisher) -> None:
return None


class EvalFalse(Operator, metaclass=ClassOperatorMeta):
class EvalFalse(Operator):
""" Filters all emits which evaluates for False.
This operator can be used in the pipline style (v | EvalFalse or as
This operator can be used in the pipline style (v | EvalFalse() or as
standalone operation (EvalFalse(v))."""
def __init__(self, publisher: Publisher) -> None:
def __init__(self, publisher: Publisher = None) -> None:
Operator.__init__(self)
if publisher is not None:
self.originator = publisher
self._originator = publisher

def get(self) -> Any:
if self._subscriptions:
Expand Down
13 changes: 0 additions & 13 deletions broqer/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ def originator(self, publisher: Publisher):
if self._subscriptions:
self._originator.subscribe(self)

def __ror__(self, publisher: Publisher) -> Publisher:
if self._originator is not None:
raise TypeError('operator already applied')
self.originator = publisher
return self

def subscribe(self, subscriber: 'Subscriber',
prepend: bool = False) -> SubscriptionDisposable:
disposable = Publisher.subscribe(self, subscriber, prepend)
Expand Down Expand Up @@ -71,13 +65,6 @@ def emit(self, value: typing.Any, who: Publisher) -> None:
"""


class ClassOperatorMeta(type):
""" Metaclass to be used, when class can directly be used as operator
e.g. EvalTrue """
def __ror__(cls, publisher: Publisher):
return cls(publisher)


class MultiOperator(Publisher, Subscriber):
""" Base class for operators depending on multiple publishers. Like
Operator all publishers will be subscribed on first subscription to this
Expand Down
9 changes: 0 additions & 9 deletions broqer/operator_overloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ def emit(self, value: Any_, who: Publisher) -> None:

return Publisher.notify(self, result)

def __ror__(self, publisher: Publisher) -> Publisher:
raise TypeError('Operator can not be applied')


class MapConstantReverse(Operator):
""" MapConstantReverse TODO """
Expand All @@ -57,9 +54,6 @@ def emit(self, value: Any_, who: Publisher) -> None:

return Publisher.notify(self, result)

def __ror__(self, publisher: Publisher) -> Publisher:
raise TypeError('Operator can not be applied')


class MapUnary(Operator):
""" MapUnary TODO """
Expand All @@ -82,9 +76,6 @@ def emit(self, value: Any_, who: Publisher) -> None:

return Publisher.notify(self, result)

def __ror__(self, publisher: Publisher) -> Publisher:
raise TypeError('Operator can not be applied')


class _GetAttr(Operator):
def __init__(self, publisher: Publisher, attribute_name) -> None:
Expand Down
5 changes: 5 additions & 0 deletions broqer/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# pylint: disable=cyclic-import
from typing import List
from broqer import Subscriber
from broqer.operator import Operator


class SubscriptionError(ValueError):
Expand Down Expand Up @@ -246,6 +247,10 @@ def add_dependencies(self, *publishers: 'Publisher') -> None:
"""
self._dependencies = self._dependencies + publishers

def __or__(self, operator: 'Operator'):
operator.originator = self
return operator

def __dir__(self):
""" Extending __dir__ with inherited type """
attrs = set(super().__dir__())
Expand Down
7 changes: 0 additions & 7 deletions tests/helper_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ def check_subscription(operator, input_vector, output_vector):
m.assert_called_once_with(output_value)


def check_use_class_as_operator(operator, input_vector, output_vector):
p = Publisher(input_vector[0])
o = p | operator # use operator before testing

check_subscription(operator, input_vector, output_vector)


def check_dependencies(operator, *_):
p = Publisher(NONE)

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

from broqer import op, NONE, Value
from tests.helper_single import check_get_method, check_subscription, \
check_dependencies, check_use_class_as_operator
check_dependencies


test_vector = [
Expand Down Expand Up @@ -71,11 +71,11 @@ def test_operator(method, o, args, kwargs, input_vector, output_vector):


@pytest.mark.parametrize('method', [check_get_method, check_subscription,
check_dependencies, check_use_class_as_operator])
check_dependencies])
@pytest.mark.parametrize('o,input_vector,output_vector',
test_vector)
def test_true_false(method, o, input_vector, output_vector):
method(o, input_vector, output_vector)
method(o(), input_vector, output_vector)


def test_filter_factory_keyword():
Expand Down
11 changes: 9 additions & 2 deletions tests/test_op_map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from broqer import op, NONE, Value
from broqer import op, NONE, Value, Publisher
from tests.helper_single import check_get_method, check_subscription, check_dependencies


Expand Down Expand Up @@ -34,4 +34,11 @@ def test_map_factory_keyword():
m = op.build_map_factory(lambda v: v+1)
v = Value()
with pytest.raises(TypeError, message='"unpack" has to be defined by decorator'):
o = v | m(unpack=True)
o = v | m(unpack=True)

def test_two_maps():
p = Publisher(1)
m1 = op.Map(lambda v: v + 1)
m2 = op.Map(lambda v: v * 2)
p2 = p | m1 | m2
assert p2.get() == 4

0 comments on commit bd9eb2d

Please sign in to comment.