From 70bc79015da388f598f8925b6f3d7e0ed6828630 Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Sun, 5 Jan 2025 12:32:48 -0600 Subject: [PATCH] Fix memory calculation algorithm --- pyarchitecture/squire.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pyarchitecture/squire.py b/pyarchitecture/squire.py index 74b3d57..bef4eb7 100644 --- a/pyarchitecture/squire.py +++ b/pyarchitecture/squire.py @@ -24,12 +24,13 @@ def size_converter(byte_size: int | float) -> str: str: Converted human-readable size. """ - if byte_size: + if byte_size > 0: size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") - index = int(math.floor(math.log(byte_size, 1024))) - return ( - f"{format_nos(round(byte_size / pow(1024, index), 2))} {size_name[index]}" - ) + index = 0 + while byte_size >= 1024 and index < len(size_name) - 1: + byte_size /= 1024 + index += 1 + return f"{format_nos(round(byte_size, 2))} {size_name[index]}" return "0 B"