Re: [PATCH v2 1/2] git-p4: print size values in appropriate units
flat view
From: Luke Diamand <hidden>
Date: 2021-12-18 10:09:48
On Sat, 18 Dec 2021 at 07:44, Joachim Kuebart [off-list ref] wrote:
quoted
On 17 Dec 2021, at 21:38, Joel Holdsworth [off-list ref] wrote: git-p4.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)diff --git a/git-p4.py b/git-p4.py index 2b4500226a..4d8a249b85 100755 --- a/git-p4.py +++ b/git-p4.py@@ -56,6 +56,16 @@p4_access_checked = False +def format_size_human_readable(num): + """ Returns a number of units (typically bytes) formatted as a human-readable + string. + """ + for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: + if abs(num) < 1024.0: + return "{:3.1f} {}B".format(num, unit) + num /= 1024.0 + return "{:.1f} Yi{}B".format(num)This now has an extra pair of braces. Cheers, Joachim
It also seems to add some slightly spurious floating point precision.
For example, 1<<20 comes out as "1.0 MiB" and 50 bytes comes out as
"50.0 B".
There's actually an older python2->python3 conversion bug going on here.
The code uses:
Size / 1024 / 1024
With python2 that came out as an integer, which for bytes is what you want.
For python3 it comes out as a float - it should have been converted to
Size // 1024 // 1024.
I guess Joel was trying to preserve that original bug, but really we
should just fix it!
Overall I think this would definitely be an improvement, just needs to
drop the spurious precision (and fix the return for values beyond ZiB
as noted by Joachim).
Luke