Edgewall Software

Opened 16 years ago

Closed 16 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)

mapping-style-__mod__-r826.diff (5.3 KB) - added by cboos 16 years ago.
Adds mapping-style interpolation to Markup constructor and __mod__ operator
mapping-style-__mod__-only-r826.diff (5.2 KB) - added by cboos 16 years ago.
Second version of the patch, dropping support for interpolation in the Markup constructor.
mapping-style-__mod__-only-and-no-Markup_new-r826.diff (5.5 KB) - added by cboos 16 years ago.
3rd attempt, trying out the idea of skipping the Markup __new__ static method altogether.

Download all attachments as: .zip

Change History (6)

Changed 16 years ago by cboos

Adds mapping-style interpolation to Markup constructor and __mod__ operator

Changed 16 years ago by cboos

Second version of the patch, dropping support for interpolation in the Markup constructor.

comment:1 Changed 16 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 16 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 16 years ago by cboos

3rd attempt, trying out the idea of skipping the Markup __new__ static method altogether.

comment:3 Changed 16 years ago by cmlenz

  • Milestone set to 0.5
  • Resolution set to fixed
  • Status changed from new to closed

Patch applied in [829]. Thanks!

Note: See TracTickets for help on using tickets.