Edgewall Software

Ticket #211 (closed enhancement: fixed)

Opened 7 years ago

Last modified 7 years ago

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

mapping-style-__mod__-r826.diff Download (5.3 KB) - added by cboos 7 years ago.
Adds mapping-style interpolation to Markup constructor and __mod__ operator
mapping-style-__mod__-only-r826.diff Download (5.2 KB) - added by cboos 7 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 Download (5.5 KB) - added by cboos 7 years ago.
3rd attempt, trying out the idea of skipping the Markup __new__ static method altogether.

Change History

Changed 7 years ago by cboos

Adds mapping-style interpolation to Markup constructor and __mod__ operator

Changed 7 years ago by cboos

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

Changed 7 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 ;-)

Changed 7 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 7 years ago by cboos

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

Changed 7 years ago by cmlenz

  • status changed from new to closed
  • resolution set to fixed
  • milestone set to 0.5

Patch applied in [829]. Thanks!

Add/Change #211 (support mapping-style % formatting in Markup)

Author


E-mail address and user name can be saved in the Preferences.


Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
 
Note: See TracTickets for help on using tickets.