Show More
@@ -32,7 +32,7 b' 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, bind | |
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,9 +974,9 b' 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 |
class Test |
|
977 | class TestBind(TestCase): | |
978 | def test_connect_same(self): |
|
978 | def test_connect_same(self): | |
979 |
"""Verify two traitlets of the same type can be bound together using |
|
979 | """Verify two traitlets of the same type can be bound together using bind.""" | |
980 |
|
980 | |||
981 | # Create two simple classes with Int traitlets. |
|
981 | # Create two simple classes with Int traitlets. | |
982 | class A(HasTraits): |
|
982 | class A(HasTraits): | |
@@ -985,17 +985,17 b' class TestConnect(TestCase):' | |||||
985 | b = A(value=8) |
|
985 | b = A(value=8) | |
986 |
|
986 | |||
987 | # Conenct the two classes. |
|
987 | # Conenct the two classes. | |
988 |
c = |
|
988 | c = bind((a, 'value'), (b, 'value')) | |
989 |
|
989 | |||
990 |
# Make sure the values are the same at the point of |
|
990 | # Make sure the values are the same at the point of binding. | |
991 | self.assertEqual(a.value, b.value) |
|
991 | self.assertEqual(a.value, b.value) | |
992 |
|
992 | |||
993 | # 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. | |
994 | a.value = 5 |
|
994 | a.value = 5 | |
995 | self.assertEqual(a.value, b.value) |
|
995 | self.assertEqual(a.value, b.value) | |
996 |
|
996 | |||
997 |
def test_ |
|
997 | def test_bind_different(self): | |
998 |
"""Verify two traitlets of different types can be bound together using |
|
998 | """Verify two traitlets of different types can be bound together using bind.""" | |
999 |
|
999 | |||
1000 | # Create two simple classes with Int traitlets. |
|
1000 | # Create two simple classes with Int traitlets. | |
1001 | class A(HasTraits): |
|
1001 | class A(HasTraits): | |
@@ -1006,17 +1006,17 b' class TestConnect(TestCase):' | |||||
1006 | b = B(count=8) |
|
1006 | b = B(count=8) | |
1007 |
|
1007 | |||
1008 | # Conenct the two classes. |
|
1008 | # Conenct the two classes. | |
1009 |
c = |
|
1009 | c = bind((a, 'value'), (b, 'count')) | |
1010 |
|
1010 | |||
1011 |
# Make sure the values are the same at the point of |
|
1011 | # Make sure the values are the same at the point of binding. | |
1012 | self.assertEqual(a.value, b.count) |
|
1012 | self.assertEqual(a.value, b.count) | |
1013 |
|
1013 | |||
1014 | # 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. | |
1015 | a.value = 5 |
|
1015 | a.value = 5 | |
1016 | self.assertEqual(a.value, b.count) |
|
1016 | self.assertEqual(a.value, b.count) | |
1017 |
|
1017 | |||
1018 |
def test_ |
|
1018 | def test_unbind(self): | |
1019 |
"""Verify two |
|
1019 | """Verify two binded traitlets can be unbinded.""" | |
1020 |
|
1020 | |||
1021 | # Create two simple classes with Int traitlets. |
|
1021 | # Create two simple classes with Int traitlets. | |
1022 | class A(HasTraits): |
|
1022 | class A(HasTraits): | |
@@ -1025,9 +1025,9 b' class TestConnect(TestCase):' | |||||
1025 | b = A(value=8) |
|
1025 | b = A(value=8) | |
1026 |
|
1026 | |||
1027 | # Conenct the two classes. |
|
1027 | # Conenct the two classes. | |
1028 |
c = |
|
1028 | c = bind((a, 'value'), (b, 'value')) | |
1029 | a.value = 4 |
|
1029 | a.value = 4 | |
1030 |
c. |
|
1030 | c.unbind() | |
1031 |
|
1031 | |||
1032 | # 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. | |
1033 | a.value = 5 |
|
1033 | a.value = 5 |
@@ -184,8 +184,8 b' def getmembers(object, predicate=None):' | |||||
184 | return results |
|
184 | return results | |
185 |
|
185 | |||
186 | @skip_doctest |
|
186 | @skip_doctest | |
187 |
class |
|
187 | class bind(object): | |
188 |
""" |
|
188 | """Bind traits from different objects together so they remain in sync. | |
189 |
|
189 | |||
190 | Parameters |
|
190 | Parameters | |
191 | ---------- |
|
191 | ---------- | |
@@ -194,7 +194,7 b' class Connect(object):' | |||||
194 | Examples |
|
194 | Examples | |
195 | -------- |
|
195 | -------- | |
196 |
|
196 | |||
197 |
>>> c = |
|
197 | >>> c = bind((obj1, 'value'), (obj2, 'value'), (obj3, 'value')) | |
198 | >>> obj1.value = 5 # updates other objects as well |
|
198 | >>> obj1.value = 5 # updates other objects as well | |
199 | """ |
|
199 | """ | |
200 | updating = False |
|
200 | updating = False | |
@@ -225,7 +225,7 b' class Connect(object):' | |||||
225 | for obj,attr in self.objects: |
|
225 | for obj,attr in self.objects: | |
226 | setattr(obj, attr, new) |
|
226 | setattr(obj, attr, new) | |
227 |
|
227 | |||
228 |
def |
|
228 | def unbind(self): | |
229 | for obj,attr in self.objects: |
|
229 | for obj,attr in self.objects: | |
230 | obj.on_trait_change(self._update, attr, remove=True) |
|
230 | obj.on_trait_change(self._update, attr, remove=True) | |
231 |
|
231 |
General Comments 0
You need to be logged in to leave comments.
Login now