git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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,
> 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
Previous: Joachim KuebartNext: Joel Holdsworth
Message 4 of 7 in “git-p4: improve formatting of numeric values”
  1. 0/2 git-p4: improve formatting of numeric valuesJoel Holdsworth, Dec 17, 2021
  2. 1/2 git-p4: print size values in appropriate unitsJoel Holdsworth, Dec 17, 2021
  3. Joachim KuebartDec 18, 2021
  4. Luke DiamandDec 18, 2021
  5. 2/2 git-p4: show progress as an integerJoel Holdsworth, Dec 17, 2021
  6. Luke DiamandDec 18, 2021
  7. Junio C HamanoDec 20, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.