Show More
@@ -59,34 +59,30 b' class MetaComponentTracker(type):' | |||
|
59 | 59 | c.__instance_refs[c.__numcreated] = instance |
|
60 | 60 | return instance |
|
61 | 61 | |
|
62 |
def get_instances(cls, name=None, |
|
|
62 | def get_instances(cls, name=None, root=None): | |
|
63 | 63 | """Get all instances of cls and its subclasses. |
|
64 | 64 | |
|
65 | 65 | Parameters |
|
66 | 66 | ---------- |
|
67 | 67 | name : str |
|
68 | 68 | Limit to components with this name. |
|
69 | klass : class | |
|
70 | Limit to components having isinstance(component, klass) | |
|
71 | 69 | root : Component or subclass |
|
72 | 70 | Limit to components having this root. |
|
73 | 71 | """ |
|
74 | 72 | instances = cls.__instance_refs.values() |
|
75 | 73 | if name is not None: |
|
76 | 74 | instances = [i for i in instances if i.name == name] |
|
77 | if klass is not None: | |
|
78 | instances = [i for i in instances if isinstance(i, klass)] | |
|
79 | 75 | if root is not None: |
|
80 | 76 | instances = [i for i in instances if i.root == root] |
|
81 | 77 | return instances |
|
82 | 78 | |
|
83 |
def get_instances_by_condition(cls, call, name=None, |
|
|
79 | def get_instances_by_condition(cls, call, name=None, root=None): | |
|
84 | 80 | """Get all instances of cls, i such that call(i)==True. |
|
85 | 81 | |
|
86 |
This also takes the ``name`` |
|
|
82 | This also takes the ``name`` and ``root`` arguments of | |
|
87 | 83 | :meth:`get_instance` |
|
88 | 84 | """ |
|
89 |
return [i for i in cls.get_instances(name, |
|
|
85 | return [i for i in cls.get_instances(name, root) if call(i)] | |
|
90 | 86 | |
|
91 | 87 | |
|
92 | 88 | class ComponentNameGenerator(object): |
@@ -52,7 +52,7 b' class TestComponentMeta(TestCase):' | |||
|
52 | 52 | c2 = MyOtherComponent(c1) |
|
53 | 53 | c3 = MyOtherComponent(c2) |
|
54 | 54 | self.assertEquals(MyComponent.get_instances(), [c1, c2, c3]) |
|
55 |
self.assertEquals(MyComponent.get_instances( |
|
|
55 | self.assertEquals(MyOtherComponent.get_instances(), [c2, c3]) | |
|
56 | 56 | |
|
57 | 57 | def test_get_instances_root(self): |
|
58 | 58 | class MyComponent(Component): |
General Comments 0
You need to be logged in to leave comments.
Login now