Skip to content

Iterables

make_batches(iterable, batch_size)

Make batches from an iterable.

Parameters:

Name Type Description Default
iterable Iterable[T]

The iterable to make batches from.

required
batch_size int

The size of the batches.

required

Returns:

Type Description
None

The batches.

Source code in inference_sdk/http/utils/iterables.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def make_batches(
    iterable: Iterable[T], batch_size: int
) -> Generator[List[T], None, None]:
    """Make batches from an iterable.

    Args:
        iterable: The iterable to make batches from.
        batch_size: The size of the batches.

    Returns:
        The batches.
    """
    batch_size = max(batch_size, 1)
    batch = []
    for element in iterable:
        batch.append(element)
        if len(batch) >= batch_size:
            yield batch
            batch = []
    if len(batch) > 0:
        yield batch

remove_empty_values(dictionary)

Remove empty values from a dictionary.

Parameters:

Name Type Description Default
dictionary dict

The dictionary to remove empty values from.

required

Returns:

Type Description
dict

The dictionary with empty values removed.

Source code in inference_sdk/http/utils/iterables.py
 6
 7
 8
 9
10
11
12
13
14
15
def remove_empty_values(dictionary: dict) -> dict:
    """Remove empty values from a dictionary.

    Args:
        dictionary: The dictionary to remove empty values from.

    Returns:
        The dictionary with empty values removed.
    """
    return {k: v for k, v in dictionary.items() if v is not None}

unwrap_single_element_list(sequence)

Unwrap a single element list.

Parameters:

Name Type Description Default
sequence List[T]

The list to unwrap.

required

Returns:

Type Description
Union[T, List[T]]

The unwrapped list.

Source code in inference_sdk/http/utils/iterables.py
18
19
20
21
22
23
24
25
26
27
28
29
def unwrap_single_element_list(sequence: List[T]) -> Union[T, List[T]]:
    """Unwrap a single element list.

    Args:
        sequence: The list to unwrap.

    Returns:
        The unwrapped list.
    """
    if len(sequence) == 1:
        return sequence[0]
    return sequence