Skip to content

Commit 22069aa

Browse files
authored
[V2 Logger] utils for import (#1536)
* utils for import * clean up + tests
1 parent 361630b commit 22069aa

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/deepsparse/loggers_v2/utils.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import importlib
16+
import re
17+
from typing import Any, Type
18+
19+
20+
LOGGER_REGISTRY = "src.deepsparse.loggers_v2.registry.__init__"
21+
22+
23+
def import_from_registry(name: str) -> Type[Any]:
24+
"""
25+
Import `name` from the LOGGER_REGISTRY
26+
27+
:param name: name of the function or class name in LOGGER_REGISTRY
28+
:return: Function or class object
29+
"""
30+
module = importlib.import_module(LOGGER_REGISTRY)
31+
try:
32+
return getattr(module, name)
33+
except AttributeError:
34+
raise AttributeError(
35+
f"Cannot import class/func with name '{name}' from {LOGGER_REGISTRY}"
36+
)
37+
38+
39+
def import_from_path(path: str) -> Type[Any]:
40+
"""
41+
Import the module and the name of the function/class separated by :
42+
43+
Examples:
44+
path = "/path/to/file.py:func_name"
45+
path = "/path/to/file:class_name"
46+
47+
:param path: path including the file path and object name
48+
:return Function or class object
49+
50+
"""
51+
path, class_name = path.split(":")
52+
_path = path
53+
54+
path = path.split(".py")[0]
55+
path = re.sub(r"/+", ".", path)
56+
try:
57+
module = importlib.import_module(path)
58+
except ImportError:
59+
raise ImportError(f"Cannot find module with path {_path}")
60+
61+
try:
62+
return getattr(module, class_name)
63+
except AttributeError:
64+
raise AttributeError(f"Cannot find {class_name} in {_path}")
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import pytest
17+
from deepsparse.loggers_v2.utils import (
18+
LOGGER_REGISTRY,
19+
import_from_path,
20+
import_from_registry,
21+
)
22+
23+
24+
@pytest.mark.parametrize(
25+
"name, is_successful",
26+
[
27+
("PythonLogger", True),
28+
("max", True),
29+
("blah", False),
30+
],
31+
)
32+
def test_import_from_registry(name, is_successful):
33+
if is_successful:
34+
assert import_from_registry(name) is not None
35+
else:
36+
with pytest.raises(AttributeError):
37+
import_from_registry(name)
38+
39+
40+
@pytest.mark.parametrize(
41+
"path, is_successful",
42+
[
43+
(f"{LOGGER_REGISTRY}.py:PythonLogger", True),
44+
(f"{LOGGER_REGISTRY}:PythonLogger", True),
45+
("foo/bar:blah", False),
46+
(f"{LOGGER_REGISTRY}:blah", False),
47+
],
48+
)
49+
def test_import_from_path(path, is_successful):
50+
if is_successful:
51+
assert import_from_path(path) is not None
52+
else:
53+
with pytest.raises((AttributeError, ImportError)):
54+
import_from_path(path)

0 commit comments

Comments
 (0)