MCPcopy Create free account
hub / github.com/ActiveState/code / Collection

Class Collection

recipes/Python/107027_Simple_Collection_class/recipe-107027.py:5–25  ·  view source on GitHub ↗

Collections group objects together and forward attribute lookups. At least this is the Collection I remember from LYMB (an interpreted OO language developed by Bill Lorensen et al at GE CRD), which almost certainly got it from Smalltalk.

Source from the content-addressed store, hash-verified

3import UserList
4
5class Collection(UserList.UserList):
6 """Collections group objects together and forward attribute lookups.
7
8 At least this is the Collection I remember from LYMB (an interpreted OO
9 language developed by Bill Lorensen et al at GE CRD), which almost
10 certainly got it from Smalltalk.
11 """
12
13 def get(self, attr):
14 """return a list of attributes from ourselves.
15
16 note that an object in the collection is not required to have 'attr',
17 so the result may be shorter than the collection itself."""
18 return Collection([getattr(x, attr) for x in self if hasattr(x, attr)])
19
20 def call(self, attr, *args, **kwds):
21 """return the result of calling 'attr' for each of our elements"""
22 attrs = self.get(attr)
23 return Collection([x(*args, **kwds)
24 for x in attrs
25 if callable(x)])
26
27if __name__ == "__main__":
28 import time

Callers 3

getMethod · 0.70
callMethod · 0.70
recipe-107027.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected