13. collection — Tools for handling collections of elements belonging to multiple parts.

This module defines the Collection class.

13.1. Classes defined in module collection

class collection.Collection(obj_type=None, keep_order=False)[source]

A collection is a set of (int,int) tuples.

The first part of the tuple has a limited number of values and are used as the keys in a dict. The second part can have a lot of different values and is implemented as an integer array with unique values. This is e.g. used to identify a set of individual parts of one or more OpenGL actors.

Examples

>>> a = Collection()
>>> a.add(range(7),3)
>>> a.add(range(4))
>>> a.remove([2,4],3)
>>> print(a)
-1 [0 1 2 3]; 3 [0 1 3 5 6];
>>> a.add([[2,0],[2,3],[-1,7],[3,88]])
>>> print(a)
-1 [0 1 2 3 7]; 2 [0 3]; 3 [ 0  1  3  5  6 88];
>>> print(a.total())
13
>>> a[2] = [1,2,3]
>>> print(a)
-1 [0 1 2 3 7]; 2 [1 2 3]; 3 [ 0  1  3  5  6 88];
>>> a[2] = []
>>> print(a)
-1 [0 1 2 3 7]; 3 [ 0  1  3  5  6 88];
>>> a.set([[2,0],[2,3],[-1,7],[3,88]])
>>> print(a)
-1 [7]; 2 [0 3]; 3 [88];
>>> print(a.keys())
[-1  2  3]
>>> for k, i in a.singles(): print(k,i)
-1 7
2 0
2 3
3 88
>>> print([k for k in a])
[-1, 2, 3]
>>> 2 in a
True
>>> del a[2]
>>> 2 in a
False
add(data, key=-1)[source]

Add new data to a Collection.

Parameters:
  • data (array_like | Collection) – An int array with shape (ndata, 2) or (ndata,), or another Collection with the same obj_type as self. If an array with shape (ndata, 2), each row has a key value in column 0 and a data value in column 1. If an (ndata,) shaped array, all items have the same key and it has to be specified separately.

  • key (int) – The key value if data is an (ndata,) shaped array. Else ignored.

  • keep_order

  • separately

  • used. (or a default value will be) –

  • Collection (data can also be another) –

  • object (if it has the same) –

  • type.

set(data, key=-1)[source]

Set the collection to the specified data.

This is equivalent to clearing the corresponding keys before adding.

remove(data, key=-1)[source]

Remove data from the collection.

get(key, default=[])[source]

Return item with given key or default.

keys()[source]

Return a sorted array with the keys

items()[source]

Return an iterator over (key,value) pairs.

singles()[source]

Return an iterator over the single instances

Returns next (k, i) pair from the collection.

13.2. Functions defined in module collection

collection.uniq(data, keep_order)[source]

Uniqify data, keeping order if needed

collection.uniq_add(a, b, keep_order)[source]

Add to a uniq array. a is supposed to be uniq, not checked!

collection.uniq_remove(a, b, keep_order)[source]

Remove from a uniq array. a is supposed to be uniq, not checked!