Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lramosduarte/0b40651bc978d1dd4378442c2b601e36 to your computer and use it in GitHub Desktop.
Save lramosduarte/0b40651bc978d1dd4378442c2b601e36 to your computer and use it in GitHub Desktop.
CustomFormatter python3 - Return string to format with replacement fields not used in string
import string
class PartialFormatter(string.Formatter):
def get_field(self, field_name, args, kwargs):
try:
val=super().get_field(field_name, args, kwargs)
except (KeyError, AttributeError):
val = '{{{nome_campo}}}'.format(nome_campo=field_name),field_name
return val
def get_value(self, key, args, kwargs):
valor=super().get_value(key, args, kwargs)
if not valor:
valor = '{{{chave}}}'.format(chave=key)
return valor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment