[Git][NTPsec/ntpsec][unit-rewrite] 2 commits: Added docstring for scalestring, comment tweak.

Ian Bruene gitlab at mg.gitlab.com
Sun Apr 9 21:45:25 UTC 2017


Ian Bruene pushed to branch unit-rewrite at NTPsec / ntpsec


Commits:
dd40ee24 by Ian Bruene at 2017-04-09T15:15:14-05:00
Added docstring for scalestring, comment tweak.

- - - - -
7ceb86c3 by Ian Bruene at 2017-04-09T16:42:24-05:00
Added rescalestring() and tests.

rescalestring() scales a string value by a specified number of units, similar
to the former rescaleunit()

- - - - -


2 changed files:

- pylib/util.py
- tests/pylib/test_util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -181,7 +181,51 @@ def oomsbetweenunits(a, b):
     return abs((a - b) * 3)
 
 
+def rescalestring(value, unitsscaled):
+    if unitsscaled == 0:
+        return value
+    negative = False
+    if value[0] == "-":
+        value = value[1:]
+        negative = True
+    if "." in value:
+        whole, dec = value.split(".")
+    else:
+        whole = value
+        dec = ""
+    hilen = len(whole)
+    lolen = len(dec)
+    digitsmoved = abs(unitsscaled * 3)
+    if unitsscaled > 0:  # Scale to a larger unit
+        if hilen < digitsmoved:  # Scaling beyond the digits, pad it out
+            padcount = digitsmoved - hilen
+            newwhole = "0"
+            newdec = ("0" * padcount) + whole + dec
+        else:  # Scaling in the digits, no need to pad
+            choppoint = -digitsmoved
+            newdec = whole[choppoint:] + dec
+            newwhole = whole[:choppoint]
+            if newwhole == "":
+                newwhole = "0"
+    elif unitsscaled < 0:  # scale to a smaller unit
+        if lolen < digitsmoved:  # Scaling beyone the digits, pad it out
+            padcount = digitsmoved - lolen
+            newwhole = whole + dec + ("0" * padcount)
+            newdec = ""
+        else:
+            newwhole = whole + dec[:digitsmoved]
+            newdec = dec[digitsmoved:]
+    if len(newdec) > 0:
+        newvalue = ".".join((newwhole, newdec))
+    else:
+        newvalue = newwhole
+    if negative is True:
+        newvalue = "-" + newvalue
+    return newvalue
+
+
 def scalestring(value):
+    "Scales a number string to fit in the range 1.0-999.9"
     negative = False
     if value[0] == "-":
         value = value[1:]
@@ -202,7 +246,7 @@ def scalestring(value):
         if i == lolen:  # didn't find anything, this number must equal zero
             newwhole = whole
             newdec = dec
-            negative = False  # -0.000 is meaningless
+            negative = False  # filter our -0.000
             unitsmoved = 0
         else:
             lounits = (i // 3) + 1  # always need to shift one more unit


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -189,5 +189,27 @@ class TestPylibUtilMethods(unittest.TestCase):
         self.assertEqual(f("00000000.000000000000"),
                          ("00000000.000000000000", 0))
 
+    def test_rescalestring(self):
+        f = ntp.util.rescalestring
+
+        # No-op
+        self.assertEqual(f("1000.42", 0), "1000.42")
+        # Scale to higher unit
+        self.assertEqual(f("123.456", 1), "0.123456")
+        # ditto, negative
+        self.assertEqual(f("-123.456", 1), "-0.123456")
+        # Scale to higher unit, beyond available digits
+        self.assertEqual(f("12.34", 2), "0.00001234")
+        # ditto, negative
+        self.assertEqual(f("-12.34", 2), "-0.00001234")
+        # Scale to lower unit
+        self.assertEqual(f("1.23456", -1), "1234.56")
+        # ditto, negative
+        self.assertEqual(f("-1.23456", -1), "-1234.56")
+        # Scale to lower unit, beyond available digits
+        self.assertEqual(f("1.23456", -2), "1234560")
+        # ditto, negative
+        self.assertEqual(f("-1.23456", -2), "-1234560")
+
 if __name__ == '__main__':
     unittest.main()



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/636830c8363cdae9568e77eb6ad408df431fb5b6...7ceb86c352000a44c46169ba30ce3eb95a3a6211
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170409/7f5acd61/attachment.html>


More information about the vc mailing list