From 30850ad0f55b0b6e2a6b9e2a194144605ba0fb8d Mon Sep 17 00:00:00 2001 From: Michael Still Date: Fri, 28 Mar 2014 15:48:48 +1100 Subject: [PATCH] Add utilities --- reports/utility.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 reports/utility.py diff --git a/reports/utility.py b/reports/utility.py new file mode 100755 index 0000000..3983acb --- /dev/null +++ b/reports/utility.py @@ -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