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

Public Member Functions

def __init__ (self, func, callback_kwd='callback', wait_seconds=0.1)
 
def __enter__ (self)
 
def __exit__ (self, exc_type, exc_value, traceback)
 
def __iter__ (self)
 
def __next__ (self)
 
def done (self)
 
def result (self)
 

Detailed Description

Convert a function that uses callbacks to an iterator.

Let *func* be a function that takes a `callback` keyword argument.
For example:

>>> def func(callback=None):
...     for i, c in [(1, 'a'), (2, 'b'), (3, 'c')]:
...         if callback:
...             callback(i, c)
...     return 4


Use ``with callback_iter(func)`` to get an iterator over the parameters
that are delivered to the callback.

>>> with callback_iter(func) as it:
...     for args, kwargs in it:
...         print(args)
(1, 'a')
(2, 'b')
(3, 'c')

The function will be called in a background thread. The ``done`` property
indicates whether it has completed execution.

>>> it.done
True

If it completes successfully, its return value will be available
in the ``result`` property.

>>> it.result
4

Notes:

* If the function uses some keyword argument besides ``callback``, supply
  *callback_kwd*.
* If it finished executing, but raised an exception, accessing the
  ``result`` property will raise the same exception.
* If it hasn't finished executing, accessing the ``result``
  property from within the ``with`` block will raise ``RuntimeError``.
* If it hasn't finished executing, accessing the ``result`` property from
  outside the ``with`` block will raise a
  ``more_itertools.AbortThread`` exception.
* Provide *wait_seconds* to adjust how frequently the it is polled for
  output.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  func,
  callback_kwd = 'callback',
  wait_seconds = 0.1 
)

Member Function Documentation

◆ __enter__()

def __enter__ (   self)

◆ __exit__()

def __exit__ (   self,
  exc_type,
  exc_value,
  traceback 
)

◆ __iter__()

def __iter__ (   self)

◆ __next__()

def __next__ (   self)

◆ done()

def done (   self)

◆ result()

def result (   self)

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