[Git][NTPsec/ntpsec][decimal-fix] 3 commits: Removed outdated formatters.
Gary E. Miller
gitlab at mg.gitlab.com
Thu Apr 13 23:49:11 UTC 2017
Gary E. Miller pushed to branch decimal-fix at NTPsec / ntpsec
Commits:
a7bb53a8 by Ian Bruene at 2017-04-13T23:45:00+00:00
Removed outdated formatters.
- - - - -
0bfbf8d2 by Ian Bruene at 2017-04-13T23:48:38+00:00
Fix for ungarded decimal split.
- - - - -
5f58b872 by Ian Bruene at 2017-04-13T23:48:39+00:00
Rearranged ifs.
- - - - -
2 changed files:
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -11,7 +11,6 @@ import os
import re
import shutil
import collections
-import math
import ntp.ntpc
import ntp.version
@@ -91,11 +90,6 @@ def portsplit(hostname):
return (hostname, portsuffix)
-def zerowiggle(ooms):
- "Generate a wiggle value for float==0 comparisons"
- return 10 ** -ooms
-
-
def stringfiltcooker(data):
"Cooks a filt* string of space seperated numbers, expects milliseconds"
parts = data.split()
@@ -123,90 +117,6 @@ def stringfiltcooker(data):
return rendered
-def filtcooker(data):
- "Cooks the string of space seperated numbers with units"
- parts = data.split()
- floatyparts = []
- oomcount = {}
- # Find out what the 'natural' unit of each value is
- for part in parts:
- part = float(part)
- floatyparts.append(part)
- value, oom = scaleforunit(part, oomsbetweenunits(UNIT_MS, UNIT_NS))
- 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]
- oomstobase = (mostcommon * 3) + 6 # 6 == UNIT_MS distance from base
- # Shift all values to the new unit
- cooked = []
- for part in floatyparts:
- part = rescaleunit(part, mostcommon)
- fmt = formatdigitsplit(part, 7, oomstobase)
- temp = fmt % part
- cooked.append(temp)
- rendered = " ".join(cooked) + " " + newunit
- return rendered
-
-
-def rescaleunit(f, units):
- "Rescale a number by enough orders of magnitude for N units"
- multiplier = 10 ** (units * 3)
- f *= multiplier
- return f
-
-
-def scaleforunit(f, oomstobase):
- "Scales a number by units to keep it in the range 0.000-999.9"
- f = round(f, oomstobase) # pre-round to base unit to filter float folly
- wiggle = zerowiggle(oomstobase)
- if -wiggle < f < wiggle: # if sufficiently close to zero do nothing
- return (f, 0)
- unitsmoved = 0
- af = abs(f)
- if af < 1.0:
- oom = math.floor(math.log10(af))
- else:
- oom = math.log10(af) # Orders Of Magnitude
- oom -= oom % 3 # We only want to move in groups of 3 ooms
- multiplier = 10 ** -oom # Reciprocol because floating * more accurate
- unitsmoved = int(oom // 3)
- f *= multiplier
- roundooms = (unitsmoved * 3) + oomstobase
- f = round(f, roundooms) # Filter out any float folly we introduced here
- return (f, unitsmoved)
-
-
-def roundsubzero(f, oomstobase):
- "Rounds a number at it's base unit"
- mul = 10 ** oomstobase
- f *= mul
- f = round(f)
- return f
-
-
-def formatdigitsplit(f, fieldsize, oomstobase):
- "Create a format string for a float without adding fake precision."
- af = abs(f)
- if af >= 100.0:
- maxdigits = fieldsize - 4 # xxx.
- elif af >= 10.0:
- maxdigits = fieldsize - 3 # xx.
- else:
- maxdigits = fieldsize - 2 # x.
- if f < 0.0:
- maxdigits -= 1 # need to fit a negative symbol
- subdigits = min(oomstobase, maxdigits) # use min so we don't add fake data
- formatter = "%" + str(fieldsize) + "." + str(subdigits) + "f"
- return formatter
-
-
def getunitgroup(unit):
for group in unitgroups:
if unit in group:
@@ -332,19 +242,22 @@ def fitinfield(value, fieldsize):
pad = " " * (fieldsize - vallen)
newvalue = pad + value
else: # Insufficient room, round as few digits as possible
- diff = vallen - fieldsize
- declen = len(value.split(".")[1]) # length of decimals
- croplen = min(declen, diff) # Never round above the decimal point
- roundlen = declen - croplen # How many digits we round to
- newvalue = str(round(float(value), roundlen))
- splitted = newvalue.split(".")
- declen = len(splitted[1])
- if roundlen == 0: # if rounding all the decimals don't display .0
- # but do display the point, to show that there is more beyond
- newvalue = splitted[0] + "."
- elif roundlen > declen: # some zeros have been cropped, fix that
- padcount = roundlen - declen
- newvalue = newvalue + ("0" * padcount)
+ if "." in value: # Ok, we *do* have decimals to crop
+ diff = vallen - fieldsize
+ declen = len(value.split(".")[1]) # length of decimals
+ croplen = min(declen, diff) # Never round above the decimal point
+ roundlen = declen - croplen # How many digits we round to
+ newvalue = str(round(float(value), roundlen))
+ splitted = newvalue.split(".") # This should never fail
+ declen = len(splitted[1])
+ if roundlen == 0: # if rounding all the decimals don't display .0
+ # but do display the point, to show that there is more beyond
+ newvalue = splitted[0] + "."
+ elif roundlen > declen: # some zeros have been cropped, fix that
+ padcount = roundlen - declen
+ newvalue = newvalue + ("0" * padcount)
+ else: # No decimals, nothing we can crop
+ newvalue = value
return newvalue
@@ -423,56 +336,6 @@ def unitify(value, startingunit, baseunit=None, strip=False, width=8):
return newvalue
-def unitformatter(f, unitgroup, startingunit, baseunit=None,
- strip=False, width=8):
- "Formatting for unit associated values in N characters."
- if width is not None: # For padding to n characters
- padder = (lambda x: (" " * (width - len(x))) + x)
- else:
- strip = True
- if baseunit is None:
- baseunit = 0 # Assume that the lowest unit is equal to LSB
- oomsfrombase = oomsbetweenunits(startingunit, baseunit)
- wiggle = zerowiggle(oomsfrombase)
- if -wiggle < f < wiggle: # Zero, don't show decimals
- unit = unitgroup[baseunit] # go all the way to the lsb
- f = roundsubzero(f, oomsfrombase)
- rendered = ("%d" % f) + unit
- if not strip:
- rendered = padder(rendered)
- return rendered
- oldf = f # keep this in case we don't fit in the units
- f, unitsmoved = scaleforunit(f, oomsfrombase)
- unitget = startingunit + unitsmoved
- oomsfrombase = oomsbetweenunits(unitget, baseunit) # will need this later
- if (0 <= unitget < len(unitgroup)):
- unit = unitgroup[unitget]
- if width is None: # Don't care about size, just display everything
- if unitget == baseunit: # Don't want fake decimals
- formatter = "%d"
- rendered = (formatter % f) + unit
- else:
- rendered = repr(f) + unit
- if strip:
- rendered = rendered.strip()
- return rendered
- else: # Do care about size, crop value so it will fit
- displaysize = width - len(unit)
- if unitget == baseunit: # Don't want fake decimals
- formatter = "%" + str(displaysize) + "d"
- else:
- formatter = formatdigitsplit(f, displaysize, oomsfrombase)
- rendered = (formatter % f) + unit
- if strip:
- rendered = rendered.strip()
- return rendered
- else: # Out of units so revert to the original. Ugly but there are very
- rendered = repr(oldf) + unitgroup[startingunit] # few options here
- if not strip:
- rendered = padder(rendered)
- return rendered
-
-
def f8dot4(f):
"Scaled floating point formatting to fit in 8 characters"
if f >= 0:
@@ -676,7 +539,7 @@ def cook(variables, showunits=False):
item += "%03lo" % value
elif name in specials:
if showunits:
- item += filtcooker(value)
+ item += stringfiltcooker(value)
else:
item += "\t".join(value.split())
elif name == "flash":
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -26,132 +26,12 @@ class TestPylibUtilMethods(unittest.TestCase):
"[0000:1111:2222:3333:4444:5555:6666:7777]:123"),
("0000:1111:2222:3333:4444:5555:6666:7777", ":123"))
- def test_rescaleunit(self):
- self.assertEqual(ntp.util.rescaleunit(1.0, 0),
- 1.0)
- self.assertEqual(ntp.util.rescaleunit(10.0, 1),
- 10000.0)
- self.assertEqual(ntp.util.rescaleunit(0.1, 1),
- 100.0)
- self.assertEqual(ntp.util.rescaleunit(10.0, -1),
- 0.01)
- self.assertEqual(ntp.util.rescaleunit(0.1, -1),
- 0.0001)
- self.assertEqual(ntp.util.rescaleunit(-100.0, 1),
- -100000.0)
- self.assertEqual(ntp.util.rescaleunit(-.001, 1),
- -1.0)
- self.assertEqual(ntp.util.rescaleunit(-100.0, -1),
- -0.1)
- self.assertEqual(ntp.util.rescaleunit(-.001, -1),
- -0.000001)
-
- def test_scaleforunit(self):
- self.assertEqual(ntp.util.scaleforunit(1.0, 9),
- (1.0, 0))
- self.assertEqual(ntp.util.scaleforunit(999.2342, 9),
- (999.2342, 0))
- self.assertEqual(ntp.util.scaleforunit(5042.7, 9),
- (5.0427, 1))
- self.assertEqual(ntp.util.scaleforunit(0.1912, 9),
- (191.2, -1))
- self.assertEqual(ntp.util.scaleforunit(0.00000042, 9),
- (420.0, -3))
- self.assertEqual(ntp.util.scaleforunit(-1.0, 9),
- (-1.0, 0))
- self.assertEqual(ntp.util.scaleforunit(-999.2342, 9),
- (-999.2342, 0))
- self.assertEqual(ntp.util.scaleforunit(-5042.7, 9),
- (-5.0427, 1))
- self.assertEqual(ntp.util.scaleforunit(-0.1912, 9),
- (-191.2, -1))
- self.assertEqual(ntp.util.scaleforunit(-0.00000042, 9),
- (-420.0, -3))
- self.assertEqual(ntp.util.scaleforunit(1.0000004, 6),
- (1.0, 0))
- self.assertEqual(ntp.util.scaleforunit(1.0000005, 6),
- (1.000001, 0))
-
def test_oomsbetweenunits(self):
f = ntp.util.oomsbetweenunits
self.assertEqual(f(ntp.util.UNIT_KS, ntp.util.UNIT_MS), 6)
self.assertEqual(f(ntp.util.UNIT_PPM, ntp.util.UNIT_PPB), 3)
- def test_filtcooker(self):
- self.assertEqual(ntp.util.filtcooker(
- "1.02 34.5 0.67835 -23.0 9 6.7 1.0 .1"),
- "1.02000 34.5000 0.67835 -23.000 9.00000 6.70000 1.00000 0.10000 ms"
- )
-
- def test_formatdigitsplit(self):
- self.assertEqual(ntp.util.formatdigitsplit(10.0, 5, 9),
- "%5.2f")
- self.assertEqual(ntp.util.formatdigitsplit(100.52, 5, 9),
- "%5.1f")
- self.assertEqual(ntp.util.formatdigitsplit(10.123456789, 8, 9),
- "%8.5f")
- self.assertEqual(ntp.util.formatdigitsplit(1.123456789, 8, 9),
- "%8.6f")
- self.assertEqual(ntp.util.formatdigitsplit(0.123456789, 8, 9),
- "%8.6f")
- self.assertEqual(ntp.util.formatdigitsplit(1.234, 10, 9),
- "%10.8f")
- self.assertEqual(ntp.util.formatdigitsplit(-1.23456789, 6, 9),
- "%6.3f")
-
- def test_unitformatter(self):
- f = ntp.util.unitformatter
- usec = ntp.util.UNITS_SEC
-
- sec_t = [
- # 1.0000000005 s rounds to 1s
- [1.0000000005, "1.00000s"],
- # -1.0000000005 s rounds to 1s
- [-1.0000000005, "-1.0000s"],
- # 0.9999999999 s round to 1s
- [0.9999999999, "1.00000s"],
- # 0.00000000049 s rounds to 0ns
- [0.00000000049, " 0ns"],
- # 0.00000000051 s rounds to 1ns
- [0.00000000051, " 1ns"],
- ]
-
- for t in sec_t:
- self.assertEqual(f(t[0], usec, ntp.util.UNIT_S), t[1])
-
- # 0.4 ms rounds to 0ns
- self.assertEqual(f(0.0000004, usec, ntp.util.UNIT_MS),
- " 0ns")
- # 0.5 ms rounds to 1ns
- self.assertEqual(f(0.0000005, usec, ntp.util.UNIT_MS),
- " 1ns")
- # 0.5 ms rounds to 1ns, with strip
- self.assertEqual(f(0.0000005, usec, ntp.util.UNIT_MS, strip=True),
- "1ns")
- self.assertEqual(f(12.45, usec, ntp.util.UNIT_MS),
- "12.450ms") # Checking normal
- self.assertEqual(f(12.45, usec, ntp.util.UNIT_MS, strip=True),
- "12.450ms") # Checking normal, strip
- # 12.499999 ms rounds to 12.45 ms
- self.assertEqual(f(12.499999, usec, ntp.util.UNIT_MS,
- strip=True, width=6),
- "12.5ms") # Checking normal, strip
- # 12.451 ms rounds to 12.45 ms
- self.assertEqual(f(12.451, usec, ntp.util.UNIT_MS,
- strip=True, width=7),
- "12.45ms") # Checking normal, strip
- self.assertEqual(f(123456789.1234, usec, ntp.util.UNIT_MS, width=None),
- "123.4567891234ks") # Checking normal, no width
- self.assertEqual(f(0.000005, usec, ntp.util.UNIT_MS),
- " 5ns") # Unit == base
- self.assertEqual(f(0.000005, usec, ntp.util.UNIT_MS, strip=True),
- "5ns") # Unit == base, strip
- self.assertEqual(f(0.000005, usec, ntp.util.UNIT_MS, width=None),
- "5ns") # Unit == base, no width
- self.assertEqual(f(10000.1, usec, ntp.util.UNIT_KS),
- "10000.1ks") # Value outside of unit ranges
-
def test_scalestring(self):
f = ntp.util.scalestring
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/52529a0256ce515474ec628eeeae6f9465e2fd6a...5f58b872dfbc11d9caf1665ccc668fecff140d48
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/52529a0256ce515474ec628eeeae6f9465e2fd6a...5f58b872dfbc11d9caf1665ccc668fecff140d48
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/20170413/bd666f0d/attachment.html>
More information about the vc
mailing list