os_helper.system_utils module
System Utilities
Cross-platform helpers for operating-system detection, worker-count resolution, executing external commands, and opening files in the system default application.
Author: - Warith HARCHAOUI, https://linkedin.com/in/warith-harchaoui
- os_helper.system_utils.get_nb_workers(workers=-1)[source]
Resolve a worker count, following scikit-learn’s
n_jobsconvention.The default pool size is
os.cpu_count()(or 1 if that returns None), overridable via theNB_WORKERSenvironment variable.- Parameters:
workers (int, optional) –
0: use the full pool size.> 0: use exactly that many workers.< 0: usepool_size + workers + 1(e.g.-1→ all CPUs,-2→ all but one), clamped to at least 1.
- Returns:
The resolved worker count (always >= 1).
- Return type:
Example
>>> get_nb_workers() # -1 → all available CPU cores 4
- os_helper.system_utils.getpid()[source]
Return the current process ID as a string.
- Returns:
str(os.getpid()).- Return type:
- os_helper.system_utils.linux()[source]
Determine if the current operating system is Linux.
- Returns:
True if the operating system is Linux, False otherwise.
- Return type:
- os_helper.system_utils.macos()[source]
Determine if the current operating system is macOS.
- Returns:
True if the operating system is macOS, False otherwise.
- Return type:
- os_helper.system_utils.openfile(filename)[source]
Open a file in the platform’s default application.
Uses
os.startfileon Windows,openon macOS, andxdg-openon Linux. Exceptions from the underlying call are propagated as-is so the caller can react to them.
- os_helper.system_utils.system(cmd, expected_output='', check_exitcode=True, check_empty=False)[source]
Run a shell-style command via
subprocessand capture its output.The command string is parsed with
shlex.split()and executed without spawning an actual shell (shell=False), which avoids shell-injection pitfalls while still accepting a familiar command string.- Parameters:
cmd (str) – Command line to execute (e.g.,
"ffmpeg -i in.mp4 out.mp3").expected_output (str, optional) – If non-empty, a file or directory path expected to be present once the command completes successfully.
check_exitcode (bool, optional) – If True, assert that the process exit code is 0.
check_empty (bool, optional) – If True, also assert that
expected_outputis non-empty (file size > 0 / directory not empty).
- Returns:
{"out": <stdout as str>, "err": <stderr as str>}.- Return type:
- Raises:
AssertionError – If the exit code check or the expected-output check fails.