##// END OF EJS Templates
fixes to make Connect pass tests
Jonathan Frederic -
Show More
@@ -32,7 +32,7 from IPython.utils.traitlets import (
32 HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict,
32 HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict,
33 Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError,
33 Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError,
34 Undefined, Type, This, Instance, TCPAddress, List, Tuple,
34 Undefined, Type, This, Instance, TCPAddress, List, Tuple,
35 ObjectName, DottedObjectName, CRegExp
35 ObjectName, DottedObjectName, CRegExp, Connect
36 )
36 )
37 from IPython.utils import py3compat
37 from IPython.utils import py3compat
38 from IPython.testing.decorators import skipif
38 from IPython.testing.decorators import skipif
@@ -974,11 +974,12 def test_dict_assignment():
974 nt.assert_equal(d, c.value)
974 nt.assert_equal(d, c.value)
975 nt.assert_true(c.value is d)
975 nt.assert_true(c.value is d)
976
976
977 def test_connect_same:
977 class TestConnect(TestCase):
978 def test_connect_same(self):
978 """Verify two traitlets of the same type can be bound together using Connect"""
979 """Verify two traitlets of the same type can be bound together using Connect"""
979
980
980 # Create two simple classes with Int traitlets.
981 # Create two simple classes with Int traitlets.
981 class A():
982 class A(HasTraits):
982 value = Int()
983 value = Int()
983 a = A(value=9)
984 a = A(value=9)
984 b = A(value=8)
985 b = A(value=8)
@@ -987,19 +988,19 def test_connect_same:
987 c = Connect((a, 'value'), (b, 'value'))
988 c = Connect((a, 'value'), (b, 'value'))
988
989
989 # Make sure the values are the same at the point of connection.
990 # Make sure the values are the same at the point of connection.
990 assertEqual(a.value, b.value)
991 self.assertEqual(a.value, b.value)
991
992
992 # Change one of the values to make sure they stay in sync.
993 # Change one of the values to make sure they stay in sync.
993 a.value = 5
994 a.value = 5
994 assertEqual(a.value, b.value)
995 self.assertEqual(a.value, b.value)
995
996
996 def test_connect_different:
997 def test_connect_different(self):
997 """Verify two traitlets of different types can be bound together using Connect"""
998 """Verify two traitlets of different types can be bound together using Connect"""
998
999
999 # Create two simple classes with Int traitlets.
1000 # Create two simple classes with Int traitlets.
1000 class A():
1001 class A(HasTraits):
1001 value = Int()
1002 value = Int()
1002 class B():
1003 class B(HasTraits):
1003 count = Int()
1004 count = Int()
1004 a = A(value=9)
1005 a = A(value=9)
1005 b = B(count=8)
1006 b = B(count=8)
@@ -1008,17 +1009,17 def test_connect_different:
1008 c = Connect((a, 'value'), (b, 'count'))
1009 c = Connect((a, 'value'), (b, 'count'))
1009
1010
1010 # Make sure the values are the same at the point of connection.
1011 # Make sure the values are the same at the point of connection.
1011 assertEqual(a.value, b.count)
1012 self.assertEqual(a.value, b.count)
1012
1013
1013 # Change one of the values to make sure they stay in sync.
1014 # Change one of the values to make sure they stay in sync.
1014 a.value = 5
1015 a.value = 5
1015 assertEqual(a.value, b.count)
1016 self.assertEqual(a.value, b.count)
1016
1017
1017 def test_disconnect:
1018 def test_disconnect(self):
1018 """Verify two connected traitlets can be disconnected"""
1019 """Verify two connected traitlets can be disconnected"""
1019
1020
1020 # Create two simple classes with Int traitlets.
1021 # Create two simple classes with Int traitlets.
1021 class A():
1022 class A(HasTraits):
1022 value = Int()
1023 value = Int()
1023 a = A(value=9)
1024 a = A(value=9)
1024 b = A(value=8)
1025 b = A(value=8)
@@ -1030,4 +1031,4 def test_disconnect:
1030
1031
1031 # Change one of the values to make sure they stay in sync.
1032 # Change one of the values to make sure they stay in sync.
1032 a.value = 5
1033 a.value = 5
1033 assertNotEqual(a.value, b.value) No newline at end of file
1034 self.assertNotEqual(a.value, b.value)
@@ -199,10 +199,17 class Connect(object):
199 """
199 """
200 updating = False
200 updating = False
201 def __init__(self, *args):
201 def __init__(self, *args):
202 if len(args) < 2:
203 raise TypeError('At least two traitlets must be provided.')
204
202 self.objects = args
205 self.objects = args
203 for obj,attr in args:
206 for obj,attr in args:
204 obj.on_trait_change(self._update, attr)
207 obj.on_trait_change(self._update, attr)
205
208
209 # Syncronize the traitlets initially.
210 initial = getattr(args[0][0], args[0][1])
211 self._update(args[0][1], initial, initial)
212
206 @contextlib.contextmanager
213 @contextlib.contextmanager
207 def _busy_updating(self):
214 def _busy_updating(self):
208 self.updating = True
215 self.updating = True
General Comments 0
You need to be logged in to leave comments. Login now