Skip to content

Instantly share code, notes, and snippets.

@kisielk
Created May 9, 2011 20:32
Show Gist options
  • Select an option

  • Save kisielk/963327 to your computer and use it in GitHub Desktop.

Select an option

Save kisielk/963327 to your computer and use it in GitHub Desktop.
PartialFormatter
from string import Formatter
class PartialFormatter(Formatter):
def format(self, format_string, *args, **kwargs):
self.fields = kwargs.keys()
def parse(self, format_string):
for parsed in super(PartialFormatter, self).parse(format_string):
if parsed[1] and parsed[1] not in self.fields:
literal_text = parsed[0] + "{" + parsed[1]
if parsed[2] is not None and len(parsed[2]) > 0:
literal_text += ":" + parsed[2]
if parsed[3] is not None and len(parsed[3]) > 0:
literal_text += "!" + parsed[3]
literal_text += "}"
yield (literal_text, None, None, None)
else:
yield parsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment