Add utilities

This commit is contained in:
Michael Still 2014-03-28 15:48:48 +11:00
parent 5d7c839915
commit 30850ad0f5
1 changed files with 30 additions and 0 deletions

30
reports/utility.py Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/python
import decimal
import types
import unicodedata
def DisplayFriendlySize(bytes):
"""DisplayFriendlySize -- turn a number of bytes into a nice string"""
t = type(bytes)
if t != types.LongType and t != types.IntType and t != decimal.Decimal:
return 'NotANumber(%s=%s)' %(t, bytes)
if bytes < 1024:
return '%d bytes' % bytes
if bytes < 1024 * 1024:
return '%d kb (%d bytes)' %((bytes / 1024), bytes)
if bytes < 1024 * 1024 * 1024:
return '%d mb (%d bytes)' %((bytes / (1024 * 1024)), bytes)
return '%d gb (%d bytes)' %((bytes / (1024 * 1024 * 1024)), bytes)
def Normalize(value):
normalized = unicodedata.normalize('NFKD', unicode(value))
normalized = normalized.encode('ascii', 'ignore')
return normalized