diff options
author | Benjamin Klimczak <benjamin.klimczak@arm.com> | 2022-12-14 11:20:11 +0000 |
---|---|---|
committer | Benjamin Klimczak <benjamin.klimczak@arm.com> | 2023-01-04 10:11:33 +0000 |
commit | dcd0bd31985c27e1d07333351b26cf8ad12ad1fd (patch) | |
tree | a3388ff5f91e7cdc7ec41271a1a76cdbfae38ece /src/mlia/utils/registry.py | |
parent | 4b4cf29cb1e7d917ae001e258ff01f7846c34778 (diff) | |
download | mlia-dcd0bd31985c27e1d07333351b26cf8ad12ad1fd.tar.gz |
MLIA-589 Create an API to get target information
Change-Id: Ieeaa9188ea1e29e2ccaad7475d457bce71e3140d
Diffstat (limited to 'src/mlia/utils/registry.py')
-rw-r--r-- | src/mlia/utils/registry.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/mlia/utils/registry.py b/src/mlia/utils/registry.py new file mode 100644 index 0000000..9b25a81 --- /dev/null +++ b/src/mlia/utils/registry.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-License-Identifier: Apache-2.0 +"""Generic registry class.""" +from __future__ import annotations + +from typing import Generic +from typing import TypeVar + +T = TypeVar("T") + + +class Registry(Generic[T]): + """Generic registry for name-config pairs.""" + + def __init__(self) -> None: + """Create an empty registry.""" + self.items: dict[str, T] = {} + + def __str__(self) -> str: + """List all registered items.""" + return "\n".join( + f"- {name}: {item}" + for name, item in sorted(self.items.items(), key=lambda v: v[0]) + ) + + def register(self, name: str, item: T) -> bool: + """Register an item: returns `False` if already registered.""" + if name in self.items: + return False # already registered + self.items[name] = item + return True |