[Git][NTPsec/ntpsec][unit-rewrite] 2 commits: Added breaknumberstring() and tests.
Ian Bruene
gitlab at mg.gitlab.com
Sun Apr 9 22:16:40 UTC 2017
Ian Bruene pushed to branch unit-rewrite at NTPsec / ntpsec
Commits:
e3b165a3 by Ian Bruene at 2017-04-09T17:07:19-05:00
Added breaknumberstring() and tests.
breaknumberstring() breaks a number string into it's above and below decimal
parts, returning both and a bool for whether the number is negative
- - - - -
5b5e442a by Ian Bruene at 2017-04-09T17:15:55-05:00
Added gluenumberstring(), tests.
gluenumberstring() is the inverse of breaknumberstring.
- - - - -
2 changed files:
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -181,6 +181,34 @@ def oomsbetweenunits(a, b):
return abs((a - b) * 3)
+def breaknumberstring(value):
+ "Breaks a number string into (aboveDecimal, belowDecimal, isNegative?)"
+ if value[0] == "-":
+ value = value[1:]
+ negative = True
+ else:
+ negative = False
+ if "." in value:
+ above, below = value.split(".")
+ else:
+ above = value
+ below = ""
+ return (above, below, negative)
+
+
+def gluenumberstring(above, below, isnegative):
+ "Glues together parts of a number string"
+ if above == "":
+ above = "0"
+ if len(below) > 0:
+ newvalue = ".".join((above, below))
+ else:
+ newvalue = above
+ if isnegative is True:
+ newvalue = "-" + newvalue
+ return newvalue
+
+
def rescalestring(value, unitsscaled):
"Rescale a number string by a given number of units"
if unitsscaled == 0:
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -211,5 +211,38 @@ class TestPylibUtilMethods(unittest.TestCase):
# ditto, negative
self.assertEqual(f("-1.23456", -2), "-1234560")
+ def test_breaknumberstring(self):
+ f = ntp.util.breaknumberstring
+
+ # No decimals, positive
+ self.assertEqual(f("1234567890"), ("1234567890", "", False))
+ # ditto, negative
+ self.assertEqual(f("-1234567890"), ("1234567890", "", True))
+ # No whole, positive
+ self.assertEqual(f(".12345"), ("", "12345", False))
+ # ditto, negative
+ self.assertEqual(f("-.12345"), ("", "12345", True))
+ # Both sides, position
+ self.assertEqual(f("123.456"), ("123", "456", False))
+ # ditto, negative
+ self.assertEqual(f("-123.456"), ("123", "456", True))
+
+ def test_gluenumberstring(self):
+ f = ntp.util.gluenumberstring
+
+ # No decimals, positive
+ self.assertEqual(f("123", "", False), "123")
+ # ditto, negative
+ self.assertEqual(f("123", "", True), "-123")
+ # No whole, positive
+ self.assertEqual(f("", "456", False), "0.456")
+ # ditto, negative
+ self.assertEqual(f("", "456", True), "-0.456")
+ # Both sides, positive
+ self.assertEqual(f("123", "456", False), "123.456")
+ # ditto, negative
+ self.assertEqual(f("123", "456", True), "-123.456")
+
+
if __name__ == '__main__':
unittest.main()
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/9886edeed0573b421be43fbdeb2e0bc373122b7f...5b5e442a71f70b3098a867733822e4fddc81785d
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170409/95771e63/attachment.html>
More information about the vc
mailing list