Skip to content

Commit

Permalink
new: _should_backend_run, _can_backend_run
Browse files Browse the repository at this point in the history
  • Loading branch information
aMahanna committed Oct 17, 2024
1 parent 18884c7 commit 4cd83f8
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions nx_arangodb/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ def _auto_func(func_name: str, /, *args: Any, **kwargs: Any) -> Any:
backend_priority.append("cugraph")

for backend in backend_priority:
if not _should_backend_run(backend, func_name, *args, **kwargs):
if not _should_backend_run(backend, dfunc, *args, **kwargs):
continue

if not _can_backend_run(backend, dfunc, *args, **kwargs):
continue

try:
Expand All @@ -87,38 +90,30 @@ def _auto_func(func_name: str, /, *args: Any, **kwargs: Any) -> Any:
return _run_with_backend(default_backend, dfunc, args, kwargs)


def _should_backend_run(
backend_name: str, func_name: str, *args: Any, **kwargs: Any
) -> bool:
"""
Determine if a specific backend should be used for a given algorithm.
Copied from networkx-3.4.1/networkx/utils/backends.py#L1514-L1535
to patch the implementation for backwards compatibility, as the signature of
this function in NetworkX 3.3 is different from the one in NetworkX 3.4.
def _should_backend_run(backend: str, dfunc: Any, *args: Any, **kwargs: Any) -> bool:
"""Wrapper around NetworkX's should_backend_run function, because
the signature is different for NetworkX <=3.3 and 3.4:
:param backend_name: The name of the backend to check.
:type backend_name: str
:param func_name: The name of the algorithm/function to be run.
:type func_name: str
:param args: Variable length argument list for the function.
:type args: Any
:param kwargs: Arbitrary keyword arguments for the function.
:type kwargs: Any
:returns: Whether the backend should be used.
:rtype: bool
- https://github.com/networkx/networkx/blob/networkx-3.3/networkx/utils/backends.py#L821 # noqa: E501
- https://github.com/networkx/networkx/blob/networkx-3.4.1/networkx/utils/backends.py#L1514 # noqa: E501
"""
if backend_name == "networkx":
return True
try:
return dfunc.__wrapped__._should_backend_run(backend, *args, **kwargs)
except TypeError:
return dfunc.__wrapped__._should_backend_run(backend, args, kwargs)

backend = _load_backend(backend_name)
should_run = backend.should_run(func_name, args, kwargs)
if isinstance(should_run, str) or not should_run:
reason = f", because: {should_run}" if isinstance(should_run, str) else ""
logger.debug(f"Backend '{backend_name}' not used for '{func_name}' ({reason})")
return False

return True
def _can_backend_run(backend: str, dfunc: Any, *args: Any, **kwargs: Any) -> bool:
"""Wrapper around NetworkX's _can_backend_run function, because
the signature is different for NetworkX <=3.3 and 3.4:
- https://github.com/networkx/networkx/blob/networkx-3.3/networkx/utils/backends.py#L810 # noqa: E501
- https://github.com/networkx/networkx/blob/networkx-3.4.1/networkx/utils/backends.py#L1489 # noqa: E501
"""
try:
return dfunc.__wrapped__._can_backend_run(backend, *args, **kwargs)
except TypeError:
return dfunc.__wrapped__._can_backend_run(backend, args, kwargs)


def _run_with_backend(
Expand Down

0 comments on commit 4cd83f8

Please sign in to comment.