-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from valence-labs/remove_pyquante
Soft remove PyQuante2
- Loading branch information
Showing
8 changed files
with
180 additions
and
29 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
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,103 @@ | ||
import importlib | ||
from functools import wraps | ||
from typing import Any, Callable, TypeVar | ||
|
||
F = TypeVar("F", bound=Callable[..., Any]) | ||
|
||
|
||
class MissingOptionalDependencyError(BaseException): | ||
""" | ||
An exception raised when an optional dependency is required | ||
but cannot be found. | ||
Attributes | ||
---------- | ||
library_name | ||
The name of the missing library. | ||
""" | ||
|
||
def __init__(self, library_name: str): | ||
""" | ||
Parameters | ||
---------- | ||
library_name | ||
The name of the missing library. | ||
license_issue | ||
Whether the library was importable but was unusable due | ||
to a missing license. | ||
""" | ||
|
||
message = f"The required {library_name} module could not be imported." | ||
|
||
super(MissingOptionalDependencyError, self).__init__(message) | ||
|
||
self.library_name = library_name | ||
|
||
|
||
def has_package(package_name: str) -> bool: | ||
""" | ||
Helper function to generically check if a Python package is installed. | ||
Intended to be used to check for optional dependencies. | ||
Parameters | ||
---------- | ||
package_name : str | ||
The name of the Python package to check the availability of | ||
Returns | ||
------- | ||
package_available : bool | ||
Boolean indicator if the package is available or not | ||
Examples | ||
-------- | ||
>>> has_numpy = has_package('numpy') | ||
>>> has_numpy | ||
True | ||
>>> has_foo = has_package('other_non_installed_package') | ||
>>> has_foo | ||
False | ||
""" | ||
try: | ||
importlib.import_module(package_name) | ||
except ModuleNotFoundError: | ||
return False | ||
return True | ||
|
||
|
||
def requires_package(package_name: str) -> Callable[..., Any]: | ||
""" | ||
Helper function to denote that a funciton requires some optional | ||
dependency. A function decorated with this decorator will raise | ||
`MissingOptionalDependencyError` if the package is not found by | ||
`importlib.import_module()`. | ||
Parameters | ||
---------- | ||
package_name : str | ||
The name of the module to be imported. | ||
Raises | ||
------ | ||
MissingOptionalDependencyError | ||
""" | ||
|
||
def inner_decorator(function: F) -> F: | ||
@wraps(function) | ||
def wrapper(*args, **kwargs): | ||
import importlib | ||
|
||
try: | ||
importlib.import_module(package_name) | ||
except ImportError: | ||
raise MissingOptionalDependencyError(library_name=package_name) | ||
except Exception as e: | ||
raise e | ||
|
||
return function(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return inner_decorator |
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