OpenQuizz
Une application de gestion des contenus pédagogiques
PBag Class Reference
Inheritance diagram for PBag:
Collaboration diagram for PBag:

Public Member Functions

def __init__ (self, counts)
 
def add (self, element)
 
def update (self, iterable)
 
def remove (self, element)
 
def count (self, element)
 
def __len__ (self)
 
def __iter__ (self)
 
def __contains__ (self, elt)
 
def __repr__ (self)
 
def __eq__ (self, other)
 
def __lt__ (self, other)
 
def __add__ (self, other)
 
def __sub__ (self, other)
 
def __or__ (self, other)
 
def __and__ (self, other)
 
def __hash__ (self)
 

Detailed Description

A persistent bag/multiset type.

Requires elements to be hashable, and allows duplicates, but has no
ordering. Bags are hashable.

Do not instantiate directly, instead use the factory functions :py:func:`b`
or :py:func:`pbag` to create an instance.

Some examples:

>>> s = pbag([1, 2, 3, 1])
>>> s2 = s.add(4)
>>> s3 = s2.remove(1)
>>> s
pbag([1, 1, 2, 3])
>>> s2
pbag([1, 1, 2, 3, 4])
>>> s3
pbag([1, 2, 3, 4])

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  counts 
)

Member Function Documentation

◆ __add__()

def __add__ (   self,
  other 
)
Combine elements from two PBags.

>>> pbag([1, 2, 2]) + pbag([2, 3, 3])
pbag([1, 2, 2, 2, 3, 3])

◆ __and__()

def __and__ (   self,
  other 
)
Intersection: Only keep elements that are present in both PBags.

>>> pbag([1, 2, 2, 2]) & pbag([2, 3, 3])
pbag([2])

◆ __contains__()

def __contains__ (   self,
  elt 
)
Check if an element is in the bag.

>>> 1 in pbag([1, 1, 2])
True
>>> 0 in pbag([1, 2])
False

◆ __eq__()

def __eq__ (   self,
  other 
)
Check if two bags are equivalent, honoring the number of duplicates,
and ignoring insertion order.

>>> pbag([1, 1, 2]) == pbag([1, 2])
False
>>> pbag([2, 1, 0]) == pbag([0, 1, 2])
True

◆ __hash__()

def __hash__ (   self)
Hash based on value of elements.

>>> m = pmap({pbag([1, 2]): "it's here!"})
>>> m[pbag([2, 1])]
"it's here!"
>>> pbag([1, 1, 2]) in m
False

◆ __iter__()

def __iter__ (   self)
Return an iterator of all elements, including duplicates.

>>> list(pbag([1, 1, 2]))
[1, 1, 2]
>>> list(pbag([1, 2]))
[1, 2]

◆ __len__()

def __len__ (   self)
Return the length including duplicates.

>>> len(pbag([1, 1, 2]))
3

◆ __lt__()

def __lt__ (   self,
  other 
)

◆ __or__()

def __or__ (   self,
  other 
)
Union: Keep elements that are present in either of two PBags.

>>> pbag([1, 2, 2, 2]) | pbag([2, 3, 3])
pbag([1, 2, 2, 2, 3, 3])

◆ __repr__()

def __repr__ (   self)

◆ __sub__()

def __sub__ (   self,
  other 
)
Remove elements from one PBag that are present in another.

>>> pbag([1, 2, 2, 2, 3]) - pbag([2, 3, 3, 4])
pbag([1, 2, 2])

◆ add()

def add (   self,
  element 
)
Add an element to the bag.

>>> s = pbag([1])
>>> s2 = s.add(1)
>>> s3 = s.add(2)
>>> s2
pbag([1, 1])
>>> s3
pbag([1, 2])

◆ count()

def count (   self,
  element 
)
Return the number of times an element appears.


>>> pbag([]).count('non-existent')
0
>>> pbag([1, 1, 2]).count(1)
2

◆ remove()

def remove (   self,
  element 
)
Remove an element from the bag.

>>> s = pbag([1, 1, 2])
>>> s2 = s.remove(1)
>>> s3 = s.remove(2)
>>> s2
pbag([1, 2])
>>> s3
pbag([1, 1])

◆ update()

def update (   self,
  iterable 
)
Update bag with all elements in iterable.

>>> s = pbag([1])
>>> s.update([1, 2])
pbag([1, 1, 2])

The documentation for this class was generated from the following file: