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>
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>
> - 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
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.
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>
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>