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 builtinsets
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 theCollection
.If any of the elements in the
Collection
are bad then bad should be set toTrue
and thedict
errors should map the item to it’s exception.All of these customizations are managed when operations occur on the
Collection
and if 2Collections
are modified with one of the binary operators (|
,-
, etc) the_collectedTypes
anderrors
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 containedallowedTypes :
set[type]
A set of types,{object}
will allow virtually everythingcollectedTypes :
set[type]
The types (or supertypes) of the objects in inSetname :
str
The name of theCollection
bad :
bool
If any of the elements are baderrors :
dict[:Exception]
A mapping from items to their errorsquietStart :
optional [bool]
DefaultFalse
, 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 smallerCollections
-
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 smallerCollections
. The sourceCollection
will be empty after this operation
-