Utility Functions
Overview
File Name: utils.py
This file provides various utility functions and classes for tasks such as file operations, generating random strings, and measuring execution time. It also includes a class for optimized progress updates.
Functions
get_current_user_name
- get_current_user_name() str
Returns the name of the currently logged-in user.
- Returns
The current user’s name.
- Return type
str
make_int128
- make_int128(hi: int, lo: int) int
Makes a 128-bit integer from two 64-bit integers.
- Parameters
hi (int) – High 64 bits.
lo (int) – Low 64 bits.
- Returns
128-bit integer combining hi and lo.
- Return type
int
html_escape
- html_escape(t: str) str
Escapes special HTML characters in the provided text.
- Parameters
t (str) – Text to escape.
- Returns
Escaped text.
- Return type
str
get_available_system_memory
- get_available_system_memory() int
Returns available system memory in bytes.
- Returns
Available system memory in bytes.
- Return type
int
file_exists
- file_exists(path_name: str) bool
Checks if a file specified by path_name exists.
- Parameters
path_name (str) – Path to the file to check.
- Returns
True if the file exists, False otherwise.
- Return type
bool
get_random_word
- get_random_word(length: int) str
Generates a random ASCII lowercase word of ‘length’ characters.
- Parameters
length (int) – Length of the random word.
- Returns
Randomly generated word.
- Return type
str
get_temp_file_path_name
- get_temp_file_path_name(suffix: str = None) str
Generates a temporary file path name.
- Parameters
suffix (str, optional) – Optional suffix for the file name.
- Returns
Temporary file path name.
- Return type
str
get_unique_temp_file_path_name
- get_unique_temp_file_path_name(suffix: str = None) str
Generates a unique temporary file path name that does not exist yet.
- Parameters
suffix (str, optional) – Optional suffix for the file name.
- Returns
Unique temporary file path name.
- Return type
str
is_local_url
- is_local_url(url: str) bool
Determines if the URL points to a local file.
- Parameters
url (str) – URL to check.
- Returns
True if the URL is local, False otherwise.
- Return type
bool
safe_copy_file
- safe_copy_file(src: str, dst: str, follow_symlinks=True) bool
Copies src to dst and returns True on success. Supports URLs.
- Parameters
src (str) – Source file path.
dst (str) – Destination file path.
follow_symlinks (bool, optional) – Whether to follow symlinks. Default is True.
- Returns
True if the file is successfully copied, False otherwise.
- Return type
bool
safe_delete_file
- safe_delete_file(file_name: str) bool
Deletes a file specified by file_name from drive and returns True on success.
- Parameters
file_name (str) – Name of the file to delete.
- Returns
True if the file is successfully deleted, False otherwise.
- Return type
bool
measure_execution_time
- measure_execution_time(func: Callable)
A decorator to measure execution time of a function.
- Parameters
func (Callable) – The function to measure.
- Returns
Wrapped function with execution time measurement.
- Return type
Callable
Classes
OptimizedProgressUpdate
This class helps decide if the user should be notified about a progress update or not. It prevents excessive updating when unnecessary. Progress value is rounded to integers.
Constructor:
- __init__(update_rate=0.1, auto_start=True)
Initializes the OptimizedProgressUpdate instance.
- param update_rate
The rate at which updates are allowed, in seconds. Default is 0.1.
- type update_rate
float, optional
- param auto_start
Whether to start the progress update automatically. Default is True.
- type auto_start
bool, optional
Methods:
- progress_value() int
Gets the percentual progress value (0..100+).
- Returns
The current progress value.
- Return type
int
- start()
Initializes or resets the progress tracking.
- update(progress_value) bool
Determines if an update should be propagated based on the progress value.
- Parameters
progress_value (float) – Current progress value in range [0.0..1.0].
- Returns
True if the update should be propagated, otherwise False.
- Return type
bool