This will be used in QA to format network interface parameters.
This is a cherry-pick of master commit
eac9b7b8
Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
return text
else:
return text[:length - len(_ASCII_ELLIPSIS)] + _ASCII_ELLIPSIS
+
+
+def FormatKeyValue(data):
+ """Formats a dictionary as "key=value" parameters.
+
+ The keys are sorted to have a stable order.
+
+ @type data: dict
+ @rtype: list of string
+
+ """
+ return ["%s=%s" % (key, value) for (key, value) in sorted(data.items())]
self.assertRaises(AssertionError, utils.Truncate, "", i)
+class TestFormatKeyValue(unittest.TestCase):
+ def test(self):
+ self.assertEqual(utils.FormatKeyValue({}), [])
+ self.assertEqual(utils.FormatKeyValue({1: 2}), ["1=2"])
+ self.assertEqual(utils.FormatKeyValue({
+ "zzz": "0",
+ "aaa": "1",
+ }),
+ ["aaa=1", "zzz=0"])
+
+
if __name__ == "__main__":
testutils.GanetiTestProgram()