test/data/vclamp_jpk/README: Document sample versions
[hooke.git] / hooke / util / si.py
index 48fe4e0c87d7fbb1f725a382325f8223edbb30b7..67366c135eb094eb51550949aaa2c349b0fb791c 100644 (file)
@@ -1,22 +1,19 @@
-# Copyright (C) 2010 Massimo Sandal <devicerandom@gmail.com>
-#                    Rolf Schmidt <rschmidt@alcor.concordia.ca>
-#                    W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
 #
 # 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 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.
+# 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/>.
+# You should have received a copy of the GNU Lesser General Public License
+# along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
 
 """Define functions for handling numbers in SI notation.
 
@@ -33,11 +30,11 @@ Get the power from the first (or last, or middle, ...) value
 
 >>> p = get_power(xs[0])
 >>> for x in xs:
-...     print ppSI(x, decimals=2, power=p)
+...     print(ppSI(x, decimals=2, power=p))
 985.00 p
 1000.00 p
 112358.00 p
->>> print prefix_from_value(xs[0]) + 'N'
+>>> print(prefix_from_value(xs[0]) + 'N')
 pN
 """
 
@@ -90,13 +87,13 @@ def ppSI(value, unit='', decimals=None, power=None, pad=False):
     Examples
     --------
     >>> x = math.pi * 1e-8
-    >>> print ppSI(x, 'N')
+    >>> print(ppSI(x, 'N'))
     31.415927 nN
-    >>> print ppSI(x, 'N', 3)
+    >>> print(ppSI(x, 'N', 3))
     31.416 nN
-    >>> print ppSI(x, 'N', 4, power=-12)
+    >>> print(ppSI(x, 'N', 4, power=-12))
     31415.9265 pN
-    >>> print ppSI(x, 'N', 5, pad=True)
+    >>> print(ppSI(x, 'N', 5, pad=True))
        31.41593 nN
 
     If you want the decimal indented by six spaces with `decimal=2`,
@@ -106,13 +103,13 @@ def ppSI(value, unit='', decimals=None, power=None, pad=False):
     * 1 (length of the decimal point)
     * 2 (places after the decimal point)
 
-    >>> print ppSI(-x, 'N', 2, pad=(6+1+2))
+    >>> print(ppSI(-x, 'N', 2, pad=(6+1+2)))
        -31.42 nN
     """
     if value == 0:
         return '0'
-    if isnan(value):
-        return 'NaN'
+    if value == None or isnan(value):
+        return 'nan'
 
     if power == None:  # auto-detect power
         power = get_power(value)
@@ -127,7 +124,11 @@ def ppSI(value, unit='', decimals=None, power=None, pad=False):
                 # 1 for ' ', 1 for '-', 3 for number, 1 for '.', and decimals.
                 pad = 6 + decimals
             format = lambda n: '%*.*f' % (pad, decimals, n)
-    return '%s %s%s' % (format(value / pow(10,power)), PREFIX[power], unit)
+    try:
+        prefix = ' '+PREFIX[power]
+    except KeyError:
+        prefix = 'e%d ' % power
+    return '%s%s%s' % (format(value / pow(10,power)), prefix, unit)
 
 
 def get_power(value):
@@ -178,7 +179,7 @@ def join_data_label(name, unit):
     >>> join_data_label('z piezo', 'm')
     'z piezo (m)'
     >>> join_data_label('deflection', 'N')
-    'deflection N'
+    'deflection (N)'
     """
     return '%s (%s)' % (name, unit)