aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/utils/types.py
blob: 07699682156ba7f79cc410db59f69dd58576dfe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Types related utility functions."""
from __future__ import annotations

from typing import Any


def is_list_of(data: Any, cls: type, elem_num: int | None = None) -> bool:
    """Check if data is a list of object of the same class."""
    return (
        isinstance(data, (tuple, list))
        and all(isinstance(item, cls) for item in data)
        and (elem_num is None or len(data) == elem_num)
    )


def is_number(value: str) -> bool:
    """Return true if string contains a number."""
    try:
        float(value)
    except (ValueError, TypeError):
        return False

    return True


def parse_int(value: Any, default: int | None = None) -> int | None:
    """Parse integer value."""
    try:
        return int(value)
    except (TypeError, ValueError):
        return default


def only_one_selected(*options: bool) -> bool:
    """Return true if only one True value found."""
    return sum(options) == 1