def formatp(string, **kwargs):
def build_stack(string):<EOL><INDENT>"""<STR_LIT>"""<EOL>class Token:<EOL><INDENT>string = "<STR_LIT>"<EOL><DEDENT>class OpeningBracket(Token):<EOL><INDENT>pass<EOL><DEDENT>class ClosingBracket(Token):<EOL><INDENT>pass<EOL><DEDENT>class String(Token):<EOL><INDENT>def __init__(self, str):<EOL><INDENT>self.string = str<EOL><DEDENT><DEDENT>TOKENS = {<EOL>"<STR_LIT:[>": OpeningBracket,<EOL>"<STR_LIT:]>": ClosingBracket,<EOL>}<EOL>stack = []<EOL>next = <NUM_LIT:0><EOL>prev = "<STR_LIT>"<EOL>char = "<STR_LIT>"<EOL>level = <NUM_LIT:0><EOL>while next < len(string):<EOL><INDENT>prev = char<EOL>char = string[next]<EOL>next += <NUM_LIT:1><EOL>if prev != "<STR_LIT:\\>" and char in TOKENS:<EOL><INDENT>token = TOKENS[char]()<EOL>token.index = next<EOL>if char == "<STR_LIT:]>":<EOL><INDENT>level -= <NUM_LIT:1><EOL><DEDENT>token.level = level<EOL>if char == "<STR_LIT:[>":<EOL><INDENT>level += <NUM_LIT:1><EOL><DEDENT>stack.append(token)<EOL><DEDENT>else:<EOL><INDENT>if stack and isinstance(stack[-<NUM_LIT:1>], String):<EOL><INDENT>stack[-<NUM_LIT:1>].string += char<EOL><DEDENT>else:<EOL><INDENT>token = String(char)<EOL>token.level = level<EOL>stack.append(token)<EOL><DEDENT><DEDENT><DEDENT>return stack<EOL><DEDENT>def build_tree(items, level=<NUM_LIT:0>):<EOL><INDENT>"""<STR_LIT>"""<EOL>subtree = []<EOL>while items:<EOL><INDENT>nested = []<EOL>while items[<NUM_LIT:0>].level > level:<EOL><INDENT>nested.append(items.pop(<NUM_LIT:0>))<EOL><DEDENT>if nested:<EOL><INDENT>subtree.append(build_tree(nested, level + <NUM_LIT:1>))<EOL><DEDENT>item = items.pop(<NUM_LIT:0>)<EOL>if item.string:<EOL><INDENT>string = item.string<EOL>if level == <NUM_LIT:0>:<EOL><INDENT>subtree.append(string.format(**kwargs))<EOL><DEDENT>else:<EOL><INDENT>fields = re.findall(r"<STR_LIT>", string)<EOL>successful_fields = <NUM_LIT:0><EOL>for fieldspec, fieldname in fields:<EOL><INDENT>if kwargs.get(fieldname, False):<EOL><INDENT>successful_fields += <NUM_LIT:1><EOL><DEDENT><DEDENT>if successful_fields == len(fields):<EOL><INDENT>subtree.append(string.format(**kwargs))<EOL><DEDENT>else:<EOL><INDENT>return []<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return subtree<EOL><DEDENT>def merge_tree(items):<EOL><INDENT>return "<STR_LIT>".join(flatten(items)).replace(r"<STR_LIT>", "<STR_LIT:]>").replace(r"<STR_LIT>", "<STR_LIT:[>")<EOL><DEDENT>stack = build_stack(string)<EOL>tree = build_tree(stack, <NUM_LIT:0>)<EOL>return merge_tree(tree)<EOL>
Function for advanced format strings with partial formatting

This function consumes format strings with groups enclosed in brackets. A
group enclosed in brackets will only become part of the result if all fields
inside the group evaluate True in boolean contexts.

Groups can be nested. The fields in a nested group do not count as fields in
the enclosing group, i.e. the enclosing group will evaluate to an empty
string even if a nested group would be eligible for formatting. Nesting is
thus equivalent to a logical or of all enclosing groups with the enclosed
group.

Escaped brackets, i.e. \\\\[ and \\\\] are copied verbatim to output.

:param string: Format string
:param kwargs: keyword arguments providing data for the format string
:returns: Formatted string