[Git][NTPsec/ntpsec][unit-rewrite] Added unitify() and several helper functions with tests.
Ian Bruene
gitlab at mg.gitlab.com
Mon Apr 10 19:50:10 UTC 2017
Ian Bruene pushed to branch unit-rewrite at NTPsec / ntpsec
Commits:
fbda0e5c by Ian Bruene at 2017-04-10T14:49:03-05:00
Added unitify() and several helper functions with tests.
unitify() is the string replaement for unitformatter()
- - - - -
2 changed files:
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -244,7 +244,7 @@ def scalestring(value):
"Scales a number string to fit in the range 1.0-999.9"
whole, dec, negative = breaknumberstring(value)
hilen = len(whole)
- if (hilen == 0) or (whole[0] == "0"): # Need to shift to smaller units
+ if (hilen == 0) or isstringzero(whole): # Need to shift to smaller units
i = 0
lolen = len(dec)
while i < lolen: # need to find the actual digits
@@ -278,7 +278,9 @@ def scalestring(value):
def fitinfield(value, fieldsize):
"Attempt to fit value into a field, preserving as much data as possible"
vallen = len(value)
- if vallen == fieldsize: # Goldilocks!
+ if fieldsize is None:
+ newvalue = value
+ elif vallen == fieldsize: # Goldilocks!
newvalue = value
elif vallen < fieldsize: # Extra room, pad it out
pad = " " * (fieldsize - vallen)
@@ -292,14 +294,42 @@ def fitinfield(value, fieldsize):
return newvalue
-def unitify(value, unitgroup, startingunit, baseunit=None,
+def cropprecision(value, ooms):
+ if "." not in value: # No decimals, nothing to crop
+ return value
+ if ooms == 0: # We are at the baseunit, crop it all
+ return value.split(".")[0]
+ dstart = value.find(".") + 1
+ dsize = len(value) - dstart
+ precision = min(ooms, dsize)
+ cropcount = dsize - precision
+ if cropcount > 0:
+ value = value[:-cropcount]
+ return value
+
+
+def isstringzero(value):
+ for i in value:
+ if i not in ("-", ".", "0"):
+ return False
+ return True
+
+
+def unitify(value, unitgroup, startingunit, baseunit=0,
strip=False, width=8):
"Formats a numberstring with relevant units. Attemps to fit in width."
- newvalue, unitsmoved = scalestring(value)
+ if isstringzero(value) is True: # display highest precision zero
+ base = unitgroup[baseunit]
+ if strip is False:
+ newvalue = fitinfield("0", width - len(base)) + base
+ return newvalue
+ ooms = oomsbetweenunits(startingunit, baseunit)
+ newvalue = cropprecision(value, ooms)
+ newvalue, unitsmoved = scalestring(newvalue)
unitget = startingunit + unitsmoved
if 0 <= unitget < len(unitgroup): # We have a unit
unit = unitgroup[unitget]
- newvalue = fitinfield(value, len(unit)) + unit
+ newvalue = fitinfield(newvalue, width - len(unit)) + unit
else: # don't have a replacement unit, use original
newvalue = value + unitgroup[startingunit]
return newvalue
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -155,6 +155,8 @@ class TestPylibUtilMethods(unittest.TestCase):
def test_scalestring(self):
f = ntp.util.scalestring
+ # Scale all decimals
+ self.assertEqual(f("0.042"), ("42", -1))
# Typical length, positive value, no scaling
self.assertEqual(f("1.23450"), ("1.23450", 0))
# Ditto, negative
@@ -257,10 +259,33 @@ class TestPylibUtilMethods(unittest.TestCase):
# Field too big
self.assertEqual(f("123.456", 10), " 123.456")
+ def test_cropprecision(self):
+ f = ntp.util.cropprecision
+
+ # No decimals
+ self.assertEqual(f("1234", 6), "1234")
+ # Decimals, no crop
+ self.assertEqual(f("12.3456", 6), "12.3456")
+ # Decimals, crop
+ self.assertEqual(f("12.3456", 3), "12.345")
+ # At baseunit
+ self.assertEqual(f("1.234", 0), "1")
+
+ def test_isstringzero(self):
+ f = ntp.util.isstringzero
+
+ # Non-zero
+ self.assertEqual(f("0.0000001"), False)
+ # Zero
+ self.assertEqual(f("-0.00"), True)
+
def test_unitify(self):
f = ntp.util.unitify
nu = ntp.util
+ # Zero
+ self.assertEqual(f("0.000", nu.UNITS_SEC, nu.UNIT_MS),
+ " 0ns")
# Standard, width=8
self.assertEqual(f("1.234", nu.UNITS_SEC, nu.UNIT_MS),
" 1.234ms")
@@ -269,7 +294,28 @@ class TestPylibUtilMethods(unittest.TestCase):
"-1.234ms")
# Scale to larger unit, width=8
self.assertEqual(f("1234.5", nu.UNITS_SEC, nu.UNIT_MS),
- u" 1.234")
+ " 1.2345s")
+ # ditto, negative
+ self.assertEqual(f("-1234.5", nu.UNITS_SEC, nu.UNIT_MS),
+ "-1.2345s")
+ # Scale to smaller unit, width=8
+ self.assertEqual(f("0.01234", nu.UNITS_SEC, nu.UNIT_MS),
+ u" 12.34\u03bcs")
+ # ditto, negative
+ self.assertEqual(f("-0.01234", nu.UNITS_SEC, nu.UNIT_MS),
+ u"-12.34\u03bcs")
+ # At baseunit
+ self.assertEqual(f("12.0", nu.UNITS_SEC, nu.UNIT_NS),
+ " 12ns")
+ # Scale to baseunit
+ self.assertEqual(f(".042", nu.UNITS_SEC, nu.UNIT_US),
+ " 42ns")
+ # Below baseunit
+ self.assertEqual(f("23.42", nu.UNITS_SEC, nu.UNIT_NS),
+ " 23ns")
+ # Different units
+ self.assertEqual(f("12.345", nu.UNITS_PPX, nu.UNIT_PPM),
+ "12.34ppm")
if __name__ == '__main__':
unittest.main()
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/fbda0e5c112a4e9166d991cd0b8ed15bb2a51a8c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170410/7f5bf869/attachment.html>
More information about the vc
mailing list