[Git][NTPsec/ntpsec][master] Cleaned up missing docstrings, comments, and tests

Gary E. Miller gitlab at mg.gitlab.com
Wed Apr 19 19:48:21 UTC 2017


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


Commits:
1d95c7de by Ian Bruene at 2017-04-19T14:39:24-05:00
Cleaned up missing docstrings, comments, and tests

- - - - -


2 changed files:

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


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -111,7 +111,7 @@ def stringfiltcooker(data):
         if count > highestcount:
             mostcommon = key
             highestcount = count
-    newunit = UNITS_SEC[mostcommon + 2]  # 2==UNIT_MS
+    newunit = UNITS_SEC[mostcommon + 2]  # 2==UNIT_MS, all the filt*s use ms
     # Shift all values to the new unit
     cooked = []
     for part in parts:
@@ -123,15 +123,17 @@ def stringfiltcooker(data):
 
 
 def getunitgroup(unit):
+    "Returns the unit group which contains a given unit"
     for group in unitgroups:
         if unit in group:
             return group
 
 
 def oomsbetweenunits(a, b):
+    "Calculates how many orders of magnitude seperate two units"
     group = getunitgroup(a)
     if b is None:  # asking for baseunit
-        return group.index(a)
+        return (group.index(a) * 3)
     elif b in group:
         ia = group.index(a)
         ib = group.index(b)
@@ -180,8 +182,7 @@ 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"
+        # This may seem redundant, but glue forces certian formatting details
         value = gluenumberstring(whole, dec, negative)
         return value
     hilen = len(whole)
@@ -192,14 +193,12 @@ def rescalestring(value, unitsscaled):
             # Scaling beyond the digits, pad it out. We can pad here
             # without making up digits that don't exist
             padcount = digitsmoved - hilen
-            newwhole = "0"
+            newwhole = ""
             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, move decimal right
         if lolen < digitsmoved:
             # Scaling beyond the digits would force us to make up data
@@ -215,6 +214,7 @@ def rescalestring(value, unitsscaled):
 
 
 def formatzero(value):
+    "Scale a zero value for the unit with the highest available precision"
     scale = maxdownscale(value)
     newvalue = rescalestring(value, scale).lstrip("-")
     return (newvalue, scale)
@@ -287,6 +287,7 @@ def fitinfield(value, fieldsize):
 
 
 def cropprecision(value, ooms):
+    "Crops digits below the maximum precision"
     if "." not in value:  # No decimals, nothing to crop
         return value
     if ooms == 0:  # We are at the baseunit, crop it all
@@ -301,6 +302,7 @@ def cropprecision(value, ooms):
 
 
 def isstringzero(value):
+    "Detects whether a string is equal to zero"
     for i in value:
         if i not in ("-", ".", "0"):
             return False
@@ -324,6 +326,7 @@ def unitrelativeto(unit, move):
 
 
 def unitifyvar(value, varname, baseunit=None, strip=False, width=8):
+    "Call unitify() with the correct units for varname"
     if varname in S_VARS:
         start = UNIT_S
     elif varname in MS_VARS:


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -31,6 +31,7 @@ class TestPylibUtilMethods(unittest.TestCase):
 
         self.assertEqual(f(ntp.util.UNIT_KS, ntp.util.UNIT_MS), 6)
         self.assertEqual(f(ntp.util.UNIT_PPM, ntp.util.UNIT_PPB), 3)
+        self.assertEqual(f(ntp.util.UNIT_S, None), 9)
 
     def test_scalestring(self):
         f = ntp.util.scalestring
@@ -148,6 +149,8 @@ class TestPylibUtilMethods(unittest.TestCase):
         self.assertEqual(f("1.994", 4), "1.99")
         # Rounding test, round up
         self.assertEqual(f("1.995", 4), "2.00")
+        # Attempt to catch bug
+        self.assertEqual(f("15937.5", None), "15937.5")
 
     def test_cropprecision(self):
         f = ntp.util.cropprecision
@@ -203,6 +206,9 @@ class TestPylibUtilMethods(unittest.TestCase):
         self.assertEqual(f("1234.5", nu.UNIT_KS), "1234.5ks")
         # Seconds
         self.assertEqual(f("42.23", nu.UNIT_S), "  42.23s")
+        # Attempt to catch bug
+        self.assertEqual(f("15937.5", nu.UNIT_MS, strip=True, width=None),
+                         "15.9375s")
 
     def test_stringfiltcooker(self):
         # No scale
@@ -243,5 +249,25 @@ class TestPylibUtilMethods(unittest.TestCase):
         # Different unitgroup
         self.assertEqual(f(ntp.util.UNIT_PPM, -1), ntp.util.UNIT_PPB)
 
+    def test_formatzero(self):
+        f = ntp.util.formatzero
+
+        # No scaling possible
+        self.assertEqual(f("0.00"), ("0.00", 0))
+        # Scaling possible
+        self.assertEqual(f("0.0000000"), ("0.0", -2))
+        # Scaling without a remainder
+        self.assertEqual(f("0.000"), ("0", -1))
+
+    def test_unitifyvar(self):
+        f = ntp.util.unitifyvar
+
+        # Second var
+        self.assertEqual(f("1.234", "tai"), "  1.234s")
+        # Millisecond var
+        self.assertEqual(f("1.234", "offset"), " 1.234ms")
+        # PPM var
+        self.assertEqual(f("1.234", "frequency"), "1.234ppm")
+
 if __name__ == '__main__':
     unittest.main()



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/1d95c7deb3bbfe427a38f660277d6840ebee2cf3

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/1d95c7deb3bbfe427a38f660277d6840ebee2cf3
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/20170419/943721a4/attachment.html>


More information about the vc mailing list