Utility Functions

Overview

The file utils. contains set of utility functions for handling files, measuring execution time, and optimizing progress updates.

Functions

get_current_user_name

get_current_user_name() str

Returns name of the currently logged-in user.

return

The username of the currently logged-in user as a string.

make_int128

make_int128(hi: int, lo: int) int

Makes a 128-bit number out of two 64-bit numbers.

param hi

The high 64 bits as an integer.

param lo

The low 64 bits as an integer.

return

A 128-bit integer constructed from the provided high and low parts.

html_escape

html_escape(t: str) str

Escapes special HTML characters in the provided string.

param t

The string to escape.

return

The escaped string with special HTML characters replaced.

get_available_system_memory

get_available_system_memory() int:

Returns available system memory in bytes.

return

Available system memory as an integer in bytes.

file_exists

file_exists(path_name: str) bool:

Checks if the file specified by path_name exists.

param path_name

The path to the file as a string.

return

True if the file exists, False otherwise.

get_random_word

get_random_word(length: int) str

Generates a random ASCII lowercase word of the specified length.

param length

The length of the word to generate as an integer.

return

A random word consisting of lowercase letters and digits as a string.

get_temp_file_path_name

get_temp_file_path_name(suffix: str = None) str

Generates a temporary file path name.

param suffix

An optional suffix to append to the file name as a string.

return

A temporary file path name as a string.

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.

param suffix

An optional suffix to append to the file name as a string.

return

A unique temporary file path name as a string.

is_local_url

is_local_url(url: str) bool

Determines if the URL points to a local file.

param url

The URL to check as a string.

return

True if the URL is a local file, False if it is a remote URL.

safe_copy_file

safe_copy_file(src: str, dst: str, follow_symlinks=True) bool

Safely copies a file from src to dst.

param src

The source file path or URL as a string.

param dst

The destination file path as a string.

param follow_symlinks

Whether to follow symlinks or not as a boolean (default is True).

return

True if the file was successfully copied, False otherwise.

safe_delete_file

safe_delete_file(file_name: str) bool

Safely deletes the file specified by file_name.

param file_name

The name of the file to delete as a string.

return

True if the file was successfully deleted, False otherwise.

measure_execution_time (Decorator)

measure_execution_time(func: Callable)

A decorator that measures the execution time of a function.

param func

The function to measure.

return

The decorated function with added execution time measurement.

Class OptimizedProgressUpdate

class OptimizedProgressUpdate

A class that helps to decide if the user should be notified about a progress update.

__init__(update_rate: float = 0.1, auto_start: bool = True)

Initializes a new instance of the OptimizedProgressUpdate class.

Parameters
  • update_rate – The rate at which progress updates are considered as a float (default is 0.1).

  • auto_start – Whether to automatically start tracking progress as a boolean (default is True).

update(progress_value: float) bool

Determines if the progress update should be propagated.

Parameters

progress_value – The current progress value as a float between 0.0 and 1.0.

Returns

True if the update should be propagated, False otherwise.