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,60 +974,61 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 | """Verify two traitlets of the same type can be bound together using Connect""" |
|
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 | # 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) | |
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) |
|
|||
991 |
|
986 | |||
992 | # Change one of the values to make sure they stay in sync. |
|
987 | # Conenct the two classes. | |
993 | a.value = 5 |
|
988 | c = Connect((a, 'value'), (b, 'value')) | |
994 | assertEqual(a.value, b.value) |
|
|||
995 |
|
989 | |||
996 | def test_connect_different: |
|
990 | # Make sure the values are the same at the point of connection. | |
997 | """Verify two traitlets of different types can be bound together using Connect""" |
|
991 | self.assertEqual(a.value, b.value) | |
998 |
|
992 | |||
999 | # Create two simple classes with Int traitlets. |
|
993 | # Change one of the values to make sure they stay in sync. | |
1000 | class A(): |
|
994 | a.value = 5 | |
1001 | value = Int() |
|
995 | self.assertEqual(a.value, b.value) | |
1002 | class B(): |
|
|||
1003 | count = Int() |
|
|||
1004 | a = A(value=9) |
|
|||
1005 | b = B(count=8) |
|
|||
1006 |
|
996 | |||
1007 | # Conenct the two classes. |
|
997 | def test_connect_different(self): | |
1008 | c = Connect((a, 'value'), (b, 'count')) |
|
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. |
|
1000 | # Create two simple classes with Int traitlets. | |
1011 | assertEqual(a.value, b.count) |
|
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. |
|
1008 | # Conenct the two classes. | |
1014 | a.value = 5 |
|
1009 | c = Connect((a, 'value'), (b, 'count')) | |
1015 | assertEqual(a.value, b.count) |
|
|||
1016 |
|
1010 | |||
1017 | def test_disconnect: |
|
1011 | # Make sure the values are the same at the point of connection. | |
1018 | """Verify two connected traitlets can be disconnected""" |
|
1012 | self.assertEqual(a.value, b.count) | |
1019 |
|
1013 | |||
1020 | # Create two simple classes with Int traitlets. |
|
1014 | # Change one of the values to make sure they stay in sync. | |
1021 | class A(): |
|
1015 | a.value = 5 | |
1022 | value = Int() |
|
1016 | self.assertEqual(a.value, b.count) | |
1023 | a = A(value=9) |
|
|||
1024 | b = A(value=8) |
|
|||
1025 |
|
1017 | |||
1026 | # Conenct the two classes. |
|
1018 | def test_disconnect(self): | |
1027 | c = Connect((a, 'value'), (b, 'value')) |
|
1019 | """Verify two connected traitlets can be disconnected""" | |
1028 | a.value = 4 |
|
|||
1029 | c.disconnect() |
|
|||
1030 |
|
1020 | |||
1031 | # Change one of the values to make sure they stay in sync. |
|
1021 | # Create two simple classes with Int traitlets. | |
1032 | a.value = 5 |
|
1022 | class A(HasTraits): | |
1033 | assertNotEqual(a.value, b.value) No newline at end of file |
|
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 | 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