Merged Rolf Schmidt's illysam branch
[hooke.git] / hooke / ui / gui / prettyformat.py
index 0303699ebd5cf957a1b3753e65a6272569364d81..0c0a662bdf1d87d7fbde8fa52730469d95d10bf2 100644 (file)
-# Copyright (C) 2009-2010 Rolf Schmidt <rschmidt@alcor.concordia.ca>
-#                         W. Trevor King <wking@drexel.edu>
-#
-# This file is part of Hooke.
-#
-# Hooke is free software: you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation, either
-# version 3 of the License, or (at your option) any later version.
-#
-# Hooke is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with Hooke.  If not, see
-# <http://www.gnu.org/licenses/>.
-
-"""Format values with nice prefixes.
-History:
-
-* 2009 07 16:
-
-  * added negative number support
-  * added decimal-formatted output
-"""
-__version__ = "1.0.1"
-
-import math
-from numpy import isnan
-
-def pretty_format(fValue, sUnit='', iDecimals=-1, iMultiplier=1, bLeadingSpaces=False):
-    if fValue != 0:
-        iLeadingSpaces = 0
-        if bLeadingSpaces:
-            iLeadingSpaces = 5
-        if iMultiplier == 1:
-            iMultiplier=get_multiplier(fValue)
-        sUnitString = ''
-        if sUnit != '':
-            sUnitString = ' ' + get_prefix(iMultiplier) + sUnit
-        if iDecimals >= 0:
-            formatString = '% ' + repr(iLeadingSpaces + iDecimals) + '.' + repr(iDecimals) + 'f'
-            return formatString % (fValue / iMultiplier) + sUnitString
-        else:
-            return str(fValue / iMultiplier) + sUnitString
-    else:
-        return '0'
-    return str(fValue / iMultiplier) + ' ' + get_prefix(fValue / iMultiplier) + sUnit
-
-def get_multiplier(fValue):
-    return pow(10, get_power(fValue))
-
-def get_power(fValue):
-    if fValue != 0 and not isnan(fValue):
-        #get the log10 from fValue (make sure the value is not negative)
-        dHelp = math.floor(math.log10(math.fabs(fValue)))
-        #reduce the log10 to a multiple of 3 and return it
-        return dHelp-(dHelp % 3)
-    else:
-        return 0
-
-def get_prefix(fValue):
-    #set up a dictionary to find the prefix
-    prefix = {
-        24: lambda: 'Y',
-        21: lambda: 'Z',
-        18: lambda: 'E',
-        15: lambda: 'P',
-        12: lambda: 'T',
-        9: lambda: 'G',
-        6: lambda: 'M',
-        3: lambda: 'k',
-        0: lambda: '',
-        -3: lambda: 'm',
-        -6: lambda: u'\u00B5',
-        -9: lambda: 'n',
-        -12: lambda: 'p',
-        -15: lambda: 'f',
-        -18: lambda: 'a',
-        -21: lambda: 'z',
-        -24: lambda: 'y',
-    }
-    if fValue != 0 and not isnan(fValue):
-        #get the log10 from fValue
-        dHelp = math.floor(math.log10(math.fabs(fValue)))
-    else:
-        dHelp = 0
-    #reduce the log10 to a multiple of 3 and create the return string
-    return prefix.get(dHelp - (dHelp % 3))()
-
-'''
-dTestValue=-2.4115665714484597e-008
-print 'Value: '+str(dTestValue)+')'
-print 'pretty_format example (value, unit)'
-print pretty_format(dTestValue, 'N')
-print'-----------------------'
-print 'pretty_format example (value, unit, decimals)'
-print pretty_format(dTestValue, 'N', 3)
-print'-----------------------'
-print 'pretty_format example (value, unit, decimals, multiplier)'
-print pretty_format(dTestValue, 'N', 5, 0.000001)
-print'-----------------------'
-print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'
-print pretty_format(0.0166276297705, 'N', 3, 0.001, True)
-print pretty_format(0.00750520813323, 'N', 3, 0.001, True)
-print pretty_format(0.0136453282825, 'N', 3, 0.001, True)
-'''
-'''
-#example use autoFormatValue
-dTestValue=0.00000000567
-print 'autoFormatValue example ('+str(dTestValue)+')'
-print autoFormatValue(dTestValue, 'N')
-#outputs 5.67 nN
-'''
-'''
-#example use of decimalFormatValue(fValue, iDecimals, sUnit):
-dTestValue=-2.4115665714484597e-008
-iDecimals=3
-print 'decimalFormatValue example ('+str(dTestValue)+')'
-print decimalFormatValue(dTestValue, iDecimals, 'N')
-#outputs -24.116 nN
-#change iDecimals to see the effect
-'''
-'''
-#example use formatValue
-dTestValue=0.000000000567
-print 'formatValue example ('+str(dTestValue)+')'
-#find the (common) multiplier
-iMultiplier=get_multiplier(dTestValue)
-#use the multiplier and a unit to format the value
-print formatValue(dTestValue, iMultiplier, 'N')
-#outputs 567.0 pN
-'''
-'''
-#to output a scale:
-#choose any value on the axis and find the multiplier and prefix for it
-#use those to format the rest of the scale
-#as values can span several orders of magnitude, you have to decide what units to use
-
-#tuple of values:
-scaleValues=0.000000000985, 0.000000001000, 0.000000001015
-#use this element (change to 1 or 2 to see the effect on the scale and label)
-iIndex=0
-#get the multiplier from the value at iIndex
-iMultiplier=get_multiplier(scaleValues[iIndex])
-print '\nScale example'
-iDecimals=3
-#print the scale
-for aValue in scaleValues: print decimalFormat(aValue/iMultiplier, iDecimals),
-#print the scale label using the value at iIndex
-print '\n'+get_prefix(scaleValues[iIndex])+'N'
-'''
+# Copyright\r
+\r
+"""Simple Python function to format values with nice prefixes.\r
+"""\r
+\r
+import math\r
+from numpy import isnan\r
+\r
+\r
+def pretty_format(value, unit='', decimals=-1, multiplier=0, leading_spaces=False):\r
+    if value == 0:\r
+        return '0'\r
+    if isnan(value):\r
+        return 'NaN'\r
+\r
+    output_str = ''\r
+    leading_spaces_int = 0\r
+    if leading_spaces:\r
+        leading_spaces_int = 5\r
+    #automatic setting of multiplier\r
+    if multiplier == 0:\r
+        multiplier=get_multiplier(value)\r
+    unit_str = ''\r
+    if unit:\r
+        unit_str = ' ' + get_prefix(multiplier) + unit\r
+    if decimals >= 0:\r
+        format_str = '% ' + repr(leading_spaces_int + decimals) + '.' + repr(decimals) + 'f'\r
+        output_str = format_str % (value / multiplier) + unit_str\r
+    else:\r
+        output_str = str(value / multiplier) + unit_str\r
+\r
+    if decimals < 0:\r
+        output_str = str(value / multiplier) + ' ' + get_prefix(value / multiplier) + unit\r
+\r
+    if leading_spaces_int == 0:\r
+        output_str = output_str.lstrip()\r
+\r
+    return output_str\r
+\r
+def get_multiplier(value):\r
+    return pow(10, get_power(value))\r
+\r
+def get_power(value):\r
+    if value != 0 and not isnan(value):\r
+        #get the log10 from value (make sure the value is not negative)\r
+        value_temp = math.floor(math.log10(math.fabs(value)))\r
+        #reduce the log10 to a multiple of 3 and return it\r
+        return value_temp - (value_temp % 3)\r
+    else:\r
+        return 0\r
+\r
+def get_exponent(prefix):\r
+    #set up a dictionary to find the exponent\r
+    exponent = {\r
+        'Y': 24,\r
+        'Z': 21,\r
+        'E': 18,\r
+        'P': 15,\r
+        'T': 12,\r
+        'G': 9,\r
+        'M': 6,\r
+        'k': 3,\r
+        '': 0,\r
+        'm': -3,\r
+        u'\u00B5': -6,\r
+        'n': -9,\r
+        'p': -12,\r
+        'f': -15,\r
+        'a': -18,\r
+        'z': -21,\r
+        'y': -24,\r
+    }\r
+    if exponent.has_key(prefix):\r
+        return exponent[prefix]\r
+    else:\r
+        return 1\r
+\r
+def get_prefix(value):\r
+    #set up a dictionary to find the prefix\r
+    prefix = {\r
+        24: lambda: 'Y',\r
+        21: lambda: 'Z',\r
+        18: lambda: 'E',\r
+        15: lambda: 'P',\r
+        12: lambda: 'T',\r
+        9: lambda: 'G',\r
+        6: lambda: 'M',\r
+        3: lambda: 'k',\r
+        0: lambda: '',\r
+        -3: lambda: 'm',\r
+        -6: lambda: u'\u00B5',\r
+        -9: lambda: 'n',\r
+        -12: lambda: 'p',\r
+        -15: lambda: 'f',\r
+        -18: lambda: 'a',\r
+        -21: lambda: 'z',\r
+        -24: lambda: 'y',\r
+    }\r
+    if value != 0 and not isnan(value):\r
+        #get the log10 from value\r
+        value_temp = math.floor(math.log10(math.fabs(value)))\r
+    else:\r
+        value_temp = 0\r
+    #reduce the log10 to a multiple of 3 and create the return string\r
+    return prefix.get(value_temp - (value_temp % 3))()\r
+\r
+'''\r
+test_value=-2.4115665714484597e-008\r
+print 'Value: '+str(test_value)+')'\r
+print 'pretty_format example (value, unit)'\r
+print pretty_format(test_value, 'N')\r
+print'-----------------------'\r
+print 'pretty_format example (value, unit, decimals)'\r
+print pretty_format(test_value, 'N', 3)\r
+print'-----------------------'\r
+print 'pretty_format example (value, unit, decimals, multiplier)'\r
+print pretty_format(test_value, 'N', 5, 0.000001)\r
+print'-----------------------'\r
+print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'\r
+print pretty_format(0.0166276297705, 'N', 3, 0.001, True)\r
+print pretty_format(0.00750520813323, 'N', 3, 0.001, True)\r
+print pretty_format(0.0136453282825, 'N', 3, 0.001, True)\r
+'''\r
+'''\r
+#to output a scale:\r
+#choose any value on the axis and find the multiplier and prefix for it\r
+#use those to format the rest of the scale\r
+#as values can span several orders of magnitude, you have to decide what units to use\r
+\r
+#tuple of values:\r
+scale_values=0.000000000985, 0.000000001000, 0.000000001015\r
+#use this element (change to 1 or 2 to see the effect on the scale and label)\r
+index=0\r
+#get the multiplier from the value at index\r
+multiplier=get_multiplier(scale_values[index])\r
+print '\nScale example'\r
+decimals=3\r
+#print the scale\r
+for aValue in scale_values: print decimalFormat(aValue/multiplier, decimals),\r
+#print the scale label using the value at index\r
+print '\n'+get_prefix(scale_values[index])+'N'\r
+'''\r