OpenQuizz
Une application de gestion des contenus pédagogiques
bucket Class Reference

Public Member Functions

def __init__ (self, iterable, key, validator=None)
 
def __contains__ (self, value)
 
def __iter__ (self)
 
def __getitem__ (self, value)
 

Detailed Description

Wrap *iterable* and return an object that buckets it iterable into
child iterables based on a *key* function.

    >>> iterable = ['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'b3']
    >>> s = bucket(iterable, key=lambda x: x[0])  # Bucket by 1st character
    >>> sorted(list(s))  # Get the keys
    ['a', 'b', 'c']
    >>> a_iterable = s['a']
    >>> next(a_iterable)
    'a1'
    >>> next(a_iterable)
    'a2'
    >>> list(s['b'])
    ['b1', 'b2', 'b3']

The original iterable will be advanced and its items will be cached until
they are used by the child iterables. This may require significant storage.

By default, attempting to select a bucket to which no items belong  will
exhaust the iterable and cache all values.
If you specify a *validator* function, selected buckets will instead be
checked against it.

    >>> from itertools import count
    >>> it = count(1, 2)  # Infinite sequence of odd numbers
    >>> key = lambda x: x % 10  # Bucket by last digit
    >>> validator = lambda x: x in {1, 3, 5, 7, 9}  # Odd digits only
    >>> s = bucket(it, key=key, validator=validator)
    >>> 2 in s
    False
    >>> list(s[2])
    []

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  iterable,
  key,
  validator = None 
)

Member Function Documentation

◆ __contains__()

def __contains__ (   self,
  value 
)

◆ __getitem__()

def __getitem__ (   self,
  value 
)

◆ __iter__()

def __iter__ (   self)

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