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