Collection(MutableSet, Hashable)

class metaknowledge.Collection(inSet, allowedTypes, collectedTypes, name, bad, errors, quietStart=False)

A named hashable set with some error reporting.

Collections have all the methods of builtin sets as well as error reporting with bad and error, and control over the contained items with allowedTypes and collectedTypes.

Customizations

When created name should be a string that allows users to easily determine the source of the Collection

When created the you must provided a set of types, allowedTypes, when new items are added they will be checked and if they are not instances of any of the types an CollectionTypeError exception will be raised. The collectedTypes set that is provided should be a set of only the types in the Collection.

If any of the elements in the Collection are bad then bad should be set to True and the dict errors should map the item to it’s exception.

All of these customizations are managed when operations occur on the Collection and if 2 Collections are modified with one of the binary operators (|, -, etc) the _collectedTypes and errors attributes will be modified the same way. name will be updated to explain the operation(s) that occurred.

__Init__

As Collection is mostly meant to be base for other classes all but one of the arguments in the __Init__ are not optional and the optional one is not used.

Parameters

inSet : set

The objects to be contained

allowedTypes : set[type]

A set of types, {object} will allow virtually everything

collectedTypes : set[type]

The types (or supertypes) of the objects in inSet

name : str

The name of the Collection

bad : bool

If any of the elements are bad

errors : dict[:Exception]

A mapping from items to their errors

quietStart : optional [bool]

Default False, does nothing. This is here for use as a interface by subclasses
__eq__(other)

Return self==value.

__ge__(other)

Return self>=value.

__hash__()

Return hash(self).

__init__(inSet, allowedTypes, collectedTypes, name, bad, errors, quietStart=False)

Basically a collections.abc.MutableSet wrapper for a set with a bunch of extra record keeping attached.

__le__(other)

Return self<=value.

__repr__()

Return repr(self).

__str__()

Return str(self).

__weakref__

list of weak references to the object (if defined)

add(elem)

Adds elem to the collection.

chunk(maxSize)

Splits the Collection into maxSize size or smaller Collections

clear()

“Removes all elements from the collection and resets the error handling

copy()

Creates a shallow copy of the collection

discard(elem)

Removes elem from the collection, will not raise an Exception if elem is missing

peek()

returns a random element from the collection. If ran twice the same element will usually be returned

pop()

Removes a random element from the collection and returns it

remove(elem)

Removes elem from the collection, will raise a KeyError is elem is missing

split(maxSize)

Destructively, splits the Collection into maxSize size or smaller Collections. The source Collection will be empty after this operation