Changeset 7078 for main


Ignore:
Timestamp:
11 Nov 2011, 09:10:26 (13 years ago)
Author:
uli
Message:

Add helper for turning numbers of bytes into some human-readable
string.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/helpers.py

    r6531 r7078  
    403403        if not b1:
    404404            return True
     405
     406def string_from_bytes(number):
     407    """Turn a number into some textual representation.
     408
     409      Examples:
     410
     411        >>> string_from_bytes(1)
     412        u'1 byte(s)'
     413
     414        >>> string_from_bytes(1025)
     415        u'1 KB'
     416
     417        >>> string_from_bytes(1.5 * 1024*1024)
     418        u'1.50 MB'
     419
     420        >>> string_from_bytes(673.286 * 1024**3)
     421        u'673.29 GB'
     422
     423    """
     424    if number < 1024:
     425        return u'%s byte(s)' % (str(number),)
     426    elif number < 1024**2:
     427        return u'%s KB' % (number / 1024,)
     428    elif number < 1024**3:
     429        return u'%.2f MB' % (number / 1024**2,)
     430    return u'%.2f GB' % (number / 1024**3,)
Note: See TracChangeset for help on using the changeset viewer.