MCPcopy Create free account
hub / github.com/EverMind-AI/MSA / format_bytes

Function format_bytes

src/utils/tools.py:5–32  ·  view source on GitHub ↗

将字节数转换为人类可读的格式 单位为 M 及以下不要小数点,单位为 G 以上保留一位小数,数值不能小于 1

(size_in_bytes)

Source from the content-addressed store, hash-verified

3import threading
4
5def format_bytes(size_in_bytes):
6 """
7 将字节数转换为人类可读的格式
8 单位为 M 及以下不要小数点,单位为 G 以上保留一位小数,数值不能小于 1
9 """
10 # 定义单位
11 units = ['B', 'K', 'M', 'G', 'T', 'P']
12
13 # 处理边界情况
14 if size_in_bytes < 1:
15 return "0B"
16
17 # 计算单位索引
18 unit_index = 0
19 size = float(size_in_bytes)
20
21 while size >= 1024 and unit_index < len(units) - 1:
22 size /= 1024
23 unit_index += 1
24
25 # 根据单位决定格式化方式
26 if unit_index <= 2: # B, K, M 不要小数点
27 if size == int(size):
28 return f"{int(size)}{units[unit_index]}"
29 else:
30 return f"{int(round(size))}{units[unit_index]}"
31 else: # G 及以上保留一位小数
32 return f"{size:.1f}{units[unit_index]}"
33
34def cumulative_concat(tensors):
35 # 一次性获取所有信息

Callers 1

_post_processMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected