Re: [PATCH v2 1/2] git-p4: print size values in appropriate units
- From
Luke Diamand <luke@diamand.org>
- Date
- Dec 18, 2021, 10:09 UTC
- Message-ID
- <CAE5ih7_knaVsxYBkWrJO4+jURM02E98iL9juroahW0k=ZF7Oiw@mail.gmail.com>
- In-Reply-To
- <39F3A9A9-AE61-4C2E-AA66-6B072634D91E@gmail.com>
On Sat, 18 Dec 2021 at 07:44, Joachim Kuebart <joachim.kuebart@gmail.com> wrote:
Show 30 quoted lines
>
>
>
> > On 17 Dec 2021, at 21:38, Joel Holdsworth <jholdsworth@nvidia.com> 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,
> JoachimIt 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