Construct Checks for Optional Dependencies
Project description
optional_dependencies
Construct Checks for Optional Dependencies
Installation
pip install optional_dependencies
Documentation
High-Level API
This package allows for easy construction of checks for optional dependencies.
As every Python project can have its own unique set of optional dependencies,
optional_dependencies
provides the enum.Enum
base class
OptionalDependencyEnum
for enumerating the optional dependencies.
A constructed OptionalDependencyEnum
enum.Enum
has members that
are the package names with values that are either a
packaging.Version
or a NOT_INSTALLED
sentinel value.
As an example:
# Note that `auto` is a convenience re-export of enum.auto
from optional_dependencies import OptionalDependencyEnum, auto
class OptDeps(OptionalDependencyEnum):
PACKAGING = auto()
THIS_IS_NOT_INSTALLED = auto()
OptDeps.PACKAGING
# <OptDeps.PACKAGING: <Version('...')>>
OptDeps.PACKAGING.is_installed
# True
OptDeps.PACKAGING.version
# <Version('...')>
OptDeps.THIS_IS_NOT_INSTALLED
# <OptDeps.THIS_IS_NOT_A_PACKAGE: <InstalledState.NOT_INSTALLED: False>>
enum.auto
on a OptionalDependencyEnum
passes version parsing to
packaging.utils.canonicalize_name
then
importlib.metadata.version
then packaging.version.parse
. If the package cannot be found
then it is considered InstalledState.NOT_INSTALLED
InstalledState.NOT_INSTALLED
is an enum.Enum
member that has a
truthy value of False
. This can be useful for boolean checks, as
packaging.Version
always has a truthy value of True
.
if not OptDeps.THIS_IS_NOT_INSTALLED:
print("NOT_INSTALLED has a truthy value of False")
# NOT_INSTALLED has a truthy value of False
if OptDeps.PACKAGING: # truthy value of `True`
print(OptDeps.PACKAGING)
# OptDeps.PACKAGING
Low-Level API
Sometimes the high-level API is insufficient to determine whether an optional dependency is present. For example, this can sometimes happen with compiled packages, where the package appears to be installed, but something is wrong. In these cases you can customize the enum members using the low-level API.
The low-level functions are:
-
optional_dependencies.utils.is_installed(pkg_name: str, /) -> bool
: a regularized form ofimportlib.util.find_spec
. -
optional_dependencies.utils.get_version(pkg_name: str) -> Version | Literal[InstalledState.NOT_INSTALLED]
: for getting thepackaging.Version
of a package if it is installed, or returningNOT_INSTALLED
otherwise. -
optional_dependencies.utils.chain_checks
: for chaining checks together and ensuring the returned value is still apackaging.Version
orNOT_INSTALLED
.
As a pseudo-code example of a package with c-compiled code that :
from optional_dependencies.utils import is_installed, get_version, chain_checks
# A subpackage needs to be checked.
chain_checks(get_version("package"), is_installed("package.subpackage"))
# <Version('...')>
chain_checks(get_version("package"), is_installed("package.subpackage"))
# <InstalledState.NOT_INSTALLED: False>
Citation
If you found this library to be useful and want to support the development and maintenance of lower-level code libraries for the scientific community, please consider citing this work.
Development
We welcome contributions!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file optional_dependencies-0.2.1.tar.gz
.
File metadata
- Download URL: optional_dependencies-0.2.1.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cd02ec9d352d2ca9740c5475e54b969bb4e088dee1ffb1c2b2499a99e947136 |
|
MD5 | 11b1b8963ad6786320f33d3de83f64c8 |
|
BLAKE2b-256 | 70c08916ff756365744875fbe2871a537f055795cebfa4f0cd0e9db284cd440c |
File details
Details for the file optional_dependencies-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: optional_dependencies-0.2.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 121df8daab6b9e757043e7d6d17bfbd7523c356ecfc45bb1e1a749f0de3f12d0 |
|
MD5 | 5e889b893e09f57e5b3e5564f291b014 |
|
BLAKE2b-256 | b7ac91b29e15b8c3089eb208bf03965cb42fc9e8d2bb9a59b2abf804de15236b |