From 04ca9b9fff0c58a703c13b830c3a25377b39cd61 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 17 Jul 2019 19:22:02 -0600 Subject: [PATCH] Added bytes_to_string() * Renamed from human_readable_size() for clarity * Now supports KB vs KiB * Now supports negative values * Removed width logic as that should be handled elsewhere * Removed auto conversion to bytes if passed a string * An exception will now be raised if an invalid size is given --- scripts/wk/std.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/scripts/wk/std.py b/scripts/wk/std.py index 04ae898a..f66b3f8d 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -71,6 +71,53 @@ def beep(repeat=1): repeat -= 1 +def bytes_to_string(size, decimals=0, use_binary=True): + """Convert size into a human-readable format, returns str.""" + LOG.debug( + 'size: %s, decimals: %s, use_binary: %s', + size, + decimals, + use_binary, + ) + size = float(size) + + # Set scale + scale = 1000 + suffix = 'B' + if use_binary: + scale = 1024 + suffix = 'iB' + + # Convert to sensible units + if abs(size) >= scale ** 5: + size /= scale ** 5 + units = 'P' + suffix + elif abs(size) >= scale ** 4: + size /= scale ** 4 + units = 'T' + suffix + elif abs(size) >= scale ** 3: + size /= scale ** 3 + units = 'G' + suffix + elif abs(size) >= scale ** 2: + size /= scale ** 2 + units = 'M' + suffix + elif abs(size) >= scale ** 1: + size /= scale ** 1 + units = 'K' + suffix + else: + size /= scale ** 0 + units = ' {}B'.format(' ' if use_binary else '') + size = '{size:0.{decimals}f} {units}'.format( + size=size, + decimals=decimals, + units=units, + ) + + # Done + LOG.debug('string: %s', size) + return size + + def choice(choices, prompt='答えろ!'): """Choose an option from a provided list, returns str.