Use the string.Formatter()
class:
from string import Formatter
def no_curly_brackets(fmt):
try:
parsed = Formatter().parse(fmt)
return not any('{' in lt or '}' in lt for lt, _, _, _ in parsed)
except ValueError:
return False
Basically anything that's parsable as a format and doesn't contain a curly brace in the parsed literal text would be True
.
This matches all your test cases:
>>> for test in tests:
... print test, no_curly_brackets(test)
...
word True
{word False
{}word True
{word} True
{{word}} False
{{{word}}} False
{word}{{}} False
plus a few of my own:
>>> no_curly_brackets('word}}')
False
>>> no_curly_brackets('{{word')
False
>>> no_curly_brackets('word{{}}')
False