Skip to content

Commit

Permalink
feat: speed up requires_package using caching (#3705)
Browse files Browse the repository at this point in the history
* test: refactor check_stds and post_mortem_checks

* test: backing off algorithm

* chore: adding changelog file 3703.added.md [dependabot-skip]

* fix: codacity warnings

* feat: using get_value to obtain the n elements

* revert: revert "feat: using get_value to obtain the n elements"
Performance is not as go

This reverts commit 877f803.

* feat: using get_value to obtain the n elements

* revert: revert "feat: using get_value to obtain the n elements"
Performance is not as go

This reverts commit 877f803.

* feat: using mapdl.exit when raising final error.

* test: fix test by avoiding killing mapdl.

* fix: test

* fix: test

* feat: adding warnings when restarting MAPDL during testing

* fix: test

* feat: caching requires_package

* test: adding more tests

* chore: adding changelog file 3705.added.md [dependabot-skip]

* chore: adding changelog file 3705.added.md [dependabot-skip]

* fix: warnings import

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Jan 27, 2025
1 parent 6060cab commit 9d63421
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3705.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: speed up `requires_package` using caching
18 changes: 14 additions & 4 deletions src/ansys/mapdl/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

"""Module for miscellaneous functions and methods"""
from enum import Enum
from functools import wraps
from functools import cache, wraps
import importlib
import inspect
import os
Expand Down Expand Up @@ -415,6 +415,16 @@ def write_array(filename: Union[str, bytes], array: np.ndarray) -> None:
np.savetxt(filename, array, fmt="%20.12f")


@cache
def is_package_installed_cached(package_name):
try:
importlib.import_module(package_name)
return True

except ModuleNotFoundError:
return False


def requires_package(package_name: str, softerror: bool = False) -> Callable:
"""
Decorator check whether a package is installed or not.
Expand All @@ -430,11 +440,11 @@ def requires_package(package_name: str, softerror: bool = False) -> Callable:
def decorator(function):
@wraps(function)
def wrapper(self, *args, **kwargs):
try:
importlib.import_module(package_name)

if is_package_installed_cached(package_name):
return function(self, *args, **kwargs)

except ModuleNotFoundError:
else:
msg = (
f"To use the method '{function.__name__}', "
f"the package '{package_name}' is required.\n"
Expand Down
1 change: 1 addition & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import subprocess
import time
from typing import Dict, List
from warnings import warn

import psutil

Expand Down
11 changes: 11 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2836,3 +2836,14 @@ def test_none_on_selecting(mapdl, cleared, func):

assert len(selfunc("all")) > 0
assert len(selfunc(None)) == 0


def test_requires_package_speed():
from ansys.mapdl.core.misc import requires_package

@requires_package("pyvista")
def my_func(i):
return i + 1

for i in range(1_000_000):
my_func(i)

0 comments on commit 9d63421

Please sign in to comment.