Utilities¶
HTML extraction helpers.
extract_img_url(string)
¶
Extract the first image source URL from an HTML fragment.
Source code in aloha/util/html.py
extract_text(raw_data)
¶
Extract visible text from an HTML fragment.
Source code in aloha/util/html.py
ObjectWithDateTimeEncoder
¶
Bases: JSONEncoder
JSON encoder that handles datetime and pandas timestamp objects.
Converts: - pandas NaT to None - pandas Timestamp to Unix timestamp (float) - datetime objects to Unix timestamp (float)
Source code in aloha/util/json.py
default(obj)
¶
Convert objects to JSON serializable types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Object to encode |
required |
Returns:
| Type | Description |
|---|---|
|
JSON serializable representation |
Source code in aloha/util/json.py
get_cpu_info(*args, **kwargs)
¶
Get CPU information.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing CPU information including core count, frequency, and usage percentage |
Source code in aloha/util/sys_info.py
get_disk_info(*args, **kwargs)
¶
Get disk information.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing disk I/O statistics and partition information |
Source code in aloha/util/sys_info.py
get_mem_info(*args, **kwargs)
¶
Get memory information.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing virtual memory and swap space information |
Source code in aloha/util/sys_info.py
get_net_info(*args, **kwargs)
¶
Get network information.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing network I/O statistics and interface information |
Source code in aloha/util/sys_info.py
get_os_info(*args, **kwargs)
¶
Get operating system information.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing OS information including boot time and platform details |
Source code in aloha/util/sys_info.py
get_size(bytes, suffix='B')
¶
Scale bytes to its proper format.
e.g: 1253656 => '1.20MB' 1253656678 => '1.17GB'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bytes
|
Number of bytes to format |
required | |
suffix
|
Unit suffix (default: "B") |
'B'
|
Returns:
| Type | Description |
|---|---|
|
Formatted size string |
Source code in aloha/util/sys_info.py
get_sys_info(*args, **kwargs)
¶
Get comprehensive system information.
Combines information from OS, CPU, memory, disk, and network subsystems.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing complete system information |
Source code in aloha/util/sys_info.py
Time Utilities (aloha.util.time)¶
This module provides tools for wrapping function calls (such as HTTP requests via requests or httpx) with time constraints (timeouts), allowing execution of optional callbacks upon completion or failure.
Key Functions¶
run_with_timeout: Wrap a synchronous function call with a timeout.run_async_with_timeout: Wrap an asynchronous function call with a timeout.
Usage Example¶
from aloha.util.time import run_with_timeout
import requests
def success_callback(response):
print("Request succeeded:", response.status_code)
def fail_callback(exception):
print("Request failed or timed out:", exception)
# Synchronous call with timeout
try:
run_with_timeout(
requests.get,
2.5, # 2.5 seconds timeout
"https://httpbin.org/delay/1",
fn_callback_success=success_callback,
fn_callback_fail=fail_callback
)
except TimeoutError:
print("Caught TimeoutError")
Time and timeout utilities.
run_async_with_timeout(func, timeout_seconds, *args, fn_callback_success=None, fn_callback_fail=None, **kwargs)
async
¶
Wrap an asynchronous function call (coroutine function or sync function inside executor) with a timeout.
If the operation completes within timeout_seconds, fn_callback_success(result)
is executed if provided, and the result is returned.
If the operation times out or raises an exception, fn_callback_fail(exception)
is executed if provided, and the exception is reraised.
Source code in aloha/util/time.py
run_with_timeout(func, timeout_seconds, *args, fn_callback_success=None, fn_callback_fail=None, **kwargs)
¶
Wrap a synchronous function call with a timeout.
If the operation completes within timeout_seconds, fn_callback_success(result)
is executed if provided, and the result is returned.
If the operation times out or raises an exception, fn_callback_fail(exception)
is executed if provided, and the exception is reraised.