Skip to content

Utils

coola.utils

Contain the utility functions.

coola.utils.check_numpy

check_numpy() -> None

Check if the numpy package is installed.

Raises:

Type Description
RuntimeError

if the numpy package is not installed.

Example usage:

>>> from coola.utils.imports import check_numpy
>>> check_numpy()

coola.utils.check_pandas

check_pandas() -> None

Check if the pandas package is installed.

Raises:

Type Description
RuntimeError

if the pandas package is not installed.

Example usage:

>>> from coola.utils.imports import check_pandas
>>> check_pandas()

coola.utils.check_polars

check_polars() -> None

Check if the polars package is installed.

Raises:

Type Description
RuntimeError

if the polars package is not installed.

Example usage:

>>> from coola.utils.imports import check_polars
>>> check_polars()

coola.utils.check_torch

check_torch() -> None

Check if the torch package is installed.

Raises:

Type Description
RuntimeError

if the torch package is not installed.

Example usage:

>>> from coola.utils.imports import check_torch
>>> check_torch()

coola.utils.check_xarray

check_xarray() -> None

Check if the xarray package is installed.

Raises:

Type Description
RuntimeError

if the xarray package is not installed.

Example usage:

>>> from coola.utils.imports import check_xarray
>>> check_xarray()

coola.utils.is_jax_available cached

is_jax_available() -> bool

Indicate if the jax package is installed or not.

Returns:

Type Description
bool

True if jax is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_jax_available
>>> is_jax_available()

coola.utils.is_numpy_available cached

is_numpy_available() -> bool

Indicate if the numpy package is installed or not.

Returns:

Type Description
bool

True if numpy is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_numpy_available
>>> is_numpy_available()

coola.utils.is_pandas_available cached

is_pandas_available() -> bool

Indicate if the pandas package is installed or not.

Returns:

Type Description
bool

True if pandas is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_pandas_available
>>> is_pandas_available()

coola.utils.is_polars_available cached

is_polars_available() -> bool

Indicate if the polars package is installed or not.

Returns:

Type Description
bool

True if polars is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_polars_available
>>> is_polars_available()

coola.utils.is_torch_available cached

is_torch_available() -> bool

Indicate if the torch package is installed or not.

Returns:

Type Description
bool

True if torch is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_torch_available
>>> is_torch_available()

coola.utils.is_xarray_available cached

is_xarray_available() -> bool

Indicate if the xarray package is installed or not.

Returns:

Type Description
bool

True if xarray is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_xarray_available
>>> is_xarray_available()

coola.utils.module_available cached

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 usage:

>>> from coola.utils.imports import module_available
>>> module_available("os")
True
>>> module_available("os.missing")
False
>>> module_available("missing.module")
False

coola.utils.package_available cached

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 usage:

>>> from coola.utils.imports import package_available
>>> package_available("os")
True
>>> package_available("missing_package")
False

coola.utils.repr_indent

repr_indent(original: Any, num_spaces: int = 2) -> str

Add indentations if the original string is a multi-lines string.

Parameters:

Name Type Description Default
original Any

The original string. If the inputis not a string, it will be converted to a string with the function repr.

required
num_spaces int

The number of spaces used for the indentation.

2

Returns:

Type Description
str

The indented string.

Raises:

Type Description
RuntimeError

if num_spaces is not a positive integer.

Example usage:

>>> from coola.utils.format import repr_indent
>>> print(repr_indent("string1\nstring2\n  string3", 4))
string1
string2
  string3

coola.utils.repr_mapping

repr_mapping(
    mapping: Mapping,
    sorted_keys: bool = False,
    num_spaces: int = 2,
) -> str

Compute a string representation of a mapping.

This function was designed for flat dictionary. If you have a nested dictionary, you may consider other functions. Note that this function works for nested dict but the output may not be nice.

Parameters:

Name Type Description Default
mapping Mapping

The mapping.

required
sorted_keys bool

If True, the keys in the mapping are sorted before to compute the string representation.

False
num_spaces int

The number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the mapping.

Example usage:

>>> from coola.utils.format import repr_mapping
>>> print(repr_mapping({"key1": "abc", "key2": "something\nelse"}))
(key1): abc
(key2): something
  else

coola.utils.repr_sequence

repr_sequence(
    sequence: Sequence, num_spaces: int = 2
) -> str

Compute a string representation of a sequence.

Parameters:

Name Type Description Default
sequence Sequence

The sequence.

required
num_spaces int

The number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the sequence.

Example usage:

>>> from coola.utils.format import repr_indent
>>> print(repr_sequence(["abc", "something\nelse"]))
(0): abc
(1): something
  else

coola.utils.str_indent

str_indent(original: Any, num_spaces: int = 2) -> str

Add indentations if the original string is a multi-lines string.

Parameters:

Name Type Description Default
original Any

The original string. If the inputis not a string, it will be converted to a string with the function str.

required
num_spaces int

The number of spaces used for the indentation.

2

Returns:

Type Description
str

The indented string.

Raises:

Type Description
RuntimeError

if num_spaces is not a positive integer.

Example usage:

>>> from coola.utils.format import str_indent
>>> print(str_indent("string1\nstring2\n  string3", 4))
string1
string2
  string3

coola.utils.str_mapping

str_mapping(
    mapping: Mapping,
    sorted_keys: bool = False,
    num_spaces: int = 2,
) -> str

Compute a string representation of a mapping.

This function was designed for flat dictionary. If you have a nested dictionary, you may consider other functions. Note that this function works for nested dict but the output may not be nice.

Parameters:

Name Type Description Default
mapping Mapping

The mapping.

required
sorted_keys bool

If True, the keys in the mapping are sorted before to compute the string representation.

False
num_spaces int

The number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the mapping.

Example usage:

>>> from coola.utils.format import str_mapping
>>> print(str_mapping({"key1": "abc", "key2": "something\nelse"}))
(key1): abc
(key2): something
  else

coola.utils.str_sequence

str_sequence(
    sequence: Sequence, num_spaces: int = 2
) -> str

Compute a string representation of a sequence.

Parameters:

Name Type Description Default
sequence Sequence

The sequence.

required
num_spaces int

The number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the sequence.

Example usage:

>>> from coola.utils.format import str_sequence
>>> print(str_sequence(["abc", "something\nelse"]))
(0): abc
(1): something
  else