[Patches] [ python-Patches-1528167 ] Tweak to make string.Templates more customizable

SourceForge.net noreply at sourceforge.net
Thu Aug 3 20:14:26 CEST 2006


Patches item #1528167, was opened at 2006-07-25 00:15
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1528167&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: Python 2.6
Status: Open
Resolution: None
Priority: 5
Submitted By: Chad Whitacre (whit537)
Assigned to: Barry A. Warsaw (bwarsaw)
Summary: Tweak to make string.Templates more customizable

Initial Comment:
Python's $-style templating is wired for optional
case-insensitivity under the hood, but doesn't expose
this via the API. The attached patch (against trunk/
r50813) adds a new optional argument to turn this
behavior on, and includes doc and tests.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2006-08-03 13:14

Message:
Logged In: YES 
user_id=80475

The replacement of _multimap is a semantic change. 
Formerly, a dictionary passed in as a positional argument is
left unmolested; now, it is mutated to reflect kwds.   Also,
the _multimap setup is an O(1) step while the update()
approach is O(n).  Also, the current implementation only
requires the positional argument to support __getitem__();
now, it requires a more fully compliant duck (one that
provides an .update() method).

-            mapping = _multimap(kws, args[0])
+            mapping = args[0]
+            mapping.update(kws)

Current behavior:

>>> t = Template('the $speed brown')
>>> d = dict(speed='quick')
>>> t.safe_substitute(d, speed='slow')
'the slow brown'
>>> d
{'speed': 'quick'}

After the patch, it returns {'speed': 'slow'}.

Likewise the following now works, but would fail after the
patch:

>>> t = Template('the $speed brown')
>>> class D:
	def __getitem__(self, key):
		return 'quick'
>>> t.safe_substitute(D(), speed='slow')
'the slow brown'

The whole approach is misguided, and the use case itself is
suspect.  If some change is warranted, consider a cleaner,
more general-purpose solution like having an optional key=
argument to the Template constructor for pre-processing keys:

>>> t = Template('the $speed brown', key=str.lower)


----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2006-08-01 21:12

Message:
Logged In: YES 
user_id=12800

Looks good to me.  Remind us when we fork the trunk.

----------------------------------------------------------------------

Comment By: Chad Whitacre (whit537)
Date: 2006-08-01 09:07

Message:
Logged In: YES 
user_id=340931

Ok, here it is! I added a test but wasn't sure this
warranted a doc change. Cf.:

  http://docs.python.org/dev/lib/node40.html

----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2006-08-01 08:24

Message:
Logged In: YES 
user_id=12800

Yes, that would be much more acceptable!

----------------------------------------------------------------------

Comment By: Chad Whitacre (whit537)
Date: 2006-08-01 08:21

Message:
Logged In: YES 
user_id=340931

Thanks for your responses, all.

> Is there a reason why the existing mechanisms are 
> insufficient?

The problem is kws: you can't customize it from the outside
like you can the mapping argument. If we replaced _multimap
with mapping.update(kws), then we'd have our hook and I
think I'd be satisfied.

Would you be any more psyched about that patch? :)

----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2006-07-31 22:56

Message:
Logged In: YES 
user_id=12800

I'm not psyched about the patch.  First, I've always thought
that case insensitivity ought to be handled by the mapping
from which the keys are being extracted and by setting the
idpattern.  Second, I definitely don't like adding the
case_sensitive argument to substitute() and
safe_substitute(). Because it lives in the same namespace as
kws it makes for icky rules on resolving any conflicts.

Is there a reason why the existing mechanisms are insufficient?

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2006-07-31 18:12

Message:
Logged In: YES 
user_id=80475

Barry, is this something you want or is it at odds with the
notion of "simplified templating"?

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-07-31 17:32

Message:
Logged In: YES 
user_id=849994

The patch looks very thorough and complete, I will try to
look into it after 2.5 is out.

Don't let that prevent you reviewing 5 patches, Chad ;-)

----------------------------------------------------------------------

Comment By: Chad Whitacre (whit537)
Date: 2006-07-31 13:35

Message:
Logged In: YES 
user_id=340931

(BTW, new patch is against trunk/ r51008)

----------------------------------------------------------------------

Comment By: Chad Whitacre (whit537)
Date: 2006-07-31 13:13

Message:
Logged In: YES 
user_id=340931

I've replaced the patch with one that polishes off a few
bugs and ambiguities, with accompanying tests and doc.

----------------------------------------------------------------------

Comment By: Chad Whitacre (whit537)
Date: 2006-07-26 14:50

Message:
Logged In: YES 
user_id=340931

Warning: I've discovered that I introduced a bug. New patch
forthcoming.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1528167&group_id=5470


More information about the Patches mailing list