Convert from DOS to UNIX line endings.
[hooke.git] / hooke / ui / gui / prettyformat.py
index 6454ad92ca7adab73c272ccb5e20fc0a0cf9aa26..32ee4053fa55804e71fbb187f054e0e4a027af39 100644 (file)
-'''\r
-//\r
-// prettyformat.py - simple Python function to format values with nice prefixes\r
-// Version 1.0.1\r
-//\r
-// History\r
-// 2009 07 16: added negative number support\r
-//             added decimal-formatted output\r
-//\r
-// Copyright (c) 2009 Rolf Schmidt, Montreal\r
-// rschmidt@alcor.concordia.ca\r
-//\r
-// This procedure is released under the GNU General Public License version 2\r
-//\r
-'''\r
-\r
-import math\r
-from numpy import isnan\r
-\r
-def pretty_format(fValue, sUnit='', iDecimals=-1, iMultiplier=1, bLeadingSpaces=False):\r
-    if fValue != 0:\r
-        iLeadingSpaces = 0\r
-        if bLeadingSpaces:\r
-            iLeadingSpaces = 5\r
-        if iMultiplier == 1:\r
-            iMultiplier=get_multiplier(fValue)\r
-        sUnitString = ''\r
-        if sUnit != '':\r
-            sUnitString = ' ' + get_prefix(iMultiplier) + sUnit\r
-        if iDecimals >= 0:\r
-            formatString = '% ' + repr(iLeadingSpaces + iDecimals) + '.' + repr(iDecimals) + 'f'\r
-            return formatString % (fValue / iMultiplier) + sUnitString\r
-        else:\r
-            return str(fValue / iMultiplier) + sUnitString\r
-    else:\r
-        return '0'\r
-    return str(fValue / iMultiplier) + ' ' + get_prefix(fValue / iMultiplier) + sUnit\r
-\r
-def get_multiplier(fValue):\r
-    return pow(10, get_power(fValue))\r
-\r
-def get_power(fValue):\r
-    if fValue != 0 and not isnan(fValue):\r
-        #get the log10 from fValue (make sure the value is not negative)\r
-        dHelp = math.floor(math.log10(math.fabs(fValue)))\r
-        #reduce the log10 to a multiple of 3 and return it\r
-        return dHelp-(dHelp % 3)\r
-    else:\r
-        return 0\r
-\r
-def get_prefix(fValue):\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 fValue != 0 and not isnan(fValue):\r
-        #get the log10 from fValue\r
-        dHelp = math.floor(math.log10(math.fabs(fValue)))\r
-    else:\r
-        dHelp = 0\r
-    #reduce the log10 to a multiple of 3 and create the return string\r
-    return prefix.get(dHelp - (dHelp % 3))()\r
-\r
-'''\r
-dTestValue=-2.4115665714484597e-008\r
-print 'Value: '+str(dTestValue)+')'\r
-print 'pretty_format example (value, unit)'\r
-print pretty_format(dTestValue, 'N')\r
-print'-----------------------'\r
-print 'pretty_format example (value, unit, decimals)'\r
-print pretty_format(dTestValue, 'N', 3)\r
-print'-----------------------'\r
-print 'pretty_format example (value, unit, decimals, multiplier)'\r
-print pretty_format(dTestValue, '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
-#example use autoFormatValue\r
-dTestValue=0.00000000567\r
-print 'autoFormatValue example ('+str(dTestValue)+')'\r
-print autoFormatValue(dTestValue, 'N')\r
-#outputs 5.67 nN\r
-'''\r
-'''\r
-#example use of decimalFormatValue(fValue, iDecimals, sUnit):\r
-dTestValue=-2.4115665714484597e-008\r
-iDecimals=3\r
-print 'decimalFormatValue example ('+str(dTestValue)+')'\r
-print decimalFormatValue(dTestValue, iDecimals, 'N')\r
-#outputs -24.116 nN\r
-#change iDecimals to see the effect\r
-'''\r
-'''\r
-#example use formatValue\r
-dTestValue=0.000000000567\r
-print 'formatValue example ('+str(dTestValue)+')'\r
-#find the (common) multiplier\r
-iMultiplier=get_multiplier(dTestValue)\r
-#use the multiplier and a unit to format the value\r
-print formatValue(dTestValue, iMultiplier, 'N')\r
-#outputs 567.0 pN\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
-scaleValues=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
-iIndex=0\r
-#get the multiplier from the value at iIndex\r
-iMultiplier=get_multiplier(scaleValues[iIndex])\r
-print '\nScale example'\r
-iDecimals=3\r
-#print the scale\r
-for aValue in scaleValues: print decimalFormat(aValue/iMultiplier, iDecimals),\r
-#print the scale label using the value at iIndex\r
-print '\n'+get_prefix(scaleValues[iIndex])+'N'\r
+'''
+//
+// prettyformat.py - simple Python function to format values with nice prefixes
+// Version 1.0.1
+//
+// History
+// 2009 07 16: added negative number support
+//             added decimal-formatted output
+//
+// Copyright (c) 2009 Rolf Schmidt, Montreal
+// rschmidt@alcor.concordia.ca
+//
+// This procedure is released under the GNU General Public License version 2
+//
+'''
+
+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'
 '''
\ No newline at end of file