Show More
@@ -1187,6 +1187,25 b' class TestLink(TestCase):' | |||||
1187 | a.value = 4 |
|
1187 | a.value = 4 | |
1188 | self.assertEqual(''.join(callback_count), 'ab') |
|
1188 | self.assertEqual(''.join(callback_count), 'ab') | |
1189 | del callback_count[:] |
|
1189 | del callback_count[:] | |
|
1190 | ||||
|
1191 | def test_validate_args(self): | |||
|
1192 | class A(HasTraits): | |||
|
1193 | value = Int() | |||
|
1194 | class B(HasTraits): | |||
|
1195 | count = Int() | |||
|
1196 | a = A(value=9) | |||
|
1197 | b = B(count=8) | |||
|
1198 | b.value = 5 | |||
|
1199 | ||||
|
1200 | with self.assertRaises(TypeError): | |||
|
1201 | link((a, 'value')) | |||
|
1202 | with self.assertRaises(TypeError): | |||
|
1203 | link((a, 'value', 'count'), (b, 'count')) | |||
|
1204 | with self.assertRaises(TypeError): | |||
|
1205 | link((a, 'value'), (b, 'value')) | |||
|
1206 | with self.assertRaises(TypeError): | |||
|
1207 | link((a, 'traits'), (b, 'count')) | |||
|
1208 | ||||
1190 |
|
1209 | |||
1191 | class TestDirectionalLink(TestCase): |
|
1210 | class TestDirectionalLink(TestCase): | |
1192 | def test_connect_same(self): |
|
1211 | def test_connect_same(self): | |
@@ -1253,6 +1272,25 b' class TestDirectionalLink(TestCase):' | |||||
1253 | a.value = 5 |
|
1272 | a.value = 5 | |
1254 | self.assertNotEqual(a.value, b.value) |
|
1273 | self.assertNotEqual(a.value, b.value) | |
1255 |
|
1274 | |||
|
1275 | def test_validate_args(self): | |||
|
1276 | class A(HasTraits): | |||
|
1277 | value = Int() | |||
|
1278 | class B(HasTraits): | |||
|
1279 | count = Int() | |||
|
1280 | a = A(value=9) | |||
|
1281 | b = B(count=8) | |||
|
1282 | b.value = 5 | |||
|
1283 | ||||
|
1284 | with self.assertRaises(TypeError): | |||
|
1285 | directional_link((a, 'value')) | |||
|
1286 | with self.assertRaises(TypeError): | |||
|
1287 | directional_link((a, 'value', 'count'), (b, 'count')) | |||
|
1288 | with self.assertRaises(TypeError): | |||
|
1289 | directional_link((a, 'value'), (b, 'value')) | |||
|
1290 | with self.assertRaises(TypeError): | |||
|
1291 | directional_link((a, 'traits'), (b, 'count')) | |||
|
1292 | ||||
|
1293 | ||||
1256 | class Pickleable(HasTraits): |
|
1294 | class Pickleable(HasTraits): | |
1257 | i = Int() |
|
1295 | i = Int() | |
1258 | j = Int() |
|
1296 | j = Int() |
@@ -172,13 +172,24 b' def getmembers(object, predicate=None):' | |||||
172 | results.sort() |
|
172 | results.sort() | |
173 | return results |
|
173 | return results | |
174 |
|
174 | |||
|
175 | def _validate_link(*tuples): | |||
|
176 | """Validate arguments for traitlet link functions""" | |||
|
177 | for t in tuples: | |||
|
178 | if not len(t) == 2: | |||
|
179 | raise TypeError("Each linked traitlet must be specified as (HasTraits, 'trait_name'), not %r" % t) | |||
|
180 | obj, trait_name = t | |||
|
181 | if not isinstance(obj, HasTraits): | |||
|
182 | raise TypeError("Each object must be HasTraits, not %r" % type(obj)) | |||
|
183 | if not trait_name in obj.traits(): | |||
|
184 | raise TypeError("%r has no trait %r" % (obj, trait_name)) | |||
|
185 | ||||
175 | @skip_doctest |
|
186 | @skip_doctest | |
176 | class link(object): |
|
187 | class link(object): | |
177 | """Link traits from different objects together so they remain in sync. |
|
188 | """Link traits from different objects together so they remain in sync. | |
178 |
|
189 | |||
179 | Parameters |
|
190 | Parameters | |
180 | ---------- |
|
191 | ---------- | |
181 |
|
|
192 | *args : pairs of objects/attributes | |
182 |
|
193 | |||
183 | Examples |
|
194 | Examples | |
184 | -------- |
|
195 | -------- | |
@@ -190,6 +201,7 b' class link(object):' | |||||
190 | def __init__(self, *args): |
|
201 | def __init__(self, *args): | |
191 | if len(args) < 2: |
|
202 | if len(args) < 2: | |
192 | raise TypeError('At least two traitlets must be provided.') |
|
203 | raise TypeError('At least two traitlets must be provided.') | |
|
204 | _validate_link(*args) | |||
193 |
|
205 | |||
194 | self.objects = {} |
|
206 | self.objects = {} | |
195 |
|
207 | |||
@@ -245,6 +257,9 b' class directional_link(object):' | |||||
245 | updating = False |
|
257 | updating = False | |
246 |
|
258 | |||
247 | def __init__(self, source, *targets): |
|
259 | def __init__(self, source, *targets): | |
|
260 | if len(targets) < 1: | |||
|
261 | raise TypeError('At least two traitlets must be provided.') | |||
|
262 | _validate_link(source, *targets) | |||
248 | self.source = source |
|
263 | self.source = source | |
249 | self.targets = targets |
|
264 | self.targets = targets | |
250 |
|
265 | |||
@@ -276,9 +291,7 b' class directional_link(object):' | |||||
276 | self.source = None |
|
291 | self.source = None | |
277 | self.targets = [] |
|
292 | self.targets = [] | |
278 |
|
293 | |||
279 | def dlink(source, *targets): |
|
294 | dlink = directional_link | |
280 | """Shorter helper function returning a directional_link object""" |
|
|||
281 | return directional_link(source, *targets) |
|
|||
282 |
|
295 | |||
283 | #----------------------------------------------------------------------------- |
|
296 | #----------------------------------------------------------------------------- | |
284 | # Base TraitType for all traits |
|
297 | # Base TraitType for all traits |
General Comments 0
You need to be logged in to leave comments.
Login now