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 ValueError:
            # In python 2.6, an empty field name causes Formatter.parse to
            # raise ValueError.
            # 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")

        Test that L{KeyFlattener.flatKey} returns the expected keys for format
        fields.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/logger/test/test_flatten.py