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

feat: speed up requires_package using caching #3705

Merged
merged 25 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3d40750
test: refactor check_stds and post_mortem_checks
germa89 Jan 24, 2025
7e3a301
test: backing off algorithm
germa89 Jan 24, 2025
8bfb76a
chore: adding changelog file 3703.added.md [dependabot-skip]
pyansys-ci-bot Jan 24, 2025
6f7585a
fix: codacity warnings
germa89 Jan 24, 2025
0687f6e
Merge remote-tracking branch 'origin/main' into test/testing-performance
germa89 Jan 24, 2025
877f803
feat: using get_value to obtain the n elements
germa89 Jan 24, 2025
41ad078
revert: revert "feat: using get_value to obtain the n elements"
germa89 Jan 24, 2025
e046259
feat: using get_value to obtain the n elements
germa89 Jan 24, 2025
1556747
revert: revert "feat: using get_value to obtain the n elements"
germa89 Jan 24, 2025
52d9e92
feat: using mapdl.exit when raising final error.
germa89 Jan 24, 2025
2b29e52
test: fix test by avoiding killing mapdl.
germa89 Jan 24, 2025
991b31a
chore: merge remote-tracking branch 'origin/main' into test/testing-p…
germa89 Jan 24, 2025
c006f61
chore: merge remote-tracking branch 'origin/main' into test/testing-p…
germa89 Jan 24, 2025
d5d2267
fix: test
germa89 Jan 24, 2025
5c081cb
fix: test
germa89 Jan 27, 2025
0e26425
feat: adding warnings when restarting MAPDL during testing
germa89 Jan 27, 2025
8238ea8
fix: test
germa89 Jan 27, 2025
88c55fd
feat: caching requires_package
germa89 Jan 27, 2025
e9ff8f6
test: adding more tests
germa89 Jan 27, 2025
a0d0fa4
chore: merge remote-tracking branch 'origin/main' into test/testing-p…
germa89 Jan 27, 2025
fe526ce
chore: adding changelog file 3705.added.md [dependabot-skip]
pyansys-ci-bot Jan 27, 2025
12c9429
Merge branch 'main' into test/testing-performance
germa89 Jan 27, 2025
a4aa6a3
chore: adding changelog file 3705.added.md [dependabot-skip]
pyansys-ci-bot Jan 27, 2025
5593e6b
fix: warnings import
germa89 Jan 27, 2025
6d13fe7
Merge branch 'test/testing-performance' of https://github.com/ansys/p…
germa89 Jan 27, 2025
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
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)
Loading