[Kimchi-devel] [PATCH] [Wok] Added utils method for handling locale based formating of number along with Measurement format.

archus at linux.vnet.ibm.com archus at linux.vnet.ibm.com
Mon Apr 18 21:28:01 UTC 2016


From: Archana Singh <archus at linux.vnet.ibm.com>

Added formatNumber method which set the passed locale
to method and does formatting of number, and revert back
the locale setting.

Added formatMeasurement method which format number
measurement also format as per locale.

Signed-off-by: Archana Singh <archus at linux.vnet.ibm.com>
---
 src/wok/utils.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/src/wok/utils.py b/src/wok/utils.py
index 0e8ec05..b37518f 100644
--- a/src/wok/utils.py
+++ b/src/wok/utils.py
@@ -33,6 +33,7 @@ import subprocess
 import sys
 import traceback
 import xml.etree.ElementTree as ET
+import locale
 
 from cherrypy.lib.reprconf import Parser
 from datetime import datetime, timedelta
@@ -646,3 +647,90 @@ def decode_value(val):
     if not isinstance(val, unicode):
         val = val.decode('utf-8')
     return val
+
+
+def formatMeasurement(number, settings):
+    '''
+    Refer to "Units of information" (
+    http://en.wikipedia.org/wiki/Units_of_information
+    ) for more information about measurement units.
+
+   @param number The number to be normalized.
+   @param settings
+        base Measurement base, accepts 2 or 10. defaults to 2.
+        unit The unit of the measurement, e.g., B, Bytes/s, bps, etc.
+        fixed The number of digits after the decimal point.
+        locale The locale for formating the number if not passed
+        format is done as per current locale.
+   @returns [object]
+       v The number part of the measurement.
+       s The suffix part of the measurement including multiple and unit.
+          e.g., kB/s means 1000B/s, KiB/s for 1024B/s.
+    '''
+    unitBaseMapping = {2: [{"us": 'Ki', "v": 1024},
+                           {"us": 'Mi', "v": 1048576},
+                           {"us": 'Gi', "v": 1073741824},
+                           {"us": 'Ti', "v": 1099511627776},
+                           {"us": 'Pi', "v": 1125899906842624}],
+                       10: [{"us": 'k', "v": 1000},
+                            {"us": 'M', "v": 1000000},
+                            {"us": 'G', "v": 1000000000},
+                            {"us": 'T', "v": 1000000000000},
+                            {"us": 'P', "v": 1000000000000000}]}
+
+    if(not number):
+        return number
+    settings = settings or {}
+    unit = settings['unit'] if 'unit' in settings else 'B'
+    base = settings['base'] if 'base' in settings else 2
+
+    new_locale = settings['locale'] if 'locale' in settings else ''
+
+    if(base != 2 and base != 10):
+        return encode_value(number) + unit
+
+    fixed = settings['fixed']
+
+    unitMapping = unitBaseMapping[base]
+    for mapping in reversed(unitMapping):
+        suffix = mapping['us']
+        startingValue = mapping['v']
+        if(number < startingValue):
+            continue
+
+        formatted = float(number) / startingValue
+        formatted = formatNumber(formatted, fixed, new_locale)
+        return formatted + suffix + unit
+
+    formatted_number = formatNumber(number, fixed, new_locale)
+    return formatted_number+unit
+
+
+def formatNumber(number, fixed, format_locale):
+    '''
+    Format the number based on format_locale passed.
+    '''
+
+    # get the current locale
+    current_locale = locale.getlocale()
+    new_locale = ''
+    # set passed locale and set new_locale to same value.
+    if format_locale:
+        new_locale = locale.setlocale(locale.LC_ALL, format_locale)
+
+    # Based on type of number use the correct formatter
+    if isinstance(number, float):
+        if fixed:
+            formatted = locale.format('%' + '.%df' % fixed, number, True)
+        else:
+            formatted = locale.format('%f', number, True)
+    if isinstance(number, int):
+        formatted = locale.format('%d', number, True)
+    # After formatting is done as per locale, reset the locale if changed.
+    if (new_locale and not current_locale[0] and not current_locale[1]):
+        locale.setlocale(locale.LC_ALL, 'C')
+    elif (new_locale):
+        locale.setlocale(locale.LC_ALL, current_locale[0] + "." +
+                         current_locale[1])
+
+    return formatted
-- 
2.5.0




More information about the Kimchi-devel mailing list