Skip to content

Utils Module

utils

utilities

safe_div(x, y)

This function divides two numbers and avoids division by zero.

Obtained from: https://www.yawintutor.com/zerodivisionerror-division-by-zero/

Parameters:

Name Type Description Default
x int or float

Numerator.

required
y int or float

Denominator. If zero, returns 0.0 to avoid ZeroDivisionError.

required

Returns:

Type Description
float

Result of the division. Returns 0.0 if the denominator is zero.

Examples:

safe_div(10, 2) 5.0

safe_div(10, 0) 0.0

Source code in allinpy/utils/utilities.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def safe_div(x: Union[int, float], y: Union[int, float]) -> float:
    """This function divides two numbers and avoids division by zero.

    Obtained from:
        https://www.yawintutor.com/zerodivisionerror-division-by-zero/

    Parameters
    ----------
    x : int or float
        Numerator.
    y : int or float
        Denominator. If zero, returns 0.0 to avoid ZeroDivisionError.

    Returns
    -------
    float
        Result of the division. Returns 0.0 if the denominator is zero.

    Examples
    --------
    safe_div(10, 2)
    5.0

    safe_div(10, 0)
    0.0
    """

    if y == 0:
        return 0.0
    return x / y

callback(show_ind_prog, pbar)

Update the progress bar if enabled.

Parameters:

Name Type Description Default
show_ind_prog bool

Flag indicating whether the progress bar should be updated.

required
pbar tqdm

Progress-bar-object instance.

required

Returns:

Type Description
None

This function does not return any value.

Source code in allinpy/utils/utilities.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def callback(show_ind_prog: bool, pbar: tqdm) -> None:
    """Update the progress bar if enabled.

    Parameters
    ----------
    show_ind_prog : bool
        Flag indicating whether the progress bar should be updated.
    pbar : tqdm.tqdm
         Progress-bar-object instance.

    Returns
    -------
    None
        This function does not return any value.
    """

    if show_ind_prog:
        pbar.update()