non rails number_to_human_size ?

43 views
Skip to first unread message

Jorge Vargas

unread,
Oct 7, 2008, 10:58:12 AM10/7/08
to pylons-...@googlegroups.com
Is this not implemented or I miss it somewhere? if it isn't
implemented I think that the rails version is good enough to be ported
to number.py

Mike Orr

unread,
Oct 7, 2008, 6:26:30 PM10/7/08
to pylons-...@googlegroups.com

It was left out for a few reasons:

- There were multiple slightly different implementations (the other is
in WebHelpers*.egg/unfinished/helpers.py written by James), and I
wasn't sure which API was best.

- I wasn't sure if the function was universal enough and non-trivial
enough to warrant being in WebHelpers.

- "human size" is not an accurate function name. The terms are
generally applied to computer hardware, and are perceived as precise
scientific terms rather than colloquial (human) terms.

I like how James's helper supports both binary units (base 1024, GiB)
and decimal units (base 1000, GB), but I don't like how you have to
pass a list of units to every call, or how the units can get out of
sync of the 'binary' flag. The rails helper does not support decimal
units at all.

The rails helper has a ``precision`` argument that's inadequately
documented (what exactly does it do?). James's helper has a
``digits`` argument, meaning the total number of digits on either side
of the decimal point. I'm not sure if that's the right approach
either.
Should it be the number of digits after the decimal point?

I would tend to support a function something like this:

def format_size(n, binary, precision?)
``n`` -- the number
``binary`` -- true to use base 1024 and the "Gi" series.
``precision`` -- not sure about this

But then there's the question of bits (b) vs bytes (B), and people
wanting the whole word spelled out, etc.

So what do y'all suggest?

--
Mike Orr <slugg...@gmail.com>

Mike Orr

unread,
Oct 7, 2008, 6:43:44 PM10/7/08
to pylons-...@googlegroups.com
On Tue, Oct 7, 2008 at 3:26 PM, Mike Orr <slugg...@gmail.com> wrote:
> def format_size(n, binary, precision?)
> ``n`` -- the number
> ``binary`` -- true to use base 1024 and the "Gi" series.
> ``precision`` -- not sure about this
>
> But then there's the question of bits (b) vs bytes (B), and people
> wanting the whole word spelled out, etc.

Perhaps this:

def format_size(n, binary, suffix="B", fullname=False, precision?):

>>> format_size(5000, False)
5 MB
>>> format_size(5120, True, "bytes")
5 Mibytes
>>> format_size(5000, False, "bytes", True)
5 megabytes

What should ``binary`` default to, or should it have a default?

--
Mike Orr <slugg...@gmail.com>

Jorge Vargas

unread,
Oct 7, 2008, 6:47:38 PM10/7/08
to pylons-...@googlegroups.com
On Tue, Oct 7, 2008 at 4:26 PM, Mike Orr <slugg...@gmail.com> wrote:
>
> On Tue, Oct 7, 2008 at 7:58 AM, Jorge Vargas <jorge....@gmail.com> wrote:
>>
>> Is this not implemented or I miss it somewhere? if it isn't
>> implemented I think that the rails version is good enough to be ported
>> to number.py
>
> It was left out for a few reasons:
>
> - There were multiple slightly different implementations (the other is
> in WebHelpers*.egg/unfinished/helpers.py written by James), and I
> wasn't sure which API was best.
>
oh my grep didn't catch that one as I have a released webhelpers in
this project.

> - I wasn't sure if the function was universal enough and non-trivial
> enough to warrant being in WebHelpers.
>

true, but the same could be said of the other number_to_* functions,
for example number_to_phone is useless in my country but that doesn't
means it is useless everywhere.

> - "human size" is not an accurate function name. The terms are
> generally applied to computer hardware, and are perceived as precise
> scientific terms rather than colloquial (human) terms.
>

correct.

