def f( bytes, factor, labels, precision ): for i, label in enumerate(labels,1): if (bytes < factor ** i) or (label == labels[-1]): return '%.*lf %s' % (precision, bytes / factor ** (i-1), label)
Dipende a cosa ti serve. Se รจ solo per 1024 e per i file, quella di Django va benissimo, ma se devi convertire i millimetri in Kilometri allora dovresti fotocopiarne un'altra. On 12/01/2011 13.59, Marcello wrote: > Massi`, > pero` il codice qui (cito Nicola, 6a mail di questo thread) > http://code.djangoproject.com/browser/django/trunk/django/template/defaultfilters.py#L794 > lo trovo piu` leggibile. > (Esplicito e` meglio che implicito ecc... ) > > Opinioni personali, eh :) > > > On Wed, Jan 12, 2011 at 1:54 PM, Andrea Ambu<andrea...@gmail.com> wrote: >> Pythonese >> >> def human_from_bytes(bytes, factor=1024., labels=['B', 'KiB', 'MiB', 'GiB',\ >> 'TiB', 'PiB'], >> precision=2): >> index = 0 >> while 1. * bytes / factor**index> factor and index< len(labels)-1: >> index += 1 >> return ('%.*lf %s') % (precision, 1.*bytes/factor**index, >> labels[index]) >> >> >> >> -- >> Andrea >> _______________________________________________ >> Python mailing list >> Python@lists.python.it >> http://lists.python.it/mailman/listinfo/python >> > _______________________________________________ > Python mailing list > Python@lists.python.it > http://lists.python.it/mailman/listinfo/python > -- Wyrmskull _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python