---
lib/utils/algo.py | 10 ++++++++++
test/ganeti.utils.algo_unittest.py | 7 +++++++
2 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/lib/utils/algo.py b/lib/utils/algo.py
index 543ac77..280a1b7 100644
--- a/lib/utils/algo.py
+++ b/lib/utils/algo.py
@@ -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.
diff --git a/test/ganeti.utils.algo_unittest.py
b/test/ganeti.utils.algo_unittest.py
index b4e3a64..96b4ff5 100755
--- a/test/ganeti.utils.algo_unittest.py
+++ b/test/ganeti.utils.algo_unittest.py
@@ -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
--
1.7.3.1