> I like how James's helper supports both binary units (base 1024, GiB)
> and decimal units (base 1000, GB), but I don't like how you have to
> pass a list of units to every call, or how the units can get out of
> sync of the 'binary' flag. The rails helper does not support decimal
> units at all.
>
> The rails helper has a ``precision`` argument that's inadequately
> documented (what exactly does it do?). James's helper has a
> ``digits`` argument, meaning the total number of digits on either side
> of the decimal point. I'm not sure if that's the right approach
> either.
> Should it be the number of digits after the decimal point?
>

yea I didn't understood that. although I just need a simple
representation so the "default" was ok

Jorge Vargas

unread,
Oct 7, 2008, 6:56:14 PM10/7/08
to pylons-...@googlegroups.com
That sounds really good it goes from the most simple case to the most
complex. I'll set a default to binary so.
As people are more familiar with Bytes than bits

def format_size(n, binary=False, suffix="B", fullname=False, precision?):

>>> format_size(5000)
5 MB
>>> format_size(5120, True,bytes")
5 Mibytes
>>> format_size(5000, suffix="bytes", fullname=True)
5 megabytes

I'm still puzzled by the name, why not format_computer_size? or
format_data_size, format_size may as well be sizes of clothing.

Mike Orr

unread,
Oct 7, 2008, 7:47:32 PM10/7/08
to pylons-...@googlegroups.com
On Tue, Oct 7, 2008 at 3:56 PM, Jorge Vargas <jorge....@gmail.com> wrote:
> I'm still puzzled by the name, why not format_computer_size? or
> format_data_size, format_size may as well be sizes of clothing.

At first I was going to suggest computer_size(), then I remembered
there was already precedent for format_*() in WebHelpers.

--
Mike Orr <slugg...@gmail.com>

Mike Orr

unread,
Oct 7, 2008, 8:24:25 PM10/7/08
to pylons-...@googlegroups.com
On Tue, Oct 7, 2008 at 3:47 PM, Jorge Vargas <jorge....@gmail.com> wrote:
>
> On Tue, Oct 7, 2008 at 4:26 PM, Mike Orr <slugg...@gmail.com> wrote:
>>
>> On Tue, Oct 7, 2008 at 7:58 AM, Jorge Vargas <jorge....@gmail.com> wrote:
>>>
>>> Is this not implemented or I miss it somewhere? if it isn't
>>> implemented I think that the rails version is good enough to be ported
>>> to number.py
>>
>> It was left out for a few reasons:
>>
>> - There were multiple slightly different implementations (the other is
>> in WebHelpers*.egg/unfinished/helpers.py written by James), and I
>> wasn't sure which API was best.
>>
> oh my grep didn't catch that one as I have a released webhelpers in
> this project.
>
>> - I wasn't sure if the function was universal enough and non-trivial
>> enough to warrant being in WebHelpers.
>>
> true, but the same could be said of the other number_to_* functions,
> for example number_to_phone is useless in my country but that doesn't
> means it is useless everywhere.

number_to_phone was also dropped. I felt it was both too
country-specific and application-specific. If a phone-number helper is
needed, it should be something more general, though I don't know what.
Something like strftime. Though I think North America is the only
place with a consistent number of digits for the area code, prefix,
and number. Other places I see codes and numbers of varying lengths
in the same country.

--
Mike Orr <slugg...@gmail.com>

ChrisB

unread,
Oct 9, 2008, 12:50:54 PM10/9/08
to pylons-discuss
On Oct 7, 3:43 pm, "Mike Orr" <sluggos...@gmail.com> wrote:
> > def format_size(n, binary, precision?)
> >    ``n`` -- the number
> >    ``binary`` -- true to use base 1024 and the "Gi" series.
> >    ``precision`` -- not sure about this

I think "precision" should be an indication of significant figures. So
a precision of 3 would give:

1.32GB

or

432B

whoever, if someone asks for a precision of say, 4, should they get:

1024B
or
1.000kB

-Chris


Reply all
Reply to author
Forward
0 new messages