toolrack.collect
Collection of objects of the same kind.
A Collection
holds objects identifying them by the value of an
attribute. For instance:
collection = Collection('SomeObject', 'name')
collection.add(obj)
will use obj.name
as key and:
collection.get('foo')
will return the object with obj.name == foo
.
The Collection
is iterable, and yields the contained objects:
for obj in collection:
# ... do something with obj
- class toolrack.collect.Collection(obj_type, key)
A Collection of objects keyed on an attribute.
It collects objects identified by the value of an attribute. No objects with duplicated keys are allowed.
- Parameters:
obj_type (
type
) – string identifying the objects type.key (
str
) – the object attribute to use as key.
- add(obj)
Add and return an object.
- clear()
Empty the collection.
- get(key)
Return the object with the specified key.
- Return type:
Any
- keys()
Return an iterator with collection keys.
- Return type:
Iterator
[str
]
- remove(key)
Remove and return the object with the specified key.
- Return type:
Any
- sorted()
Return a list of objects sorted by key.
- Return type:
list
- exception toolrack.collect.DuplicatedObject(obj_type, obj_key)
An object with the specified key is already the
Collection
.
- exception toolrack.collect.UnknownObject(obj_type, obj_key)
No object with the specified key in the
Collection
.