Opened 17 years ago
Closed 17 years ago
#211 closed enhancement (fixed)
support mapping-style % formatting in Markup
Reported by: | cboos | Owned by: | cmlenz |
---|---|---|---|
Priority: | minor | Milestone: | 0.5 |
Component: | General | Version: | 0.4.4 |
Keywords: | Cc: |
Description
Currently, Markup supports % formatting for simple %s, %d, etc. substitution:
m = Markup("Total %d for group %s") % (123, 'closed')
However, the interpolation with named keys is not supported.
m = Markup("Total %(total)d for group %(group)s") % { total=123, group='closed'}
Attachments (3)
Change History (6)
Changed 17 years ago by cboos
Changed 17 years ago by cboos
Second version of the patch, dropping support for interpolation in the Markup constructor.
comment:1 Changed 17 years ago by cboos
The second patch (suggested by cmlenz) is indeed simpler.
Note that Markup_new has been a bit hurried, there's no error checking proper, only truncation of the extra args. We might consider another approach, simply forwarding the all the args wholesale to unicode and have encoding support and error checking for free ;-)
comment:2 Changed 17 years ago by cboos
Actually, now that we started down the way to break compatibility on Markup constructor, I think that not implementing __new__ at all is way better. For example, we can now build Markup strings specifying the encoding directly:
>>> Markup("C'est bientôt <b>l'été</b>!") Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 11: ordinal not in range(128) >>> Markup("C'est bientôt <b>l'été</b>!", sys.stdin.encoding) <Markup u"C'est bient\xf4t <b>l'\xe9t\xe9</b>!">
And it's less code, the diffstat for the patch is 48 insertions(+), 52 deletions(-)!
Changed 17 years ago by cboos
3rd attempt, trying out the idea of skipping the Markup __new__ static method altogether.
comment:3 Changed 17 years ago by cmlenz
- Milestone set to 0.5
- Resolution set to fixed
- Status changed from new to closed
Patch applied in [829]. Thanks!
Adds mapping-style interpolation to Markup constructor and __mod__ operator