[Git][NTPsec/ntpsec][master] Zero values now correctly auto-range.

Gary E. Miller gitlab at mg.gitlab.com
Sat Apr 15 01:00:09 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
6572850d by Ian Bruene at 2017-04-14T19:42:57-05:00
Zero values now correctly auto-range.

- - - - -


2 changed files:

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


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -214,8 +214,16 @@ def rescalestring(value, unitsscaled):
     return newvalue
 
 
+def formatzero(value):
+    scale = maxdownscale(value)
+    newvalue = rescalestring(value, scale).lstrip("-")
+    return (newvalue, scale)
+
+
 def scalestring(value):
     "Scales a number string to fit in the range 1.0-999.9"
+    if isstringzero(value):
+        return formatzero(value)
     whole, dec, negative = breaknumberstring(value)
     hilen = len(whole)
     if (hilen == 0) or isstringzero(whole):  # Need to shift to smaller units
@@ -225,22 +233,16 @@ def scalestring(value):
             if dec[i] != "0":
                 break
             i += 1
-        if i == lolen:  # didn't find anything, this number must equal zero
-            newwhole = whole
-            newdec = dec
-            negative = False  # filter our -0.000
-            unitsmoved = 0
-        else:
-            lounits = (i // 3) + 1  # always need to shift one more unit
+        lounits = (i // 3) + 1  # always need to shift one more unit
+        movechars = lounits * 3
+        if lolen < movechars:
+            # Not enough digits to scale all the way down. Inventing
+            # digits is unacceptable, so scale down as much as we can.
+            lounits = (i // 3)  # "always", unless out of digits
             movechars = lounits * 3
-            if lolen < movechars:
-                # Not enough digits to scale all the way down. Inventing
-                # digits is unacceptable, so scale down as much as we can.
-                lounits = (i // 3)  # "always", unless out of digits
-                movechars = lounits * 3
-            newwhole = dec[:movechars].lstrip("0")
-            newdec = dec[movechars:]
-            unitsmoved = -lounits
+        newwhole = dec[:movechars].lstrip("0")
+        newdec = dec[movechars:]
+        unitsmoved = -lounits
     else:  # Shift to larger units
         hiunits = hilen // 3  # How many we have, not how many to move
         hidigits = hilen % 3
@@ -321,6 +323,10 @@ def unitrelativeto(unit, move):
     return None  # couldn't find anything
 
 
+def zeroformatter():
+    pass
+
+
 def unitifyvar(value, varname, baseunit=None, strip=False, width=8):
     if varname in S_VARS:
         start = UNIT_S
@@ -337,15 +343,12 @@ def unitify(value, startingunit, baseunit=None, strip=False, width=8):
     "Formats a numberstring with relevant units. Attemps to fit in width."
     if baseunit is None:
         baseunit = getunitgroup(startingunit)[0]
-    if isstringzero(value) is True:  # display highest precision zero
-        if strip is False:
-            value = fitinfield("0", width - len(baseunit)) + baseunit
-        else:
-            value = "0" + baseunit
-        return value
     ooms = oomsbetweenunits(startingunit, baseunit)
-    newvalue = cropprecision(value, ooms)
-    newvalue, unitsmoved = scalestring(newvalue)
+    if isstringzero(value):
+        newvalue, unitsmoved = formatzero(value)
+    else:
+        newvalue = cropprecision(value, ooms)
+        newvalue, unitsmoved = scalestring(newvalue)
     unitget = unitrelativeto(startingunit, unitsmoved)
     if unitget is not None:  # We have a unit
         if width is None:


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -48,9 +48,11 @@ class TestPylibUtilMethods(unittest.TestCase):
         # ditto, negative
         self.assertEqual(f("-1.234567890123456"), ("-1.234567890123456", 0))
         # Zero
-        self.assertEqual(f("0.000000"), ("0.000000", 0))
+        self.assertEqual(f("0.000000"), ("0", -2))
+        # Zero with point
+        self.assertEqual(f("0.0000"), ("0.0", -1))
         # Negative zero
-        self.assertEqual(f("-0.000"), ("0.000", 0))
+        self.assertEqual(f("-0.000"), ("0", -1))
         # Large, positive, non scaled
         self.assertEqual(f("987.654"), ("987.654", 0))
         # ditto, negative
@@ -71,7 +73,7 @@ class TestPylibUtilMethods(unittest.TestCase):
                            ("1.23456789098765432100000000000000042", 7))
         # Bizzare 3
         self.assertEqual(f("00000000.000000000000"),
-                         ("00000000.000000000000", 0))
+                         ("0", -4))
 
     def test_rescalestring(self):
         f = ntp.util.rescalestring
@@ -172,7 +174,7 @@ class TestPylibUtilMethods(unittest.TestCase):
         nu = ntp.util
 
         # Zero
-        self.assertEqual(f("0.000", nu.UNIT_MS), "     0ns")
+        self.assertEqual(f("0.000", nu.UNIT_MS), u"     0\u03bcs")
         # Standard, width=8
         self.assertEqual(f("1.234", nu.UNIT_MS), " 1.234ms")
         # ditto, negative



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/6572850da7db15808e4e47b2b758fd4d9fb7ee27

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/6572850da7db15808e4e47b2b758fd4d9fb7ee27
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170415/972079c5/attachment.html>


More information about the vc mailing list