def test_validator_not_notified_when_theres_no_change(owner_with_validator): instance = owner_with_validator value_1 = Sentinel('value_1') value_2 = Sentinel('value_2') instance.observable = value_1 instance.observable = value_1 instance.observable = value_2 expected = [((instance, undefined_value, value_1), ), ((instance, value_1, value_2), )] assert instance.validator.call_args_list == expected
def test_observer_not_notified_when_theres_no_change( owner_with_instance_observer): instance = owner_with_instance_observer value_1 = Sentinel("value_1") value_2 = Sentinel("value_2") instance.observable = value_1 instance.observable = value_1 instance.observable = value_2 expected = [((instance, undefined_value, value_1), ), ((instance, value_1, value_2), )] assert instance.observer.call_args_list == expected
import typing as t from pca.utils.sentinel import Sentinel undefined_value = Sentinel(module="pca.data.descriptors", name="undefined_value") Owner = t.TypeVar("Owner") Value = t.TypeVar("Value")
import typing as t from pca.utils.collections import OrderedSet from pca.utils.inspect import is_argspec_valid from pca.utils.sentinel import Sentinel # sentinel object for expressing that a value of a observable hasn't been set # NB: None object might be a valid value undefined_value = Sentinel(module='pca.data.observable', name='undefined_value') Owner = t.TypeVar('Owner') Value = t.TypeVar('Value') Preprocessor = t.Callable[[Value], Value] Validator = t.Callable[[Value], None] # raises errors Observer = t.Callable[[Owner, Value, Value], None] class Observable: _owner: Owner _label: str # pattern for __dict__ key on the owner class; space char is intended to be # sure we are not colliding with any proper attribute name _observer_key_pattern = '%s instance observers' def __init__(self, default=undefined_value, preprocessor: Preprocessor = None, validator: Validator = None, class_observers: t.Iterable[Observer] = None):
def value(): return Sentinel(name='value')
import typing as t from pca.utils.sentinel import Sentinel undefined_value = Sentinel(module='pca.data.descriptors', name='undefined_value') Owner = t.TypeVar('Owner') Value = t.TypeVar('Value')
import typing as t from pca.utils.collections import OrderedSet from pca.utils.inspect import is_argspec_valid from pca.utils.sentinel import Sentinel # sentinel object for expressing that a value of a observable hasn't been set # NB: None object might be a valid value undefined_value = Sentinel(module="pca.data.observable", name="undefined_value") Owner = t.TypeVar("Owner") Value = t.TypeVar("Value") Preprocessor = t.Callable[[Value], Value] Validator = t.Callable[[Value], None] # raises errors Observer = t.Callable[[Owner, Value, Value], None] class Observable: _owner: Owner _label: str # pattern for __dict__ key on the owner class; space char is intended to be # sure we are not colliding with any proper attribute name _observer_key_pattern = "%s instance observers" def __init__( self, default=undefined_value, preprocessor: Preprocessor = None, validator: Validator = None,
def value(): return Sentinel(name="value")