Mike Orr wrote: > Nobody has followed up with suggestions for a better API spec. Should > I ask again for volunteers, leave the ticket open, or just close it? > My concern is this is too implementation-specific an issue to come up > with an API that would work for a wide variety of users, which is our > threshold for helper acceptance.
In the attached file you can find my proposal for the function name, spec and implementation. What do you think about it? Feel free to modify if anyone has an idea of how to improve it. Regards, Wojciech Malinowski --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-devel" group. To post to this group, send email to pylons-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en -~----------~----~----~----~------~----~------~--~---
def format_data_size(size, precision=1, binary_prefix=True, full_name=False, unit='byte'): if unit not in ('bit', 'byte'): raise Exception('Unknown unit') if not full_name and unit=='byte': unit = 'B' if not binary_prefix: base = 1000 if full_name: multiples = ('', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'yotta') else: multiples = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') else: base = 1024 if full_name: multiples = ('', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi', 'zebi', 'yobi') else: multiples = ('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi') from math import log, pow m = int(log(size) / log(base)) if m > 8: m = 8 if m == 0: precision = '%.0f' else: precision = '%%.%df' % precision size = precision % (size / pow(base, m)) return '%s %s%s' % (size.strip(), multiples[m], unit) if __name__ == '__main__': assert format_data_size(1) == '1 B' assert format_data_size(1000) == '1000 B' assert format_data_size(1024, 0) == '1 KiB' assert format_data_size(1024, 2) == '1.00 KiB' assert format_data_size(1024, 0, False) == '1 kB' assert format_data_size(1024, 2, False) == '1.02 kB' assert format_data_size(1024, 0, False, True) == '1 kilobyte' assert format_data_size(1024, 2, False, True) == '1.02 kilobyte' assert format_data_size(1024, 0, False, True, 'bit') == '1 kilobit' assert format_data_size(1024, 2, False, True, 'bit') == '1.02 kilobit' assert format_data_size(12345678, 2, True) == '11.77 MiB' assert format_data_size(12345678, 2, False) == '12.35 MB' assert format_data_size(12345678901234, 2, True) == '11.23 TiB' assert format_data_size(12345678901234, 2, False) == '12.35 TB' assert format_data_size(1234567890123456789012, 2, True) == '1.05 ZiB' assert format_data_size(1234567890123456789012, 2, False) == '1.23 ZB' assert format_data_size(123456789012345678901234567890, 2, True) == '102121.06 YiB' assert format_data_size(123456789012345678901234567890, 2, False) == '123456.79 YB'