IdCollection Module

See this module if you like to store object s that have an id attribute.

class knittingpattern.IdCollection.IdCollection[source]

Bases: object

This is a collections of object that have an id attribute.

__bool__()[source]
Returns:whether there is anything in the collection.
Return type:bool
__getitem__(id_)[source]

Get the object with the id

ic = IdCollection()
ic.append(object_1)
ic.append(object_2)
assert ic[object_1.id] == object_1
assert ic[object_2.id] == object_1
Parameters:id_ – the id of an object
Returns:the object with the id
Raises:KeyError – if no object with id was found
__init__()[source]

Create a new IdCollection with no arguments.

You can add objects later using the method append().

__iter__()[source]

allows you to iterate and use for-loops

The objects in the iterator have the order in which they were appended.

__len__()[source]
Returns:the number of objects in this collection
__weakref__

list of weak references to the object (if defined)

append(item)[source]

Add an object to the end of the IdCollection.

Parameters:item – an object that has an id
at(index)[source]

Get the object at an index.

Parameters:index (int) – the index of the object
Returns:the object at index
first

The first element in this collection.

Returns:the first element in this collection
Raises:IndexError – if this collection is empty