2024-03-20 22:30:52 +08:00
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
|
|
|
|
|
|
2024-03-19 13:16:25 +08:00
|
|
|
|
def convert_size(size: int, precision: int = 2, add_unit: bool = True, suffix: str = "iB") -> str:
|
|
|
|
|
"""把字节数转换为人类可读的字符串,计算正负
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
|
|
add_unit: 是否添加单位,False后则suffix无效
|
|
|
|
|
suffix: iB或B
|
|
|
|
|
precision: 浮点数的小数点位数
|
|
|
|
|
size (int): 字节数
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
str: The human-readable string, e.g. "1.23 GB".
|
|
|
|
|
"""
|
|
|
|
|
is_negative = False
|
|
|
|
|
if size < 0:
|
|
|
|
|
is_negative = True
|
|
|
|
|
size = -size
|
|
|
|
|
|
|
|
|
|
for unit in ["", "K", "M", "G", "T", "P", "E", "Z", "Y"]:
|
|
|
|
|
if size < 1024:
|
|
|
|
|
if add_unit:
|
|
|
|
|
result = f"{size:.{precision}f} {unit}" + suffix
|
|
|
|
|
return f"-{result}" if is_negative else result
|
|
|
|
|
else:
|
|
|
|
|
return f"{size:.{precision}f}"
|
|
|
|
|
size /= 1024
|
|
|
|
|
if add_unit:
|
|
|
|
|
return f"{size:.{precision}f} Y" + suffix
|
|
|
|
|
else:
|
|
|
|
|
return f"{size:.{precision}f}"
|
2024-03-19 21:56:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def de_escape(text: str) -> str:
|
|
|
|
|
str_map = {
|
2024-03-20 22:30:52 +08:00
|
|
|
|
"[": "[",
|
|
|
|
|
"]": "]",
|
|
|
|
|
"&": "&",
|
|
|
|
|
",": ",",
|
2024-03-19 21:56:31 +08:00
|
|
|
|
}
|
|
|
|
|
for k, v in str_map.items():
|
|
|
|
|
text = text.replace(k, v)
|
|
|
|
|
|
|
|
|
|
return text
|
2024-03-20 22:30:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def encode_url(text: str) -> str:
|
|
|
|
|
return quote(text, safe="")
|