Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving class loading efficiency and considering dynamic type list #919

Merged
merged 17 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion forte/data/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ def _is_subclass(
if cls_qualified_name in type_name_parent_class:
return True
else:
entry_class = get_class(type_name)
entry_class = get_class(type_name, cached_lookup=False)
if issubclass(entry_class, cls):
type_name_parent_class.add(cls_qualified_name)
return True
Expand Down
38 changes: 30 additions & 8 deletions forte/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""
import sys
import difflib
from functools import wraps
from functools import wraps, lru_cache
from inspect import getfullargspec
from pydoc import locate
from typing import Dict, List, Optional, get_type_hints, Tuple
Expand Down Expand Up @@ -78,27 +78,49 @@ def get_class_name(o, lower: bool = False) -> str:
return o.__name__


def get_class(full_class_name: str, module_paths: Optional[List[str]] = None):
r"""Returns the class based on class name.
@lru_cache()
def cached_locate(name_to_locate_class):
r"""Wrapped version of locate for loading class based on
``name_to_locate_class``, cached by lru_cache.
Args:
name_to_locate_class (str): Name or full path to the class.

Returns:
The target class.

"""
return locate(name_to_locate_class)


def get_class(
full_class_name: str,
module_paths: Optional[List[str]] = None,
cached_lookup: Optional[bool] = True,
):
r"""Returns the class based on class name, with cached lookup option.

Args:
full_class_name (str): Name or full path to the class.
module_paths (list): Paths to candidate modules to search for the
class. This is used if the class cannot be located solely based on
``class_name``. The first module in the list that contains the class
is used.
cached_lookup (bool): Flag to use "cached_locate" for class loading

Returns:
The target class.

Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
"""
class_ = locate(full_class_name)
if cached_lookup:
class_ = cached_locate(full_class_name)
else:
class_ = locate(full_class_name)
if (class_ is None) and (module_paths is not None):
for module_path in module_paths:
class_ = locate(".".join([module_path, full_class_name]))
if cached_lookup:
class_ = cached_locate(".".join([module_path, full_class_name]))
else:
class_ = locate(".".join([module_path, full_class_name]))
if class_ is not None:
break

Expand Down