##// END OF EJS Templates
Replace O(N^2) algorithm with a faster one.
Jonathan Frederic -
Show More
@@ -1,52 +1,56 b''
1 """ContainerWidget class.
1 """ContainerWidget class.
2
2
3 Represents a container that can be used to group other widgets.
3 Represents a container that can be used to group other widgets.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from .widget import DOMWidget
16 from .widget import DOMWidget
17 from IPython.utils.traitlets import Unicode, Bool, List, Instance
17 from IPython.utils.traitlets import Unicode, Bool, List, Instance
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Classes
20 # Classes
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 class ContainerWidget(DOMWidget):
22 class ContainerWidget(DOMWidget):
23 view_name = Unicode('ContainerView', sync=True)
23 view_name = Unicode('ContainerView', sync=True)
24
24
25 # Keys, all private and managed by helper methods. Flexible box model
25 # Keys, all private and managed by helper methods. Flexible box model
26 # classes...
26 # classes...
27 children = List(Instance(DOMWidget))
27 children = List(Instance(DOMWidget))
28 _children = List(Instance(DOMWidget), sync=True)
28 _children = List(Instance(DOMWidget), sync=True)
29
29
30 def __init__(self, *pargs, **kwargs):
30 def __init__(self, *pargs, **kwargs):
31 """Constructor"""
31 """Constructor"""
32 DOMWidget.__init__(self, *pargs, **kwargs)
32 DOMWidget.__init__(self, *pargs, **kwargs)
33 self.on_trait_change(self._validate, ['children'])
33 self.on_trait_change(self._validate, ['children'])
34
34
35 def _validate(self, name, old, new):
35 def _validate(self, name, old, new):
36 """Validate children list.
36 """Validate children list.
37
37
38 Makes sure only one instance of any given model can exist in the
38 Makes sure only one instance of any given model can exist in the
39 children list."""
39 children list.
40 An excellent post on uniqifiers is available at
41 http://www.peterbe.com/plog/uniqifiers-benchmark
42 which provides the inspiration for using this implementation. Below
43 I've implemented the `f5` algorithm using Python comprehensions."""
40 if new is not None and isinstance(new, list):
44 if new is not None and isinstance(new, list):
41 children = []
45 seen = {}
42 for child in new:
46 def add_item(i):
43 if child not in children:
47 seen[i.model_id] = True
44 children.append(child)
48 return i
45 self._children = children
49 return [add_item(i) for i in new if not i.model_id in seen]
46
50
47
51
48 class PopupWidget(ContainerWidget):
52 class PopupWidget(ContainerWidget):
49 view_name = Unicode('PopupView', sync=True)
53 view_name = Unicode('PopupView', sync=True)
50
54
51 description = Unicode(sync=True)
55 description = Unicode(sync=True)
52 button_text = Unicode(sync=True)
56 button_text = Unicode(sync=True)
General Comments 0
You need to be logged in to leave comments. Login now