VirtualMailManager/common.py
branchv0.6.x
changeset 396 7f931c1ca059
parent 395 0a13849243f2
child 399 fb22773f7a85
equal deleted inserted replaced
395:0a13849243f2 396:7f931c1ca059
    67     return binary
    67     return binary
    68 
    68 
    69 
    69 
    70 def human_size(size):
    70 def human_size(size):
    71     """Converts the `size` in bytes in human readable format."""
    71     """Converts the `size` in bytes in human readable format."""
    72     if not isinstance(size, (long, int)) or size < 0:
    72     if not isinstance(size, (long, int)):
    73         raise TypeError("'size' must be a positive long or int.")
    73         try:
       
    74             size = long(size)
       
    75         except ValueError:
       
    76             raise TypeError("'size' must be a positive long or int.")
       
    77     if size < 0:
       
    78         raise ValueError("'size' must be a positive long or int.")
    74     if not size:
    79     if not size:
    75         return '0b'
    80         return '0b'
    76     unit_limit = (('T', 1 << 40), ('G', 1 << 30), ('M', 1 << 20),
    81     unit_limit = (('T', 1 << 40), ('G', 1 << 30), ('M', 1 << 20),
    77                   ('k', 1 << 10), ('b', 1))
    82                   ('k', 1 << 10), ('b', 1))
    78     for unit, limit in unit_limit:
    83     for unit, limit in unit_limit: