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.
| 3 | import UserList |
| 4 | |
| 5 | class 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 | |
| 27 | if __name__ == "__main__": |
| 28 | import time |
no outgoing calls
no test coverage detected