-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: module lookup for building trackers
- Loading branch information
Showing
5 changed files
with
96 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import importlib | ||
from types import ModuleType | ||
from typing import Callable, Optional, Union | ||
|
||
|
||
def load_module(path: str) -> Union[ModuleType, Optional[Callable[..., object]]]: | ||
""" | ||
Loads and returns the module/module attr represented by the ``path``: ``full.module.path:optional_attr`` | ||
:: | ||
1. ``load_module("this.is.a_module:fn")`` -> equivalent to ``this.is.a_module.fn`` | ||
1. ``load_module("this.is.a_module")`` -> equivalent to ``this.is.a_module`` | ||
""" | ||
parts = path.split(":", 2) | ||
module_path, method = parts[0], parts[1] if len(parts) > 1 else None | ||
module = None | ||
i, n = -1, len(module_path) | ||
try: | ||
while i < n: | ||
i = module_path.find(".", i + 1) | ||
i = i if i >= 0 else n | ||
module = importlib.import_module(module_path[:i]) | ||
return getattr(module, method) if method else module | ||
except Exception: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
|
||
from torchx.util.modules import load_module | ||
|
||
|
||
class ModulesTest(unittest.TestCase): | ||
def test_load_module(self) -> None: | ||
result = load_module("os.path") | ||
import os | ||
|
||
self.assertEqual(result, os.path) | ||
|
||
def test_load_module_method(self) -> None: | ||
result = load_module("os.path:join") | ||
import os | ||
|
||
self.assertEqual(result, os.path.join) |