From 14bdcaf770baa33c20bfe386222c0b9eb14440b5 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 9 Jun 2022 14:46:36 +0200 Subject: [PATCH] Added size print function --- RNS/__init__.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/RNS/__init__.py b/RNS/__init__.py index 5dd856b..ccb7bf8 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -153,6 +153,25 @@ def prettyhexrep(data): hexrep = "<"+delimiter.join("{:02x}".format(c) for c in data)+">" return hexrep +def prettysize(num, suffix='B'): + units = ['','K','M','G','T','P','E','Z'] + last_unit = 'Y' + + if suffix == 'b': + num *= 8 + units = ['','K','M','G','T','P','E','Z'] + last_unit = 'Y' + + for unit in units: + if abs(num) < 1000.0: + if unit == "": + return "%.0f %s%s" % (num, unit, suffix) + else: + return "%.2f %s%s" % (num, unit, suffix) + num /= 1000.0 + + return "%.2f%s%s" % (num, last_unit, suffix) + def panic(): os._exit(255)