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: type, key: str)¶ 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 – string identifying the objects type.
- key – the object attribute to use as key.
-
add
(obj: Any)¶ Add and return an object.
-
clear
()¶ Empty the collection.
-
get
(key: str) → Any¶ Return the object with the specified key.
-
keys
() → Iterator[str]¶ Return an iterator with collection keys.
-
remove
(key: str) → Any¶ Remove and return the object with the specified key.
-
sorted
() → List[T]¶ Return a list of objects sorted by key.
-
exception
toolrack.collect.
DuplicatedObject
(obj_type: type, obj_key: str)¶ An object with the specified key is already the
Collection
.
-
exception
toolrack.collect.
UnknownObject
(obj_type, obj_key: str)¶ No object with the specified key in the
Collection
.