stouputils 1.14.3__py3-none-any.whl → 1.15.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stouputils/data_science/config/get.py +51 -51
- stouputils/data_science/data_processing/image/__init__.py +66 -66
- stouputils/data_science/data_processing/image/auto_contrast.py +79 -79
- stouputils/data_science/data_processing/image/axis_flip.py +58 -58
- stouputils/data_science/data_processing/image/bias_field_correction.py +74 -74
- stouputils/data_science/data_processing/image/binary_threshold.py +73 -73
- stouputils/data_science/data_processing/image/blur.py +59 -59
- stouputils/data_science/data_processing/image/brightness.py +54 -54
- stouputils/data_science/data_processing/image/canny.py +110 -110
- stouputils/data_science/data_processing/image/clahe.py +92 -92
- stouputils/data_science/data_processing/image/common.py +30 -30
- stouputils/data_science/data_processing/image/contrast.py +53 -53
- stouputils/data_science/data_processing/image/curvature_flow_filter.py +74 -74
- stouputils/data_science/data_processing/image/denoise.py +378 -378
- stouputils/data_science/data_processing/image/histogram_equalization.py +123 -123
- stouputils/data_science/data_processing/image/invert.py +64 -64
- stouputils/data_science/data_processing/image/laplacian.py +60 -60
- stouputils/data_science/data_processing/image/median_blur.py +52 -52
- stouputils/data_science/data_processing/image/noise.py +59 -59
- stouputils/data_science/data_processing/image/normalize.py +65 -65
- stouputils/data_science/data_processing/image/random_erase.py +66 -66
- stouputils/data_science/data_processing/image/resize.py +69 -69
- stouputils/data_science/data_processing/image/rotation.py +80 -80
- stouputils/data_science/data_processing/image/salt_pepper.py +68 -68
- stouputils/data_science/data_processing/image/sharpening.py +55 -55
- stouputils/data_science/data_processing/image/shearing.py +64 -64
- stouputils/data_science/data_processing/image/threshold.py +64 -64
- stouputils/data_science/data_processing/image/translation.py +71 -71
- stouputils/data_science/data_processing/image/zoom.py +83 -83
- stouputils/data_science/data_processing/image_augmentation.py +118 -118
- stouputils/data_science/data_processing/image_preprocess.py +183 -183
- stouputils/data_science/data_processing/prosthesis_detection.py +359 -359
- stouputils/data_science/data_processing/technique.py +481 -481
- stouputils/data_science/dataset/__init__.py +45 -45
- stouputils/data_science/dataset/dataset.py +292 -292
- stouputils/data_science/dataset/dataset_loader.py +135 -135
- stouputils/data_science/dataset/grouping_strategy.py +296 -296
- stouputils/data_science/dataset/image_loader.py +100 -100
- stouputils/data_science/dataset/xy_tuple.py +696 -696
- stouputils/data_science/metric_dictionnary.py +106 -106
- stouputils/data_science/mlflow_utils.py +206 -206
- stouputils/data_science/models/abstract_model.py +149 -149
- stouputils/data_science/models/all.py +85 -85
- stouputils/data_science/models/keras/all.py +38 -38
- stouputils/data_science/models/keras/convnext.py +62 -62
- stouputils/data_science/models/keras/densenet.py +50 -50
- stouputils/data_science/models/keras/efficientnet.py +60 -60
- stouputils/data_science/models/keras/mobilenet.py +56 -56
- stouputils/data_science/models/keras/resnet.py +52 -52
- stouputils/data_science/models/keras/squeezenet.py +233 -233
- stouputils/data_science/models/keras/vgg.py +42 -42
- stouputils/data_science/models/keras/xception.py +38 -38
- stouputils/data_science/models/keras_utils/callbacks/__init__.py +20 -20
- stouputils/data_science/models/keras_utils/callbacks/colored_progress_bar.py +219 -219
- stouputils/data_science/models/keras_utils/callbacks/learning_rate_finder.py +148 -148
- stouputils/data_science/models/keras_utils/callbacks/model_checkpoint_v2.py +31 -31
- stouputils/data_science/models/keras_utils/callbacks/progressive_unfreezing.py +249 -249
- stouputils/data_science/models/keras_utils/callbacks/warmup_scheduler.py +66 -66
- stouputils/data_science/models/keras_utils/losses/__init__.py +12 -12
- stouputils/data_science/models/keras_utils/losses/next_generation_loss.py +56 -56
- stouputils/data_science/models/keras_utils/visualizations.py +416 -416
- stouputils/data_science/models/sandbox.py +116 -116
- stouputils/data_science/range_tuple.py +234 -234
- stouputils/data_science/utils.py +285 -285
- stouputils/decorators.py +53 -39
- stouputils/decorators.pyi +2 -2
- stouputils/installer/__init__.py +18 -18
- stouputils/installer/linux.py +144 -144
- stouputils/installer/main.py +223 -223
- stouputils/installer/windows.py +136 -136
- stouputils/io.py +16 -9
- stouputils/print.py +229 -2
- stouputils/print.pyi +90 -1
- stouputils/py.typed +1 -1
- {stouputils-1.14.3.dist-info → stouputils-1.15.0.dist-info}/METADATA +1 -1
- {stouputils-1.14.3.dist-info → stouputils-1.15.0.dist-info}/RECORD +78 -78
- {stouputils-1.14.3.dist-info → stouputils-1.15.0.dist-info}/WHEEL +1 -1
- {stouputils-1.14.3.dist-info → stouputils-1.15.0.dist-info}/entry_points.txt +0 -0
stouputils/installer/windows.py
CHANGED
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
""" Installer module for Windows specific functions.
|
|
2
|
-
|
|
3
|
-
Provides Windows specific implementations for checking administrator privileges,
|
|
4
|
-
determining appropriate installation paths (global/local), and modifying
|
|
5
|
-
the user's PATH environment variable.
|
|
6
|
-
"""
|
|
7
|
-
# Imports
|
|
8
|
-
import os
|
|
9
|
-
|
|
10
|
-
from ..decorators import LogLevels, handle_error
|
|
11
|
-
from ..io import clean_path
|
|
12
|
-
from ..print import debug, info, warning
|
|
13
|
-
from .common import ask_install_type, prompt_for_path
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
# Functions
|
|
17
|
-
@handle_error(message="Failed to add to PATH (Windows)", error_log=LogLevels.WARNING_TRACEBACK)
|
|
18
|
-
def add_to_path_windows(install_path: str) -> bool | None:
|
|
19
|
-
""" Add install_path to the User PATH environment variable on Windows.
|
|
20
|
-
|
|
21
|
-
Args:
|
|
22
|
-
install_path (str): The path to add to the User PATH environment variable.
|
|
23
|
-
|
|
24
|
-
Returns:
|
|
25
|
-
bool | None: True if the path was added to the User PATH environment variable, None otherwise.
|
|
26
|
-
"""
|
|
27
|
-
# Convert install_path to a Windows path if it's not already
|
|
28
|
-
install_path = install_path.replace("/", "\\")
|
|
29
|
-
os.makedirs(install_path, exist_ok=True)
|
|
30
|
-
|
|
31
|
-
# Get current user PATH
|
|
32
|
-
import winreg
|
|
33
|
-
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment", 0, winreg.KEY_READ | winreg.KEY_WRITE) as key:
|
|
34
|
-
|
|
35
|
-
# Get the number of values in the registry key
|
|
36
|
-
num_values = winreg.QueryInfoKey(key)[1]
|
|
37
|
-
|
|
38
|
-
# Find the index of the 'Path' value
|
|
39
|
-
path_index = -1
|
|
40
|
-
for i in range(num_values):
|
|
41
|
-
if winreg.EnumValue(key, i)[0] == 'Path':
|
|
42
|
-
path_index = i
|
|
43
|
-
break
|
|
44
|
-
|
|
45
|
-
# Get the current path value
|
|
46
|
-
current_path: str = winreg.EnumValue(key, path_index)[1]
|
|
47
|
-
|
|
48
|
-
# Check if path is already present
|
|
49
|
-
if install_path not in current_path.split(';'):
|
|
50
|
-
new_path: str = f"{current_path};{install_path}"
|
|
51
|
-
winreg.SetValueEx(key, "Path", 0, winreg.REG_EXPAND_SZ, new_path)
|
|
52
|
-
debug(f"Added '{install_path}' to user PATH. Please restart your terminal for changes to take effect.")
|
|
53
|
-
else:
|
|
54
|
-
debug(f"'{install_path}' is already in user PATH.")
|
|
55
|
-
return True
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
def check_admin_windows() -> bool:
|
|
59
|
-
""" Check if the script is running with administrator privileges on Windows. """
|
|
60
|
-
try:
|
|
61
|
-
import ctypes
|
|
62
|
-
return ctypes.windll.shell32.IsUserAnAdmin() != 0
|
|
63
|
-
except Exception:
|
|
64
|
-
return False
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
@handle_error(message="Failed to get installation path (Windows)", error_log=LogLevels.ERROR_TRACEBACK)
|
|
68
|
-
def get_install_path_windows(
|
|
69
|
-
program_name: str,
|
|
70
|
-
ask_global: int = 0,
|
|
71
|
-
add_path: bool = True,
|
|
72
|
-
append_to_path: str = "",
|
|
73
|
-
default_global: str = os.environ.get("ProgramFiles", "C:\\Program Files")
|
|
74
|
-
) -> str:
|
|
75
|
-
""" Get the installation path for the program
|
|
76
|
-
|
|
77
|
-
Args:
|
|
78
|
-
program_name (str): The name of the program to install.
|
|
79
|
-
ask_global (int): 0 = ask for anything, 1 = install globally, 2 = install locally
|
|
80
|
-
add_path (bool): Whether to add the program to the PATH environment variable. (Only if installed globally)
|
|
81
|
-
append_to_path (str): String to append to the installation path when adding to PATH.
|
|
82
|
-
(ex: "bin" if executables are in the bin folder)
|
|
83
|
-
default_global (str): The default global installation path.
|
|
84
|
-
(Default is "C:\\Program Files" which is the most common location for executables on Windows)
|
|
85
|
-
|
|
86
|
-
Returns:
|
|
87
|
-
str: The installation path.
|
|
88
|
-
"""
|
|
89
|
-
# Default path is located in the current working directory
|
|
90
|
-
default_local_path: str = clean_path(os.path.join(os.getcwd(), program_name))
|
|
91
|
-
|
|
92
|
-
# Define default global path (used in prompt even if not chosen initially)
|
|
93
|
-
default_global_path: str = clean_path(os.path.join(default_global, program_name))
|
|
94
|
-
|
|
95
|
-
# Ask user for installation type (global/local)
|
|
96
|
-
install_type: str = ask_install_type(ask_global, default_local_path, default_global_path)
|
|
97
|
-
|
|
98
|
-
# If the user wants to install globally,
|
|
99
|
-
if install_type == 'g':
|
|
100
|
-
|
|
101
|
-
# Check if the user has admin privileges,
|
|
102
|
-
if not check_admin_windows():
|
|
103
|
-
|
|
104
|
-
# If the user doesn't have admin privileges, fallback to local
|
|
105
|
-
warning(
|
|
106
|
-
f"Global installation requires administrator privileges. Please re-run as administrator.\n"
|
|
107
|
-
f"Install locally instead to '{default_local_path}'? (Y/n): "
|
|
108
|
-
)
|
|
109
|
-
if input().lower() == 'n':
|
|
110
|
-
info("Installation cancelled.")
|
|
111
|
-
return ""
|
|
112
|
-
else:
|
|
113
|
-
# Fallback to local path if user agrees
|
|
114
|
-
return prompt_for_path(
|
|
115
|
-
f"Falling back to local installation path: {default_local_path}.",
|
|
116
|
-
default_local_path
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
# If the user has admin privileges,
|
|
120
|
-
else:
|
|
121
|
-
# Ask it user wants to override the default global install path
|
|
122
|
-
install_path: str = prompt_for_path(
|
|
123
|
-
f"Default global installation path is {default_global_path}.",
|
|
124
|
-
default_global_path
|
|
125
|
-
)
|
|
126
|
-
if add_path:
|
|
127
|
-
add_to_path_windows(os.path.join(install_path, append_to_path))
|
|
128
|
-
return install_path
|
|
129
|
-
|
|
130
|
-
# Local install
|
|
131
|
-
else: # install_type == 'l'
|
|
132
|
-
return prompt_for_path(
|
|
133
|
-
f"Default local installation path is {default_local_path}.",
|
|
134
|
-
default_local_path
|
|
135
|
-
)
|
|
136
|
-
|
|
1
|
+
""" Installer module for Windows specific functions.
|
|
2
|
+
|
|
3
|
+
Provides Windows specific implementations for checking administrator privileges,
|
|
4
|
+
determining appropriate installation paths (global/local), and modifying
|
|
5
|
+
the user's PATH environment variable.
|
|
6
|
+
"""
|
|
7
|
+
# Imports
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
from ..decorators import LogLevels, handle_error
|
|
11
|
+
from ..io import clean_path
|
|
12
|
+
from ..print import debug, info, warning
|
|
13
|
+
from .common import ask_install_type, prompt_for_path
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Functions
|
|
17
|
+
@handle_error(message="Failed to add to PATH (Windows)", error_log=LogLevels.WARNING_TRACEBACK)
|
|
18
|
+
def add_to_path_windows(install_path: str) -> bool | None:
|
|
19
|
+
""" Add install_path to the User PATH environment variable on Windows.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
install_path (str): The path to add to the User PATH environment variable.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
bool | None: True if the path was added to the User PATH environment variable, None otherwise.
|
|
26
|
+
"""
|
|
27
|
+
# Convert install_path to a Windows path if it's not already
|
|
28
|
+
install_path = install_path.replace("/", "\\")
|
|
29
|
+
os.makedirs(install_path, exist_ok=True)
|
|
30
|
+
|
|
31
|
+
# Get current user PATH
|
|
32
|
+
import winreg
|
|
33
|
+
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment", 0, winreg.KEY_READ | winreg.KEY_WRITE) as key:
|
|
34
|
+
|
|
35
|
+
# Get the number of values in the registry key
|
|
36
|
+
num_values = winreg.QueryInfoKey(key)[1]
|
|
37
|
+
|
|
38
|
+
# Find the index of the 'Path' value
|
|
39
|
+
path_index = -1
|
|
40
|
+
for i in range(num_values):
|
|
41
|
+
if winreg.EnumValue(key, i)[0] == 'Path':
|
|
42
|
+
path_index = i
|
|
43
|
+
break
|
|
44
|
+
|
|
45
|
+
# Get the current path value
|
|
46
|
+
current_path: str = winreg.EnumValue(key, path_index)[1]
|
|
47
|
+
|
|
48
|
+
# Check if path is already present
|
|
49
|
+
if install_path not in current_path.split(';'):
|
|
50
|
+
new_path: str = f"{current_path};{install_path}"
|
|
51
|
+
winreg.SetValueEx(key, "Path", 0, winreg.REG_EXPAND_SZ, new_path)
|
|
52
|
+
debug(f"Added '{install_path}' to user PATH. Please restart your terminal for changes to take effect.")
|
|
53
|
+
else:
|
|
54
|
+
debug(f"'{install_path}' is already in user PATH.")
|
|
55
|
+
return True
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def check_admin_windows() -> bool:
|
|
59
|
+
""" Check if the script is running with administrator privileges on Windows. """
|
|
60
|
+
try:
|
|
61
|
+
import ctypes
|
|
62
|
+
return ctypes.windll.shell32.IsUserAnAdmin() != 0
|
|
63
|
+
except Exception:
|
|
64
|
+
return False
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@handle_error(message="Failed to get installation path (Windows)", error_log=LogLevels.ERROR_TRACEBACK)
|
|
68
|
+
def get_install_path_windows(
|
|
69
|
+
program_name: str,
|
|
70
|
+
ask_global: int = 0,
|
|
71
|
+
add_path: bool = True,
|
|
72
|
+
append_to_path: str = "",
|
|
73
|
+
default_global: str = os.environ.get("ProgramFiles", "C:\\Program Files")
|
|
74
|
+
) -> str:
|
|
75
|
+
""" Get the installation path for the program
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
program_name (str): The name of the program to install.
|
|
79
|
+
ask_global (int): 0 = ask for anything, 1 = install globally, 2 = install locally
|
|
80
|
+
add_path (bool): Whether to add the program to the PATH environment variable. (Only if installed globally)
|
|
81
|
+
append_to_path (str): String to append to the installation path when adding to PATH.
|
|
82
|
+
(ex: "bin" if executables are in the bin folder)
|
|
83
|
+
default_global (str): The default global installation path.
|
|
84
|
+
(Default is "C:\\Program Files" which is the most common location for executables on Windows)
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
str: The installation path.
|
|
88
|
+
"""
|
|
89
|
+
# Default path is located in the current working directory
|
|
90
|
+
default_local_path: str = clean_path(os.path.join(os.getcwd(), program_name))
|
|
91
|
+
|
|
92
|
+
# Define default global path (used in prompt even if not chosen initially)
|
|
93
|
+
default_global_path: str = clean_path(os.path.join(default_global, program_name))
|
|
94
|
+
|
|
95
|
+
# Ask user for installation type (global/local)
|
|
96
|
+
install_type: str = ask_install_type(ask_global, default_local_path, default_global_path)
|
|
97
|
+
|
|
98
|
+
# If the user wants to install globally,
|
|
99
|
+
if install_type == 'g':
|
|
100
|
+
|
|
101
|
+
# Check if the user has admin privileges,
|
|
102
|
+
if not check_admin_windows():
|
|
103
|
+
|
|
104
|
+
# If the user doesn't have admin privileges, fallback to local
|
|
105
|
+
warning(
|
|
106
|
+
f"Global installation requires administrator privileges. Please re-run as administrator.\n"
|
|
107
|
+
f"Install locally instead to '{default_local_path}'? (Y/n): "
|
|
108
|
+
)
|
|
109
|
+
if input().lower() == 'n':
|
|
110
|
+
info("Installation cancelled.")
|
|
111
|
+
return ""
|
|
112
|
+
else:
|
|
113
|
+
# Fallback to local path if user agrees
|
|
114
|
+
return prompt_for_path(
|
|
115
|
+
f"Falling back to local installation path: {default_local_path}.",
|
|
116
|
+
default_local_path
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
# If the user has admin privileges,
|
|
120
|
+
else:
|
|
121
|
+
# Ask it user wants to override the default global install path
|
|
122
|
+
install_path: str = prompt_for_path(
|
|
123
|
+
f"Default global installation path is {default_global_path}.",
|
|
124
|
+
default_global_path
|
|
125
|
+
)
|
|
126
|
+
if add_path:
|
|
127
|
+
add_to_path_windows(os.path.join(install_path, append_to_path))
|
|
128
|
+
return install_path
|
|
129
|
+
|
|
130
|
+
# Local install
|
|
131
|
+
else: # install_type == 'l'
|
|
132
|
+
return prompt_for_path(
|
|
133
|
+
f"Default local installation path is {default_local_path}.",
|
|
134
|
+
default_local_path
|
|
135
|
+
)
|
|
136
|
+
|
stouputils/io.py
CHANGED
|
@@ -188,16 +188,23 @@ def csv_dump(
|
|
|
188
188
|
done: bool = False
|
|
189
189
|
|
|
190
190
|
# Handle Polars DataFrame
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
copy_kwargs.setdefault("include_header", has_header)
|
|
197
|
-
data.write_csv(output, *args, **copy_kwargs)
|
|
198
|
-
done = True
|
|
199
|
-
except Exception:
|
|
191
|
+
import sys
|
|
192
|
+
if sys.version_info >= (3, 14) and not sys._is_gil_enabled(): # pyright: ignore[reportPrivateUsage]
|
|
193
|
+
# Skip Polars on free-threaded Python 3.14 due to segfault
|
|
194
|
+
# TODO: Remove this check when Polars is fixed
|
|
195
|
+
# See https://github.com/pola-rs/polars/issues/21889 and https://github.com/durandtibo/coola/issues/1066
|
|
200
196
|
pass
|
|
197
|
+
else:
|
|
198
|
+
try:
|
|
199
|
+
import polars as pl # type: ignore
|
|
200
|
+
if isinstance(data, pl.DataFrame):
|
|
201
|
+
copy_kwargs = kwargs.copy()
|
|
202
|
+
copy_kwargs.setdefault("separator", delimiter)
|
|
203
|
+
copy_kwargs.setdefault("include_header", has_header)
|
|
204
|
+
data.write_csv(output, *args, **copy_kwargs)
|
|
205
|
+
done = True
|
|
206
|
+
except Exception:
|
|
207
|
+
pass
|
|
201
208
|
|
|
202
209
|
# Handle pandas DataFrame
|
|
203
210
|
if not done:
|
stouputils/print.py
CHANGED
|
@@ -4,6 +4,10 @@ This module provides utility functions for printing messages with different leve
|
|
|
4
4
|
If a message is printed multiple times, it will be displayed as "(xN) message"
|
|
5
5
|
where N is the number of times the message has been printed.
|
|
6
6
|
|
|
7
|
+
The module also includes a `colored()` function that formats text with Python 3.14 style coloring
|
|
8
|
+
for file paths, line numbers, function names (in magenta), and exception names (in bold magenta).
|
|
9
|
+
All functions have their colored counterparts with a 'c' suffix (e.g., `infoc()`, `debugc()`, etc.)
|
|
10
|
+
|
|
7
11
|
.. image:: https://raw.githubusercontent.com/Stoupy51/stouputils/refs/heads/main/assets/print_module.gif
|
|
8
12
|
:alt: stouputils print examples
|
|
9
13
|
"""
|
|
@@ -24,6 +28,7 @@ BLUE: str = "\033[94m"
|
|
|
24
28
|
MAGENTA: str = "\033[95m"
|
|
25
29
|
CYAN: str = "\033[96m"
|
|
26
30
|
LINE_UP: str = "\033[1A"
|
|
31
|
+
BOLD: str = "\033[1m"
|
|
27
32
|
|
|
28
33
|
# Constants
|
|
29
34
|
BAR_FORMAT: str = "{l_bar}{bar}" + MAGENTA + "| {n_fmt}/{total_fmt} [{rate_fmt}{postfix}, {elapsed}<{remaining}]" + RESET
|
|
@@ -84,12 +89,210 @@ def colored_for_loop[T](
|
|
|
84
89
|
from tqdm.auto import tqdm
|
|
85
90
|
yield from tqdm(iterable, desc=desc, bar_format=bar_format, ascii=ascii, **kwargs)
|
|
86
91
|
|
|
92
|
+
def format_colored(*values: Any) -> str:
|
|
93
|
+
""" Format text with Python 3.14 style colored formatting.
|
|
94
|
+
|
|
95
|
+
Dynamically colors text by analyzing each word:
|
|
96
|
+
- File paths in magenta
|
|
97
|
+
- Numbers in magenta
|
|
98
|
+
- Function names (built-in and callable objects) in magenta
|
|
99
|
+
- Exception names in bold magenta
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
values (Any): Values to format (like the print function)
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
str: The formatted text with ANSI color codes
|
|
106
|
+
|
|
107
|
+
Examples:
|
|
108
|
+
>>> # Test function names with parentheses
|
|
109
|
+
>>> result = format_colored("Call print() with 42 items")
|
|
110
|
+
>>> result.count(MAGENTA) == 2 # print and 42
|
|
111
|
+
True
|
|
112
|
+
|
|
113
|
+
>>> # Test function names without parentheses
|
|
114
|
+
>>> result = format_colored("Use len and sum functions")
|
|
115
|
+
>>> result.count(MAGENTA) == 2 # len and sum
|
|
116
|
+
True
|
|
117
|
+
|
|
118
|
+
>>> # Test exceptions (bold magenta)
|
|
119
|
+
>>> result = format_colored("Got ValueError when parsing")
|
|
120
|
+
>>> result.count(MAGENTA) == 1 and result.count(BOLD) == 1 # ValueError in bold magenta
|
|
121
|
+
True
|
|
122
|
+
|
|
123
|
+
>>> # Test file paths
|
|
124
|
+
>>> result = format_colored("Processing ./data.csv file")
|
|
125
|
+
>>> result.count(MAGENTA) == 1 # ./data.csv
|
|
126
|
+
True
|
|
127
|
+
|
|
128
|
+
>>> # Test file paths with quotes
|
|
129
|
+
>>> result = format_colored('File "/path/to/script.py" line 42')
|
|
130
|
+
>>> result.count(MAGENTA) == 2 # /path/to/script.py and 42
|
|
131
|
+
True
|
|
132
|
+
|
|
133
|
+
>>> # Test numbers
|
|
134
|
+
>>> result = format_colored("Found 100 items and 3.14 value")
|
|
135
|
+
>>> result.count(MAGENTA) == 2 # 100 and 3.14
|
|
136
|
+
True
|
|
137
|
+
|
|
138
|
+
>>> # Test mixed content
|
|
139
|
+
>>> result = format_colored("Call sum() got IndexError at line 256 in utils.py")
|
|
140
|
+
>>> result.count(MAGENTA) == 3 # sum, IndexError (bold), and 256
|
|
141
|
+
True
|
|
142
|
+
>>> result.count(BOLD) == 1 # IndexError is bold
|
|
143
|
+
True
|
|
144
|
+
|
|
145
|
+
>>> # Test plain text (no coloring)
|
|
146
|
+
>>> result = format_colored("This is plain text")
|
|
147
|
+
>>> result.count(MAGENTA) == 0 and result == "This is plain text"
|
|
148
|
+
True
|
|
149
|
+
"""
|
|
150
|
+
import builtins
|
|
151
|
+
import re
|
|
152
|
+
|
|
153
|
+
# Dynamically retrieve all Python exception names and function names
|
|
154
|
+
EXCEPTION_NAMES: set[str] = {
|
|
155
|
+
name for name in dir(builtins)
|
|
156
|
+
if isinstance(getattr(builtins, name, None), type)
|
|
157
|
+
and issubclass(getattr(builtins, name), BaseException)
|
|
158
|
+
}
|
|
159
|
+
BUILTIN_FUNCTIONS: set[str] = {
|
|
160
|
+
name for name in dir(builtins)
|
|
161
|
+
if callable(getattr(builtins, name, None))
|
|
162
|
+
and not (isinstance(getattr(builtins, name, None), type)
|
|
163
|
+
and issubclass(getattr(builtins, name), BaseException))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
def is_filepath(word: str) -> bool:
|
|
167
|
+
""" Check if a word looks like a file path """
|
|
168
|
+
# Remove quotes if present
|
|
169
|
+
clean_word: str = word.strip('"\'')
|
|
170
|
+
|
|
171
|
+
# Check for path separators and file extensions
|
|
172
|
+
if ('/' in clean_word or '\\' in clean_word) and '.' in clean_word:
|
|
173
|
+
# Check if it has a reasonable extension (2-4 chars)
|
|
174
|
+
parts = clean_word.split('.')
|
|
175
|
+
if len(parts) >= 2 and 2 <= len(parts[-1]) <= 4:
|
|
176
|
+
return True
|
|
177
|
+
|
|
178
|
+
# Check for Windows absolute paths (C:\, D:\, etc.)
|
|
179
|
+
if len(clean_word) > 3 and clean_word[1:3] == ':\\':
|
|
180
|
+
return True
|
|
181
|
+
|
|
182
|
+
# Check for Unix absolute paths starting with /
|
|
183
|
+
if clean_word.startswith('/') and '.' in clean_word:
|
|
184
|
+
return True
|
|
185
|
+
|
|
186
|
+
return False
|
|
187
|
+
|
|
188
|
+
def is_number(word: str) -> bool:
|
|
189
|
+
try:
|
|
190
|
+
float(word.strip('.,;:!?'))
|
|
191
|
+
return True
|
|
192
|
+
except ValueError:
|
|
193
|
+
return False
|
|
194
|
+
|
|
195
|
+
def is_function_name(word: str) -> tuple[bool, str]:
|
|
196
|
+
# Check if word ends with () or just (, or it's a known built-in function
|
|
197
|
+
clean_word: str = word.rstrip('.,;:!?')
|
|
198
|
+
if clean_word.endswith(('()','(')) or clean_word in BUILTIN_FUNCTIONS:
|
|
199
|
+
return (True, clean_word)
|
|
200
|
+
return (False, "")
|
|
201
|
+
|
|
202
|
+
def is_exception(word: str) -> bool:
|
|
203
|
+
""" Check if a word is a known exception name """
|
|
204
|
+
return word.strip('.,;:!?') in EXCEPTION_NAMES
|
|
205
|
+
|
|
206
|
+
# Convert all values to strings and join them and split into words while preserving separators
|
|
207
|
+
text: str = " ".join(str(v) for v in values)
|
|
208
|
+
words: list[str] = re.split(r'(\s+)', text)
|
|
209
|
+
|
|
210
|
+
# Process each word
|
|
211
|
+
colored_words: list[str] = []
|
|
212
|
+
i: int = 0
|
|
213
|
+
while i < len(words):
|
|
214
|
+
word = words[i]
|
|
215
|
+
|
|
216
|
+
# Skip whitespace
|
|
217
|
+
if word.isspace():
|
|
218
|
+
colored_words.append(word)
|
|
219
|
+
i += 1
|
|
220
|
+
continue
|
|
221
|
+
|
|
222
|
+
# Try to identify and color the word
|
|
223
|
+
colored: bool = False
|
|
224
|
+
if is_filepath(word):
|
|
225
|
+
colored_words.append(f"{MAGENTA}{word}{RESET}")
|
|
226
|
+
colored = True
|
|
227
|
+
elif is_exception(word):
|
|
228
|
+
colored_words.append(f"{BOLD}{MAGENTA}{word}{RESET}")
|
|
229
|
+
colored = True
|
|
230
|
+
elif is_number(word):
|
|
231
|
+
# Preserve punctuation
|
|
232
|
+
clean_word = word.strip('.,;:!?')
|
|
233
|
+
prefix = word[:len(word) - len(word.lstrip('.,;:!?'))]
|
|
234
|
+
suffix = word[len(clean_word) + len(prefix):]
|
|
235
|
+
colored_words.append(f"{prefix}{MAGENTA}{clean_word}{RESET}{suffix}")
|
|
236
|
+
colored = True
|
|
237
|
+
elif is_function_name(word)[0]:
|
|
238
|
+
func_name = is_function_name(word)[1]
|
|
239
|
+
# Find where the function name ends in the original word
|
|
240
|
+
func_start = word.find(func_name)
|
|
241
|
+
if func_start != -1:
|
|
242
|
+
prefix = word[:func_start]
|
|
243
|
+
func_end = func_start + len(func_name)
|
|
244
|
+
suffix = word[func_end:]
|
|
245
|
+
colored_words.append(f"{prefix}{MAGENTA}{func_name}{RESET}{suffix}")
|
|
246
|
+
else:
|
|
247
|
+
# Fallback if we can't find it (shouldn't happen)
|
|
248
|
+
colored_words.append(f"{MAGENTA}{word}{RESET}")
|
|
249
|
+
colored = True
|
|
250
|
+
|
|
251
|
+
# If nothing matched, keep the word as is
|
|
252
|
+
if not colored:
|
|
253
|
+
colored_words.append(word)
|
|
254
|
+
i += 1
|
|
255
|
+
|
|
256
|
+
# Join and return
|
|
257
|
+
return "".join(colored_words)
|
|
258
|
+
|
|
259
|
+
def colored(
|
|
260
|
+
*values: Any,
|
|
261
|
+
file: TextIO | None = None,
|
|
262
|
+
**print_kwargs: Any,
|
|
263
|
+
) -> None:
|
|
264
|
+
""" Print with Python 3.14 style colored formatting.
|
|
265
|
+
|
|
266
|
+
Dynamically colors text by analyzing each word:
|
|
267
|
+
- File paths in magenta
|
|
268
|
+
- Numbers in magenta
|
|
269
|
+
- Function names (built-in and callable objects) in magenta
|
|
270
|
+
- Exception names in bold magenta
|
|
271
|
+
|
|
272
|
+
Args:
|
|
273
|
+
values (Any): Values to print (like the print function)
|
|
274
|
+
file (TextIO): File to write the message to (default: sys.stdout)
|
|
275
|
+
print_kwargs (dict): Keyword arguments to pass to the print function
|
|
276
|
+
|
|
277
|
+
Examples:
|
|
278
|
+
>>> colored("File '/path/to/file.py', line 42, in function_name") # doctest: +SKIP
|
|
279
|
+
>>> colored("KeyboardInterrupt") # doctest: +SKIP
|
|
280
|
+
>>> colored("Processing data.csv with 100 items") # doctest: +SKIP
|
|
281
|
+
>>> colored("Using print and len functions") # doctest: +SKIP
|
|
282
|
+
"""
|
|
283
|
+
if file is None:
|
|
284
|
+
file = sys.stdout
|
|
285
|
+
|
|
286
|
+
result: str = format_colored(*values)
|
|
287
|
+
print(result, file=file, **print_kwargs)
|
|
288
|
+
|
|
87
289
|
def info(
|
|
88
290
|
*values: Any,
|
|
89
291
|
color: str = GREEN,
|
|
90
292
|
text: str = "INFO ",
|
|
91
293
|
prefix: str = "",
|
|
92
294
|
file: TextIO | list[TextIO] | None = None,
|
|
295
|
+
use_colored: bool = False,
|
|
93
296
|
**print_kwargs: Any,
|
|
94
297
|
) -> None:
|
|
95
298
|
""" Print an information message looking like "[INFO HH:MM:SS] message" in green by default.
|
|
@@ -100,6 +303,7 @@ def info(
|
|
|
100
303
|
text (str): Text of the message (default: "INFO ")
|
|
101
304
|
prefix (str): Prefix to add to the values
|
|
102
305
|
file (TextIO|list[TextIO]): File(s) to write the message to (default: sys.stdout)
|
|
306
|
+
use_colored (bool): Whether to use the colored() function to format the message
|
|
103
307
|
print_kwargs (dict): Keyword arguments to pass to the print function
|
|
104
308
|
"""
|
|
105
309
|
# Use stdout if no file is specified
|
|
@@ -109,7 +313,7 @@ def info(
|
|
|
109
313
|
# If file is a list, recursively call info() for each file
|
|
110
314
|
if isinstance(file, list):
|
|
111
315
|
for f in file:
|
|
112
|
-
info(*values, color=color, text=text, prefix=prefix, file=f, **print_kwargs)
|
|
316
|
+
info(*values, color=color, text=text, prefix=prefix, file=f, use_colored=use_colored, **print_kwargs)
|
|
113
317
|
else:
|
|
114
318
|
# Build the message with prefix, color, text and timestamp
|
|
115
319
|
message: str = f"{prefix}{color}[{text} {current_time()}]"
|
|
@@ -119,7 +323,10 @@ def info(
|
|
|
119
323
|
message = f"{LINE_UP}{message} (x{nb_values})"
|
|
120
324
|
|
|
121
325
|
# Print the message with the values and reset color
|
|
122
|
-
|
|
326
|
+
if use_colored:
|
|
327
|
+
print(message, format_colored(*values).replace(RESET, RESET+color), RESET, file=file, **print_kwargs)
|
|
328
|
+
else:
|
|
329
|
+
print(message, *values, RESET, file=file, **print_kwargs)
|
|
123
330
|
|
|
124
331
|
def debug(*values: Any, **print_kwargs: Any) -> None:
|
|
125
332
|
""" Print a debug message looking like "[DEBUG HH:MM:SS] message" in cyan by default. """
|
|
@@ -444,6 +651,26 @@ def current_time() -> str:
|
|
|
444
651
|
else:
|
|
445
652
|
return time.strftime("%H:%M:%S")
|
|
446
653
|
|
|
654
|
+
# Convenience colored functions
|
|
655
|
+
def infoc(*args: Any, **kwargs: Any) -> None:
|
|
656
|
+
return info(*args, use_colored=True, **kwargs)
|
|
657
|
+
def debugc(*args: Any, **kwargs: Any) -> None:
|
|
658
|
+
return debug(*args, use_colored=True, **kwargs)
|
|
659
|
+
def alt_debugc(*args: Any, **kwargs: Any) -> None:
|
|
660
|
+
return alt_debug(*args, use_colored=True, **kwargs)
|
|
661
|
+
def warningc(*args: Any, **kwargs: Any) -> None:
|
|
662
|
+
return warning(*args, use_colored=True, **kwargs)
|
|
663
|
+
def errorc(*args: Any, **kwargs: Any) -> None:
|
|
664
|
+
return error(*args, use_colored=True, **kwargs)
|
|
665
|
+
def progressc(*args: Any, **kwargs: Any) -> None:
|
|
666
|
+
return progress(*args, use_colored=True, **kwargs)
|
|
667
|
+
def suggestionc(*args: Any, **kwargs: Any) -> None:
|
|
668
|
+
return suggestion(*args, use_colored=True, **kwargs)
|
|
669
|
+
def whatisitc(*args: Any, **kwargs: Any) -> None:
|
|
670
|
+
return whatisit(*args, use_colored=True, **kwargs)
|
|
671
|
+
def breakpointc(*args: Any, **kwargs: Any) -> None:
|
|
672
|
+
return breakpoint(*args, use_colored=True, **kwargs)
|
|
673
|
+
|
|
447
674
|
|
|
448
675
|
# Test the print functions
|
|
449
676
|
if __name__ == "__main__":
|