def test_keyFlattening(self):
        """
        Test that L{KeyFlattener.flatKey} returns the expected keys for format
        fields.
        """

        def keyFromFormat(format):
            for (
                literalText,
                fieldName,
                formatSpec,
                conversion,
            ) in aFormatter.parse(format):
                return KeyFlattener().flatKey(
                    fieldName, formatSpec, conversion
                )

        # No name
        try:
            self.assertEqual(keyFromFormat("{}"), "!:")
        except __HOLE__:
            if sys.version_info[:2] == (2, 6):
                # In python 2.6, an empty field name causes Formatter.parse to
                # raise ValueError.
                pass
            else:
                # In Python 2.7, it's allowed, so this exception is unexpected.
                raise

        # Just a name
        self.assertEqual(keyFromFormat("{foo}"), "foo!:")

        # Add conversion
        self.assertEqual(keyFromFormat("{foo!s}"), "foo!s:")
        self.assertEqual(keyFromFormat("{foo!r}"), "foo!r:")

        # Add format spec
        self.assertEqual(keyFromFormat("{foo:%s}"), "foo!:%s")
        self.assertEqual(keyFromFormat("{foo:!}"), "foo!:!")
        self.assertEqual(keyFromFormat("{foo::}"), "foo!::")

        # Both
        self.assertEqual(keyFromFormat("{foo!s:%s}"), "foo!s:%s")
        self.assertEqual(keyFromFormat("{foo!s:!}"), "foo!s:!")
        self.assertEqual(keyFromFormat("{foo!s::}"), "foo!s::")
        [keyPlusLiteral] = aFormatter.parse("{x}")
        key = keyPlusLiteral[1:]
        sameFlattener = KeyFlattener()
        self.assertEqual(sameFlattener.flatKey(*key), "x!:")
        self.assertEqual(sameFlattener.flatKey(*key), "x!:/2")
dataset/ETHPy150Open twisted/twisted/twisted/logger/test/test_flatten.py/FlatFormattingTests.test_keyFlattening