Skip to content

medcat.utils.iterutils

Functions:

  • callback_iterator

    Get an iterable with callback function to identify number of items.

Attributes:

  • T

T module-attribute

T = TypeVar('T')

callback_iterator

callback_iterator(identifier: str, data_iterator: Iterable[T], callback: Callable[[str, int], None]) -> Iterable[T]

Get an iterable with callback function to identify number of items.

If the data has a size (i.e list or dict), the length of the data will be reported before iteration.

If the data doesn't have a size (i.e a generator), the number of items iterated will be reported after the iteration is done.

Parameters:

  • identifier

    (str) –

    The identifier / name for the iterator.

  • data_iterator

    (Iterable[T]) –

    The iterator.

  • callback

    (Callable[[str, int], None]) –

    The callback method.

Returns:

  • Iterable[T]

    Iterable[T]: The wrapped iterator.

Source code in medcat-v2/medcat/utils/iterutils.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def callback_iterator(identifier: str, data_iterator: Iterable[T],
                      callback: Callable[[str, int], None]) -> Iterable[T]:
    """Get an iterable with callback function to identify number of items.

    If the data has a size (i.e list or dict), the length of the data will
    be reported before iteration.

    If the data doesn't have a size (i.e a generator), the number of items
    iterated will be reported after the iteration is done.

    Args:
        identifier (str): The identifier / name for the iterator.
        data_iterator (Iterable[T]): The iterator.
        callback (Callable[[str, int], None]): The callback method.

    Returns:
        Iterable[T]: The wrapped iterator.
    """
    if isinstance(data_iterator, Sized):
        callback(identifier, len(data_iterator))
        return data_iterator
    return _callback_iterator_iterable(identifier, data_iterator, callback)