Last active
June 15, 2017 16:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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