Skip to content

Commit 4ecab3f

Browse files
committed
Add utility function bytes_to_human_size
1 parent 02a9d4e commit 4ecab3f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

mergin/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,21 @@ def is_mergin_config(path: str) -> bool:
275275
"""Check if the given path is for file mergin-config.json"""
276276
filename = os.path.basename(path).lower()
277277
return filename == "mergin-config.json"
278+
279+
def bytes_to_human_size(bytes: int):
280+
"""
281+
Convert bytes to human readable size
282+
example :
283+
bytes_to_human_size(5600000) -> "5.3 MB"
284+
"""
285+
precision = 1;
286+
if ( bytes < 1e-5 ):
287+
return "0.0 MB";
288+
elif ( bytes < 1024.0 * 1024.0 ):
289+
return f"{round( bytes / 1024.0, precision )} KB";
290+
elif ( bytes < 1024.0 * 1024.0 * 1024.0 ):
291+
return f"{round( bytes / 1024.0 / 1024.0, precision)} MB";
292+
elif ( bytes < 1024.0 * 1024.0 * 1024.0 * 1024.0 ):
293+
return f"{round( bytes / 1024.0 / 1024.0 / 1024.0, precision )} GB";
294+
else:
295+
return f"{round( bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0, precision )} TB";

0 commit comments

Comments
 (0)