Skip to content

Imports

feu.imports

Contain utilities to manage optional dependencies.

feu.imports.check_click

check_click() -> None

Check if the click package is installed.

Raises:

Type Description
RuntimeError

if the click package is not installed.

Example
>>> from feu.imports import check_click
>>> check_click()

feu.imports.check_git

check_git() -> None

Check if the git package is installed.

Raises:

Type Description
RuntimeError

if the git package is not installed.

Example
>>> from feu.imports import check_git
>>> check_git()

feu.imports.check_package

check_package(
    package: str, command: str | None = None
) -> None

Check if the given package is installed.

Parameters:

Name Type Description Default
package str

The package name.

required
command str | None

The command to install the package.

None

Raises:

Type Description
RuntimeError

if the package is not installed.

Example
>>> from feu.imports import check_package
>>> check_package("os")

feu.imports.check_requests

check_requests() -> None

Check if the requests package is installed.

Raises:

Type Description
RuntimeError

if the requests package is not installed.

Example
>>> from feu.imports import check_requests
>>> check_requests()

feu.imports.check_urllib3

check_urllib3() -> None

Check if the urllib3 package is installed.

Raises:

Type Description
RuntimeError

if the urllib3 package is not installed.

Example
>>> from feu.imports import check_urllib3
>>> check_urllib3()

feu.imports.click_available

click_available(fn: F) -> F

Implement a decorator to execute a function only if click package is installed.

Parameters:

Name Type Description Default
fn F

The function to execute.

required

Returns:

Type Description
F

A wrapper around fn if click package is installed, otherwise None.

Example
>>> from feu.imports import click_available
>>> @click_available
... def my_function(n: int = 0) -> int:
...     return 42 + n
...
>>> my_function()

feu.imports.decorator_package_available

decorator_package_available(
    fn: F, condition: Callable[[], bool]
) -> F

Implement a decorator to execute a function only if a package is installed.

Parameters:

Name Type Description Default
fn F

The function to execute.

required
condition Callable[[], bool]

The condition to check if a package is installed or not.

required

Returns:

Type Description
F

A wrapper around fn if condition is true, otherwise None.

Example
>>> from functools import partial
>>> from feu.imports import is_git_available
>>> from feu.imports import decorator_package_available
>>> decorator = partial(decorator_package_available, condition=is_git_available)
>>> @decorator
... def my_function(n: int = 0) -> int:
...     return 42 + n
...
>>> my_function(2)
    44

feu.imports.git_available

git_available(fn: F) -> F

Implement a decorator to execute a function only if git package is installed.

Parameters:

Name Type Description Default
fn F

The function to execute.

required

Returns:

Type Description
F

A wrapper around fn if git package is installed, otherwise None.

Example
>>> from feu.imports import git_available
>>> @git_available
... def my_function(n: int = 0) -> int:
...     return 42 + n
...
>>> my_function()

feu.imports.is_click_available cached

is_click_available() -> bool

Indicate if the click package is installed or not.

Returns:

Type Description
bool

True if click is available otherwise False.

Example
>>> from feu.imports import is_click_available
>>> is_click_available()

feu.imports.is_git_available cached

is_git_available() -> bool

Indicate if the git package is installed or not.

Returns:

Type Description
bool

True if git is available otherwise False.

Example
>>> from feu.imports import is_git_available
>>> is_git_available()

feu.imports.is_module_available cached

is_module_available(name: str) -> bool

Indicate if a module is available or not.

Parameters:

Name Type Description Default
name str

The module name to check.

required

Returns:

Type Description
bool

True if the module is available, otherwise False.

Example
>>> from feu.imports import is_module_available
>>> is_module_available("os")
True
>>> is_module_available("os.missing")
False
>>> is_module_available("missing.module")
False

feu.imports.is_package_available cached

is_package_available(name: str) -> bool

Indicate if a package is available or not.

Parameters:

Name Type Description Default
name str

The package name to check.

required

Returns:

Type Description
bool

True if the package is available, otherwise False.

Example
>>> from feu.imports import is_package_available
>>> is_package_available("os")
True
>>> is_package_available("missing_package")
False

feu.imports.is_requests_available cached

is_requests_available() -> bool

Indicate if the requests package is installed or not.

Returns:

Type Description
bool

True if requests is available otherwise False.

Example
>>> from feu.imports import is_requests_available
>>> is_requests_available()

feu.imports.is_urllib3_available cached

is_urllib3_available() -> bool

Indicate if the urllib3 package is installed or not.

Returns:

Type Description
bool

True if urllib3 is available otherwise False.

Example
>>> from feu.imports import is_urllib3_available
>>> is_urllib3_available()

feu.imports.raise_click_missing_error

raise_click_missing_error() -> NoReturn

Raise a RuntimeError to indicate the click package is missing.

feu.imports.raise_git_missing_error

raise_git_missing_error() -> NoReturn

Raise a RuntimeError to indicate the git package is missing.

feu.imports.raise_package_missing_error

raise_package_missing_error(
    package_name: str, install_cmd: str
) -> NoReturn

Raise a RuntimeError for a missing package.

Parameters:

Name Type Description Default
package_name str

The name of the missing package.

required
install_cmd str

The pip install command for the package.

required

Raises:

Type Description
RuntimeError

Always raised to indicate the package is missing.

feu.imports.raise_requests_missing_error

raise_requests_missing_error() -> NoReturn

Raise a RuntimeError to indicate the requests package is missing.

feu.imports.raise_urllib3_missing_error

raise_urllib3_missing_error() -> NoReturn

Raise a RuntimeError to indicate the urllib3 package is missing.

feu.imports.requests_available

requests_available(fn: F) -> F

Implement a decorator to execute a function only if requests package is installed.

Parameters:

Name Type Description Default
fn F

The function to execute.

required

Returns:

Type Description
F

A wrapper around fn if requests package is installed, otherwise None.

Example
>>> from feu.imports import requests_available
>>> @requests_available
... def my_function(n: int = 0) -> int:
...     return 42 + n
...
>>> my_function()

feu.imports.urllib3_available

urllib3_available(fn: F) -> F

Implement a decorator to execute a function only if urllib3 package is installed.

Parameters:

Name Type Description Default
fn F

The function to execute.

required

Returns:

Type Description
F

A wrapper around fn if urllib3 package is installed, otherwise None.

Example
>>> from feu.imports import urllib3_available
>>> @urllib3_available
... def my_function(n: int = 0) -> int:
...     return 42 + n
...
>>> my_function()