工具函数¶
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
时间工具 (aloha.util.time)¶
该模块提供用于包装函数调用(如通过 requests 或 httpx 发起外部 HTTP 请求)的超时控制工具,并在操作成功或失败(超时/异常)时触发可选的回调函数。
核心函数¶
run_with_timeout: 以同步方式运行函数,并应用超时限制。run_async_with_timeout: 以异步方式(协程或在执行器中运行同步函数)运行函数,并应用超时限制。
使用示例¶
from aloha.util.time import run_with_timeout
import requests
def success_callback(response):
print("请求成功:", response.status_code)
def fail_callback(exception):
print("请求失败或超时:", exception)
# 同步超时包装调用
try:
run_with_timeout(
requests.get,
2.5, # 2.5 秒超时限制
"https://httpbin.org/delay/1",
fn_callback_success=success_callback,
fn_callback_fail=fail_callback
)
except TimeoutError:
print("捕获到超时异常 (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.