> So I created this last night:

> 
> import collections
> 
> class braces_fmt(str):
> 
>     def __mod__(self, stuff):
>         if isinstance(stuff, tuple):
>             return self.__class__(self.format(*stuff))
>         elif isinstance(stuff, collections.Mapping):
>             return self.__class__(self.format(**stuff))
>         else:
>             return self.__class__(self.format(stuff))
> 
> The biggest issue is that ``"%s" % {'a': 42}`` substitutes the dict
> instead of throwing an error that str.format() would do with the code
> above. But what's nice about this is I think I can use this now w/ any
> library that expects % interpolation and it should basically work.

So there's no need to change modules like logging to explicitly provide support 
for {}-formatting? What's not to like? ;-) Something like this perhaps should 
have been added in at the same time as str.format went in.

> I don't think Paul's suggestion requires much more work to support
> string.Template, simply a subclass that implements __mod__

True.

> I guess my question is what's the point of the class if you are simply
> converting it before you pass it in to the logger? To be lazy about
> the formatting call? Otherwise you could simply call str.format() with
> your arguments before you pass the string into the logger and not have
> to wrap anything.

That's exactly the reason - to defer the formatting until it's needed. 
Otherwise you can always format the string yourself,as you say, and pass it as 
the single argument in the logging call - logging won't know or care if it was 
passed in as a literal, or was computed by %-, {}-, $- or any other formatting 
approach.

Regards,

Vinay Sajip



      
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to