Skip to content

Commit 42baa9e

Browse files
committed
Modified clicker to update observers when one is added.
1 parent cdf6355 commit 42baa9e

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

diffpy/srfit/equation/clicker.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
A Clicker is an object for recording changes of state in other objects. The main
1818
functions are 'click' and '__cmp__', which changes the counter of a Clicker and
1919
compares its counter to that of other Clickers, respectively. The idea is to
20-
click a clicker whenever the state that it monitor changes. That clicker can
20+
click a clicker whenever the state that it monitors changes. That clicker can
2121
then be compared to other clickers that are used to monitor other objects.
2222
2323
Clickers of the same type share a global counter that records the total number
@@ -66,6 +66,7 @@ def __init__(self):
6666
def addObserver(self, other):
6767
"""Add a Clicker that observes this one."""
6868
self._observers.add(other)
69+
self.update()
6970
return
7071

7172
def addSubject(self, other):
@@ -107,7 +108,11 @@ def click(self):
107108
return
108109

109110
def update(self):
110-
"""Update the observers."""
111+
"""Update the local state and that of the observers.
112+
113+
This sets the local state to the global state and updates all
114+
observers.
115+
"""
111116
self._state = self.__class__._numclicks
112117
for clicker in self._observers:
113118
clicker.update()

tests/testclicker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def testClicker(self):
4242
self.assertFalse( c1 >= c2)
4343
self.assertFalse( c2 <= c1)
4444

45-
# Observe these two. Note that this does not change the state of the
46-
# observer or the subjects.
45+
# Observe these two. Note that this sets the state of the subject and
46+
# all observers equal to the global state.
4747
observer.addSubject(c1)
4848
observer.addSubject(c2)
49-
self.assertTrue( observer < c1 )
50-
self.assertTrue( observer < c2 )
51-
self.assertTrue( c2 > c1 )
49+
self.assertTrue( observer == c1 )
50+
self.assertTrue( observer == c2 )
51+
self.assertTrue( c2 == c1 )
5252

5353
# Check relations
5454
self.assertTrue( c1.hasObserver(observer) )

0 commit comments

Comments
 (0)