I have a format string fmt
on which the format
method will be called in another module which I have no control over.
In this format string I want to optionally include a field if it was provided as an argument to the format method, otherwise I want to leave it out.
Something like:
fmt = 'Optional value: {value if wasPassed("value") else ""}'
fmt.format(value = "foo") #Should return 'Optional value: foo'
fmt.format() #Should return 'Optional value: ' and not throw a Key error
Since I don't have access to the "format" call I want to solve this within the string somehow. For context I am using this as a format string passed to the logging module but I would really like a general solution for the above problem.
In the specific use case I could pass a custom formatter to the logger but this question really aims at the problem of solving this within an expression in the string to be formatted. (If that is just impossible I would also appreciate an answer indicating why, so that I can stop barking up the wrong tree and just use a custom formatter)