Commit 0a9a0e5a authored by René Nussbaumer's avatar René Nussbaumer
Browse files

utils.algo: Add InvertDict to invert a dict


Signed-off-by: default avatarRené Nussbaumer <rn@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 03881cb0
......@@ -115,6 +115,16 @@ def NiceSort(values, key=None):
return sorted(values, key=keyfunc)
def InvertDict(dict_in):
"""Inverts the key/value mapping of a dict.
@param dict_in: The dict to invert
@returns the inverted dict
"""
return dict(zip(dict_in.values(), dict_in.keys()))
class RunningTimeout(object):
"""Class to calculate remaining timeout when doing several operations.
......
......@@ -229,6 +229,13 @@ class TestNiceSort(unittest.TestCase):
None, ""])
class TestInvertDict(unittest.TestCase):
def testInvertDict(self):
test_dict = { "foo": 1, "bar": 2, "baz": 5 }
self.assertEqual(algo.InvertDict(test_dict),
{ 1: "foo", 2: "bar", 5: "baz"})
class TimeMock:
def __init__(self, values):
self.values = 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