Show More
@@ -52,7 +52,7 b' Authors:' | |||||
52 | # Imports |
|
52 | # Imports | |
53 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
54 |
|
54 | |||
55 |
|
55 | import contextlib | ||
56 | import inspect |
|
56 | import inspect | |
57 | import re |
|
57 | import re | |
58 | import sys |
|
58 | import sys | |
@@ -182,6 +182,42 b' def getmembers(object, predicate=None):' | |||||
182 | results.sort() |
|
182 | results.sort() | |
183 | return results |
|
183 | return results | |
184 |
|
184 | |||
|
185 | class Connect(object): | |||
|
186 | """Connect traits from different objects together so they remain in sync. | |||
|
187 | ||||
|
188 | Parameters | |||
|
189 | ---------- | |||
|
190 | obj : pairs of objects/attributes | |||
|
191 | ||||
|
192 | Examples | |||
|
193 | -------- | |||
|
194 | ||||
|
195 | >>> c = Connect((obj1, 'value'), (obj2, 'value'), (obj3, 'value')) | |||
|
196 | >>> obj1.value = 5 # updates other objects as well | |||
|
197 | """ | |||
|
198 | updating = False | |||
|
199 | def __init__(self, *args): | |||
|
200 | ||||
|
201 | self.objects = args | |||
|
202 | for obj,attr in args: | |||
|
203 | obj.on_trait_change(self._update, attr) | |||
|
204 | ||||
|
205 | @contextlib.contextmanager | |||
|
206 | def _busy_updating(self): | |||
|
207 | self.updating = True | |||
|
208 | yield | |||
|
209 | self.updating = False | |||
|
210 | ||||
|
211 | def _update(self, name, old, new): | |||
|
212 | if self.updating: | |||
|
213 | return | |||
|
214 | with self._busy_updating(): | |||
|
215 | for obj,attr in self.objects: | |||
|
216 | setattr(obj, attr, new) | |||
|
217 | ||||
|
218 | def disconnect(self): | |||
|
219 | for obj,attr in self.objects: | |||
|
220 | obj.on_trait_change(self._update, attr, remove=True) | |||
185 |
|
221 | |||
186 | #----------------------------------------------------------------------------- |
|
222 | #----------------------------------------------------------------------------- | |
187 | # Base TraitType for all traits |
|
223 | # Base TraitType for all traits |
General Comments 0
You need to be logged in to leave comments.
Login now