[Git][NTPsec/ntpsec][unit-rewrite] Added stringfiltcooker() and tests. Related bugfix for rescalestring().

Ian Bruene gitlab at mg.gitlab.com
Mon Apr 10 22:18:45 UTC 2017


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


Commits:
cc311f04 by Ian Bruene at 2017-04-10T17:18:01-05:00
Added stringfiltcooker() and tests. Related bugfix for rescalestring().

- - - - -


2 changed files:

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


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -93,6 +93,33 @@ def zerowiggle(ooms):
     return 10 ** -ooms
 
 
+def stringfiltcooker(data):
+    "Cooks a filt* string of space seperated numbers, expects milliseconds"
+    parts = data.split()
+    oomcount = {}
+    # Find out what the 'natural' unit of each value is
+    for part in parts:
+        value, oom = scalestring(part)
+        oomcount[oom] = oomcount.get(oom, 0) + 1
+    # Find the most common unit
+    mostcommon = None
+    highestcount = 0
+    for key in oomcount.keys():
+        count = oomcount[key]
+        if count > highestcount:
+            mostcommon = key
+            highestcount = count
+    newunit = UNITS_SEC[mostcommon + UNIT_MS]
+    # Shift all values to the new unit
+    cooked = []
+    for part in parts:
+        part = rescalestring(part, mostcommon)
+        fitted = fitinfield(part, 7)
+        cooked.append(fitted)
+    rendered = " ".join(cooked) + " " + newunit
+    return rendered
+
+
 def filtcooker(data):
     "Cooks the string of space seperated numbers with units"
     parts = data.split()
@@ -211,9 +238,12 @@ def gluenumberstring(above, below, isnegative):
 
 def rescalestring(value, unitsscaled):
     "Rescale a number string by a given number of units"
+    whole, dec, negative = breaknumberstring(value)
     if unitsscaled == 0:
+        if whole == "":  # render .1 as 0.1
+            whole = "0"
+        value = gluenumberstring(whole, dec, negative)
         return value
-    whole, dec, negative = breaknumberstring(value)
     hilen = len(whole)
     lolen = len(dec)
     digitsmoved = abs(unitsscaled * 3)


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -323,5 +323,17 @@ class TestPylibUtilMethods(unittest.TestCase):
         self.assertEqual(f("1.234", nu.UNITS_SEC, nu.UNIT_MS, width=12),
                          "     1.234ms")
 
+    def test_stringfiltcooker(self):
+        # No scale
+        self.assertEqual(ntp.util.stringfiltcooker(
+            "1.02 34.5 0.67835 -23.0 9 6.7 1.00 .1"),
+            "   1.02    34.5 0.67835   -23.0       9     6.7    1.00     0.1 ms"
+        )
+        # Scale
+        self.assertEqual(ntp.util.stringfiltcooker(
+            "1000.02 3400.5 0.67835 -23.0 9001 6.7 1.00 1234"),
+            "1.00002  3.4005 0.00067 -0.0230   9.001  0.0067 0.00100   1.234 s"
+        )
+
 if __name__ == '__main__':
     unittest.main()



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/cc311f04af5fe88c208f0136e0201a488ba61bf6
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170410/295b905f/attachment.html>


More information about the vc mailing list