lordkrandel wrote:
#On 11/01/2011 15.00, Lorenzo Sutton wrote:
#>  def bytesToHuman (b,factor=1024.):
#>  units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB']
#>  for i in xrange (1,len (units)+1):
#>      if b<  factor ** i or i>= len(units):
#>          return ('%.1f %s') % ((b/(factor **(i-1))),units[i-1])
#
# Che ne dici di un po' di magia
Eh eh.. ai limiti dell'offuscamento ;)
# Dimmi se funziona
Certo che funziona.. molto carina.
def f(
     bytes,
     factor=1024,
     labels=['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi'],
     precision=4
):
     for x,y in map(
         lambda x: (
             factor**(x[0]+1),
             ('%.' + str(precision) + 'lf %sB') % (bytes / factor**x[0], x[1])
         ),
         enumerate(labels)
     ):
         if (bytes<  x) or (y == labels[-1]):
             return y

# Test
for x in range(16):
     y = 512.12*(10**x)
     print f(y)

_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a