[Git][NTPsec/ntpsec][master] 2 commits: Removed strip argument from unitify.
Gary E. Miller
gitlab at mg.gitlab.com
Thu Apr 20 00:39:44 UTC 2017
Gary E. Miller pushed to branch master at NTPsec / ntpsec
Commits:
52785e36 by Ian Bruene at 2017-04-19T19:35:27-05:00
Removed strip argument from unitify.
strip was a holdover from the earliest version of these formatters, and
logicaly redundant with the width argument
Now width=None strips any whitespace, and width=N formats into a field size
- - - - -
9b6e8ecf by Ian Bruene at 2017-04-19T19:35:27-05:00
Changed stringfiltcooker() to not use magic number
stringfiltcooker() previously used a magic number to find the correct unit
for output. It now looks up the correct value for the starting unit.
- - - - -
4 changed files:
- ntpclients/ntpmon
- ntpclients/ntpq
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
ntpclients/ntpmon
=====================================
--- a/ntpclients/ntpmon
+++ b/ntpclients/ntpmon
@@ -104,19 +104,16 @@ def peer_detail(variables, showunits=False):
if name in vcopy:
vcopy[name] = ntp.util.unitify(vcopyraw[name],
ntp.util.UNIT_MS,
- strip=True,
width=None)
for name in ntp.util.PPM_VARS:
if name in vcopy:
vcopy[name] = ntp.util.unitify(vcopyraw[name],
ntp.util.UNIT_PPM,
- strip=True,
width=None)
for name in ntp.util.S_VARS:
if name in vcopy:
vcopy[name] = ntp.util.unitify(vcopyraw[name],
ntp.util.UNIT_S,
- strip=True,
width=None)
vcopy['filtdelay'] = ntp.util.stringfiltcooker(vcopyraw['filtdelay'])
vcopy['filtoffset'] = ntp.util.stringfiltcooker(vcopyraw['filtoffset'])
=====================================
ntpclients/ntpq
=====================================
--- a/ntpclients/ntpq
+++ b/ntpclients/ntpq
@@ -528,8 +528,7 @@ usage: timeout [ msec ]
self.say("%s %s\n" % (legend, value))
elif fmt in (NTP_UINT, NTP_INT, NTP_FLOAT):
if self.showunits:
- displayvalue = ntp.util.unitifyvar(rawvalue, name,
- strip=True)
+ displayvalue = ntp.util.unitifyvar(rawvalue, name)
else:
displayvalue = value
self.say("%s %s\n" % (legend, displayvalue))
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -111,7 +111,8 @@ def stringfiltcooker(data):
if count > highestcount:
mostcommon = key
highestcount = count
- newunit = UNITS_SEC[mostcommon + 2] # 2==UNIT_MS, all the filt*s use ms
+ mspos = UNITS_SEC.index(UNIT_MS)
+ newunit = UNITS_SEC[mostcommon + mspos]
# Shift all values to the new unit
cooked = []
for part in parts:
@@ -325,7 +326,7 @@ def unitrelativeto(unit, move):
return None # couldn't find anything
-def unitifyvar(value, varname, baseunit=None, strip=False, width=8):
+def unitifyvar(value, varname, baseunit=None, width=8):
"Call unitify() with the correct units for varname"
if varname in S_VARS:
start = UNIT_S
@@ -335,10 +336,10 @@ def unitifyvar(value, varname, baseunit=None, strip=False, width=8):
start = UNIT_PPM
else:
return value
- return unitify(value, start, baseunit, strip, width)
+ return unitify(value, start, baseunit, width)
-def unitify(value, startingunit, baseunit=None, strip=False, width=8):
+def unitify(value, startingunit, baseunit=None, width=8):
"Formats a numberstring with relevant units. Attemps to fit in width."
if baseunit is None:
baseunit = getunitgroup(startingunit)[0]
@@ -357,7 +358,7 @@ def unitify(value, startingunit, baseunit=None, strip=False, width=8):
newvalue = fitinfield(newvalue, realwidth) + unitget
else: # don't have a replacement unit, use original
newvalue = value + startingunit
- if strip is True:
+ if width is None:
newvalue = newvalue.strip()
return newvalue
@@ -598,20 +599,17 @@ def cook(variables, showunits=False):
# missing variables here.
# Completion cannot occur until all units are tracked down.
if showunits:
- item += unitify(rawvalue, UNIT_MS, UNIT_NS,
- True, width=None)
+ item += unitify(rawvalue, UNIT_MS, UNIT_NS, width=None)
else:
item += repr(value)
elif name in S_VARS:
if showunits:
- item += unitify(rawvalue, UNIT_S, UNIT_NS,
- True, width=None)
+ item += unitify(rawvalue, UNIT_S, UNIT_NS, width=None)
else:
item += repr(value)
elif name in PPM_VARS:
if showunits:
- item += unitify(rawvalue, UNIT_PPM,
- strip=True, width=None)
+ item += unitify(rawvalue, UNIT_PPM, width=None)
else:
item += repr(value)
else:
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -199,7 +199,7 @@ class TestPylibUtilMethods(unittest.TestCase):
# Different units
self.assertEqual(f("12.345", nu.UNIT_PPM), "12.35ppm")
# Strip
- self.assertEqual(f("1.23", nu.UNIT_MS, strip=True), "1.23ms")
+ self.assertEqual(f("1.23", nu.UNIT_MS, width=None), "1.23ms")
# Different width
self.assertEqual(f("1.234", nu.UNIT_MS, width=12), " 1.234ms")
# Outside of available units
@@ -207,8 +207,7 @@ class TestPylibUtilMethods(unittest.TestCase):
# 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")
+ self.assertEqual(f("15937.5", nu.UNIT_MS, width=None), "15.9375s")
def test_stringfiltcooker(self):
# No scale
@@ -233,6 +232,7 @@ class TestPylibUtilMethods(unittest.TestCase):
self.assertEqual(ntp.util.stringfiltcooker(
"0.47 0.4200 0.4300 0.5000 0.4600 0.4200 0.4900 0.4800"),
" 0.47 0.4200 0.4300 0.5000 0.4600 0.4200 0.4900 0.4800 ms")
+
def test_unitrelativeto(self):
f = ntp.util.unitrelativeto
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/5704c031dd6c3516069ef1f554158c3928d03b42...9b6e8ecfa09b53302784abc94316369a468fc8b7
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/5704c031dd6c3516069ef1f554158c3928d03b42...9b6e8ecfa09b53302784abc94316369a468fc8b7
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/20170420/d17ad66d/attachment.html>
More information about the vc
mailing list