aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/utils/types.py
diff options
context:
space:
mode:
authorDiego Russo <diego.russo@arm.com>2022-05-30 13:34:14 +0100
committerDiego Russo <diego.russo@arm.com>2022-05-30 13:34:14 +0100
commit0efca3cadbad5517a59884576ddb90cfe7ac30f8 (patch)
treeabed6cb6fbf3c439fc8d947f505b6a53d5daeb1e /src/mlia/utils/types.py
parent0777092695c143c3a54680b5748287d40c914c35 (diff)
downloadmlia-0efca3cadbad5517a59884576ddb90cfe7ac30f8.tar.gz
Add MLIA codebase0.3.0-rc.1
Add MLIA codebase including sources and tests. Change-Id: Id41707559bd721edd114793618d12ccd188d8dbd
Diffstat (limited to 'src/mlia/utils/types.py')
-rw-r--r--src/mlia/utils/types.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/mlia/utils/types.py b/src/mlia/utils/types.py
new file mode 100644
index 0000000..9b63928
--- /dev/null
+++ b/src/mlia/utils/types.py
@@ -0,0 +1,37 @@
+# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+"""Types related utility functions."""
+from typing import Any
+from typing import Optional
+
+
+def is_list_of(data: Any, cls: type, elem_num: Optional[int] = 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:
+ return False
+
+ return True
+
+
+def parse_int(value: Any, default: Optional[int] = None) -> Optional[int]:
+ """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