utils#
This module provides utility functions for file operations, data conversion, and progress tracking.
Functions#
File Operations#
- omni.physxclashdetectioncore.utils.file_exists(path_name: str) bool#
Checks if file specified by path_name exists and returns True on success, otherwise returns False.
- Parameters:
path_name (str) – Path to the file to check.
- Returns:
True if the file exists, False otherwise.
- Return type:
bool
- omni.physxclashdetectioncore.utils.safe_copy_file(
- src: str,
- dst: str,
- follow_symlinks: bool = True,
Copies src to dst and returns True on success, otherwise returns False. Also supports URLs.
- Parameters:
src (str) – Source file path.
dst (str) – Destination file path.
follow_symlinks (bool) – Whether to follow symlinks. Defaults to True.
- Returns:
True if the file is successfully copied, False otherwise.
- Return type:
bool
- omni.physxclashdetectioncore.utils.safe_delete_file(file_name: str) bool#
Deletes file specified by file_name from drive and returns True on success, otherwise returns False.
- Parameters:
file_name (str) – Name of the file to delete.
- Returns:
True if the file is successfully deleted, False otherwise.
- Return type:
bool
- omni.physxclashdetectioncore.utils.get_temp_file_path_name(suffix: str = '') str#
Generates temp file path name.
- Parameters:
suffix (str) – Optional suffix for the file name. Defaults to “”.
- Returns:
Temporary file path name.
- Return type:
str
- omni.physxclashdetectioncore.utils.get_unique_temp_file_path_name(suffix: str = '') str#
Generates unique temp file path name (makes sure it does not exist yet).
- Parameters:
suffix (str) – Optional suffix for the file name. Defaults to “”.
- Returns:
Unique temporary file path name.
- Return type:
str
- omni.physxclashdetectioncore.utils.is_local_url(url: str) bool#
Returns True if the url points to a local file. Returns False in case of remote URL.
- Parameters:
url (str) – URL to check.
- Returns:
True if the URL is local, False otherwise.
- Return type:
bool
Data Conversion#
- omni.physxclashdetectioncore.utils.make_int128(hi: int, lo: int) int#
Makes 128bit number out of two 64bit numbers.
- Parameters:
hi (int) – High 64 bits.
lo (int) – Low 64 bits.
- Returns:
128-bit integer combining hi and lo.
- Return type:
int
- omni.physxclashdetectioncore.utils.html_escape(t: str) str#
Escapes special HTML characters (provided in t).
- Parameters:
t (str) – Text to escape.
- Returns:
Escaped text.
- Return type:
str
- omni.physxclashdetectioncore.utils.to_json_str_safe(obj: Any, **kwargs) str#
Safely converts a Python object into a JSON string.
This function attempts to serialize a Python object into a JSON-formatted string. If serialization fails, it logs an error message and returns an empty string.
- Parameters:
obj (Any) – The Python object to be serialized into a JSON string.
kwargs – Additional keyword arguments passed to json.dumps(), such as formatting options.
- Returns:
A JSON-formatted string representation of the object. Returns an empty string if serialization fails.
- Return type:
str
- omni.physxclashdetectioncore.utils.from_json_str_safe(json_str: str) Any#
Safely converts a JSON string into a Python object.
This function attempts to parse a JSON-encoded string into a corresponding Python object. If the input string is empty or an error occurs during parsing, it logs an error message and returns an empty list.
- Parameters:
json_str (str) – The JSON string to be converted.
- Returns:
A Python object resulting from the parsed JSON string. Returns an empty list if the input is empty or if parsing fails.
- Return type:
Any
- omni.physxclashdetectioncore.utils.clamp_value(value: Any, min_value: Any, max_value: Any) Any#
Clamps value within the min and max range.
- Parameters:
value – The value to be clamped.
min_value – The minimum allowable value.
max_value – The maximum allowable value.
- Returns:
The clamped value within the specified range.
- omni.physxclashdetectioncore.utils.obj_to_dict(
- obj: Any,
- attr_convert_fn: Callable[[str, Any], Any] | None = None,
Converts an object instance to a dictionary.
Converts object attributes to a dictionary, handling special cases for Enum and datetime types. Enum values are converted to their string names and datetime objects are converted to ISO format strings.
- Parameters:
obj (Any) – The object to convert to a dictionary.
attr_convert_fn (Optional[Callable[[str, Any], Any]]) – Optional function to convert attribute values. Takes attribute name and value as arguments and returns the converted value.
- Returns:
Dictionary containing the object’s attributes and their converted values.
- Return type:
Dict[str, Any]
- omni.physxclashdetectioncore.utils.dict_to_obj(
- obj: Any,
- data: Dict[str, Any],
- attr_convert_fn: Callable[[str, Any], Any] | None = None,
Converts a dictionary to an object instance.
- Parameters:
obj (Any) – The target object to populate with dictionary data.
data (Dict[str, Any]) – The source dictionary containing attribute values.
attr_convert_fn (Optional[Callable[[str, Any], Any]]) – Optional function to convert attribute values. Takes attribute name and value as arguments and returns the converted value.
- Returns:
True if successful, False otherwise.
- Return type:
bool
System Information#
- omni.physxclashdetectioncore.utils.get_current_user_name() str#
Returns name of the currently logged-in user.
- Returns:
The current user’s name.
- Return type:
str
- omni.physxclashdetectioncore.utils.get_available_system_memory() int#
Returns available system memory in bytes.
- Returns:
Available system memory in bytes.
- Return type:
int
- omni.physxclashdetectioncore.utils.get_random_word(length: int) str#
Generates random ascii lowercase word of ‘length’ characters.
- Parameters:
length (int) – Length of the random word.
- Returns:
Randomly generated word.
- Return type:
str
Decorators#
- omni.physxclashdetectioncore.utils.measure_execution_time(func: Callable) Callable#
A decorator to measure execution time.
- Parameters:
func (Callable) – The function to measure.
- Returns:
Wrapped function with execution time measurement.
- Return type:
Callable
Classes#
OptimizedProgressUpdate#
- class omni.physxclashdetectioncore.utils.OptimizedProgressUpdate(
- update_rate: float = 0.1,
- force_update_rate: float = 1.0,
- auto_start: bool = True,
A utility class to manage progress updates efficiently by limiting excessive updates.
This class determines whether a progress update should be propagated based on the elapsed time since the last update and the change in progress value. It ensures that updates are sent at a controlled rate, preventing unnecessary updates that could impact performance.
- Parameters:
update_rate (float) – Time interval (in seconds) between successive updates when the progress value changes. Defaults to 0.1 seconds.
force_update_rate (float) – Maximum allowable time interval (in seconds) before forcing an update, regardless of progress change. Defaults to 1.0 seconds.
auto_start (bool) – Whether to initialize progress tracking automatically upon instantiation. Defaults to True.
Methods:
- start()#
Resets the progress tracking state.
This method initializes or resets the internal state, ensuring that the first update is allowed immediately after calling this method.
- update(progress_value: float) bool#
Determines whether a progress update should be propagated.
An update is triggered if either:
The elapsed time since the last update exceeds force_update_rate, or
The progress value has changed and the elapsed time exceeds update_rate.
- Parameters:
progress_value (float) – The current progress value as a fraction between 0.0 and 1.0.
- Returns:
True if an update should be propagated, False otherwise.
- Return type:
bool
Properties:
- progress_value: float#
Gets the last recorded progress value between 0.0 and 1.0+.
Example#
Using utility functions and classes:
from omni.physxclashdetectioncore.utils import (
file_exists,
get_unique_temp_file_path_name,
OptimizedProgressUpdate,
measure_execution_time
)
# Check if file exists
if file_exists("/path/to/file.txt"):
print("File exists")
# Get unique temp file path
temp_file = get_unique_temp_file_path_name(suffix=".tmp")
print(f"Temp file: {temp_file}")
# Use progress tracker
progress = OptimizedProgressUpdate()
for i in range(100):
# Your processing here
if progress.update(float(i) / 100.0):
print(f"Progress: {progress.progress_value * 100:.1f}%")
# Measure execution time
@measure_execution_time
def my_function():
# Your code here
pass
my_function() # Will print execution time