samuraisam <[EMAIL PROTECTED]> writes:

> Quick file size formatting for all those seekers out there...
>
> import math
>
> def filesizeformat(bytes, precision=2):
>     """Returns a humanized string for a given amount of bytes"""
>     bytes = int(bytes)
>     if bytes is 0:
>         return '0bytes'
>     log = math.floor(math.log(bytes, 1024))
>     return "%.*f%s" % (
>         precision,
>         bytes / math.pow(1024, log),
>         ['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
> [int(log)]
>     )

The output doesn't match the calculation.

The symbol for "bit" is 'b'. The symbol for "byte" is 'B'. 'kb' is
'kilobit', i.e. 1000 bits. 'mb' is a "metre-bit", a combination of two
units. And so on. The SI units have definitions that are only muddied
by misusing them this way.

Especially since we now have units that actually do mean what we want
them to. The units that match the calculation you're using are 'KiB',
'MiB', 'GiB' and so on.

     <URL:http://en.wikipedia.org/wiki/Binary_prefix>

Dividing by 1024 doesn't give 'kb', it gives 'KiB'. Likewise for the
rest of the units. So the list of units should be::

     ['bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']

-- 
 \                         Contentsofsignaturemaysettleduringshipping. |
  `\                                                                   |
_o__)                                                                  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to