Commit f47941f8 authored by Michael Hanselmann's avatar Michael Hanselmann

utils: Export NiceSortKey function

The ability to split a string into a list of strings and integers can be
handy elsewhere and is necessary for sorting query results by names.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 8e74adce
......@@ -26,7 +26,8 @@ import re
import time
_SORTER_RE = re.compile("^%s(.*)$" % (8 * "(\D+|\d+)?"))
_SORTER_GROUPS = 8
_SORTER_RE = re.compile("^%s(.*)$" % (_SORTER_GROUPS * "(\D+|\d+)?"))
_SORTER_DIGIT = re.compile("^\d+$")
......@@ -78,7 +79,7 @@ def _NiceSortTryInt(val):
return val
def _NiceSortKey(value):
def NiceSortKey(value):
"""Extract key for sorting.
"""
......@@ -107,9 +108,9 @@ def NiceSort(values, key=None):
"""
if key is None:
keyfunc = _NiceSortKey
keyfunc = NiceSortKey
else:
keyfunc = lambda value: _NiceSortKey(key(value))
keyfunc = lambda value: NiceSortKey(key(value))
return sorted(values, key=keyfunc)
......
......@@ -218,6 +218,16 @@ class TestNiceSort(unittest.TestCase):
self.assertEqual(keyfn.count, len(data),
msg="Key function was not called once per value")
def testNiceSortKey(self):
self.assertEqual(algo.NiceSortKey(""),
([None] * algo._SORTER_GROUPS) + [""])
self.assertEqual(algo.NiceSortKey("Hello World"),
["Hello World"] +
([None] * int(algo._SORTER_GROUPS - 1)) + [""])
self.assertEqual(algo.NiceSortKey("node1.net75.bld3.example.com"),
["node", 1, ".net", 75, ".bld", 3, ".example.com",
None, ""])
class TimeMock:
def __init__(self, values):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment