Skip to content

utils#

This module defines utility functions that are used across the package.

clear_screen() #

Clears the terminal console.

Source code in picharsso/utils.py
11
12
13
14
15
16
17
18
def clear_screen():
    """Clears the terminal console."""
    # Windows systems
    if name == "nt":
        _ = system("cls")
    # Unix systems
    else:
        _ = system("clear")

embolden(text) #

Modifies text to appear in a bold typeface, using ANSI escape codes.

Parameters:

Name Type Description Default
text str

The subject text.

required

Returns:

Type Description
str

The text in a bold typeface.

Source code in picharsso/utils.py
21
22
23
24
25
26
27
28
29
30
31
def embolden(text):
    """Modifies text to appear in a bold typeface,
    using [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code).

    Args:
        text (str): The subject text.

    Returns:
        str: The text in a bold typeface.
    """
    return f"{ef.bold}{text}{rs.bold_dim}"

ensure_rgb(image) #

Usually converts any Pillow image to its equivalent in the RGB mode.

Parameters:

Name Type Description Default
image PIL.Image.Image

The subject image.

required

Returns:

Type Description
PIL.Image.Image

The image in the RGB mode.

Source code in picharsso/utils.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def ensure_rgb(image):
    """Usually converts any [Pillow](https://python-pillow.org/)
    `image` to its equivalent in the `RGB` mode.

    Args:
        image (PIL.Image.Image): The subject image.

    Returns:
        PIL.Image.Image: The image in the `RGB` mode.
    """
    # If the image has a color palette,
    # convert to the `RGBA` mode.
    if image.mode == "P":
        image = image.convert("RGBA")

    # If the image is in `RGBA` mode,
    # create a white background.
    if image.mode == "RGBA":
        temp = Image.new("RGB", image.size, (255, 255, 255))
        temp.paste(image, mask=image.split()[3])
        image = temp

    # Convert to `RGB` mode
    return image.convert("RGB")

italicize(text) #

Modifies text to appear in italics, using ANSI escape codes.

Parameters:

Name Type Description Default
text str

The subject text.

required

Returns:

Type Description
str

The text in italics.

Source code in picharsso/utils.py
60
61
62
63
64
65
66
67
68
69
70
def italicize(text):
    """Modifies text to appear in italics,
    using [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code).

    Args:
        text (str): The subject text.

    Returns:
        str: The text in italics.
    """
    return f"{ef.italic}{text}{rs.italic}"

submatrices(matrix, shape) #

Returns a rolling window view of a matrix, without overlapping, given the shape of the window.

Parameters:

Name Type Description Default
matrix numpy.ndarray

The subject matrix.

required
shape Tuple[int, int]

The <height> and <width> of the window.

required

Returns:

Type Description
numpy.ndarray

The rolling window view of the matrix.

Note

This operation doesn't account for the loss of border elements.

Source code in picharsso/utils.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def submatrices(matrix, shape):
    """Returns a rolling window view of a `matrix`, without overlapping,
    given the `shape` of the window.

    Args:
        matrix (numpy.ndarray): The subject matrix.
        shape (Tuple[int, int]): The `<height>` and `<width>` of the window.

    Returns:
        numpy.ndarray: The rolling window view of the matrix.

    Note:
        This operation doesn't account for the loss of border elements.
    """
    # Extract strides and shapes for calculation.
    mat_hs, mat_ws = matrix.strides[:2]
    mat_h, mat_w = matrix.shape[:2]
    ker_h, ker_w = shape

    # View `matrix` according to new strides and shape.
    return np.lib.stride_tricks.as_strided(
        matrix,
        (1 + (mat_h - ker_h) // ker_h, 1 + (mat_w - ker_w) // ker_w, ker_h, ker_w)
        + matrix.shape[2:],
        strides=(ker_h * mat_hs, ker_w * mat_ws, mat_hs, mat_ws) + matrix.strides[2:],
    )

terminal_size() #

Returns the size of the terminal window.

Returns:

Type Description
(Tuple[int, int])

The <height> and <width> of the terminal window in characters.

Note

When used while piping, this function usually returns the default terminal size, (24, 80).

Source code in picharsso/utils.py
101
102
103
104
105
106
107
108
109
110
111
112
def terminal_size():
    """Returns the size of the terminal window.

    Returns:
        (Tuple[int, int]): The `<height>` and `<width>`
                            of the terminal window in characters.

    Note:
        When used while piping,
        this function usually returns the default terminal size, `(24, 80)`.
    """
    return shutil.get_terminal_size()[::-1]