Skip to content

Commit

Permalink
Merge 97730a0 into ee24692
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Sep 21, 2018
2 parents ee24692 + 97730a0 commit dc39af3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
20 changes: 18 additions & 2 deletions param/parameterized.py
Expand Up @@ -512,8 +512,10 @@ def __setattr__(self,attribute,value):
super(Parameter, self).__setattr__(attribute, value)

if old is not NotImplemented:
change = Change(what=attribute,name=self._attrib_name,obj=None,cls=self._owner,old=old,new=value)
for subscriber in self.subscribers[attribute]:
subscriber(Change(what=attribute,name=self._attrib_name,obj=None,cls=self._owner,old=old,new=value))
if self.param._changed(change):
subscriber(change)


def __get__(self,obj,objtype): # pylint: disable-msg=W0613
Expand Down Expand Up @@ -593,8 +595,11 @@ def __set__(self,obj,val):
subscribers = self.subscribers.get("value",[])
else:
subscribers = getattr(obj,"_param_subscribers",{}).get(self._attrib_name,{}).get('value',self.subscribers.get("value",[]))

change = Change(what='value',name=self._attrib_name,obj=obj,cls=self._owner,old=_old,new=val)
for s in subscribers:
s(Change(what='value',name=self._attrib_name,obj=obj,cls=self._owner,old=_old,new=val))
if self._owner.param._changed(change):
s(change)


def __delete__(self,obj):
Expand Down Expand Up @@ -802,6 +807,17 @@ def inner(*args, **kwargs):
return inner


@classmethod
def _changed(cls, change):
"""
Predicate that determines whether a Change objects has actually
changed such that old!=new.
"""
try: # To be improve by value equality testing machinery
return (change.old != change.new)
except:
return True

# CEBALERT: this is a bit ugly
def _instantiate_param(self_,param_obj,dict_=None,key=None):
# deepcopy param_obj.default into self.__dict__ (or dict_ if supplied)
Expand Down
43 changes: 43 additions & 0 deletions tests/API1/testwatch.py
@@ -0,0 +1,43 @@
"""
Unit test for watch mechanism
"""
from . import API1TestCase

import param

class TestWatch(API1TestCase):

class SimpleWatchExample(param.Parameterized):
a = param.Integer(default=0)

def setUp(self):
super(TestWatch, self).setUp()
self.switch = False

def test_triggered_when_changed(self):
def switcher(*args):
self.switch = (not self.switch)

obj = self.SimpleWatchExample()
obj.param.watch(switcher, 'a')
obj.a = 1
self.assertEqual(self.switch, True)
obj.a = 2
self.assertEqual(self.switch, False)


def test_untriggered_when_unchanged(self):
def switcher(*args):
self.switch = (not self.switch)

obj = self.SimpleWatchExample()
obj.param.watch(switcher, 'a')
obj.a = 1
self.assertEqual(self.switch, True)
obj.a = 1
self.assertEqual(self.switch, True)

if __name__ == "__main__":
import nose
nose.runmodule()

0 comments on commit dc39af3

Please sign in to comment.