Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [PATCH] allow partial replace in string.Template

Reply
Thread Tools

[PATCH] allow partial replace in string.Template

 
 
Stefan Behnel
Guest
Posts: n/a
 
      02-14-2005
Hi!

Here's a trivial patch against Lib/string.py that adds two new methods. The
first replaces the template by a partially resolved substitution and the
second creates a new, partially substituted template. I find those two useful
enough for integration in the stdlib, especially the replacing one is very
useful when pre-replacing some placeholders outside of string generation loops
or when building a string from a template step by step.

Maybe the method names need some discussion. Also, the creation of a new
Template does not handle sub-classes. I didn't know the best was to do this.
Use type(self)? That doesn't necessarily mean the constructor of that type
takes the same arguments...

Any comments?

Stefan


--- Lib/string.py~ 2004-11-01 04:52:43.000000000 +0100
+++ Lib/string.py 2005-02-14 10:41:41.000000000 +0100
@@ -145,6 +145,12 @@
raise ValueError('Invalid placeholder in string: line %d, col %d' %
(lineno, colno))

+ def partial_replace(self, *args, **kwargs):
+ self.template = self.safe_substitute(*args, **kwargs)
+
+ def partial_substitute(self, *args, **kwargs):
+ return Template( self.safe_substitute(*args, **kwargs) )
+
def substitute(self, *args, **kws):
if len(args) > 1:
raise TypeError('Too many positional arguments')

--- Lib/string.py~ 2004-11-01 04:52:43.000000000 +0100
+++ Lib/string.py 2005-02-14 10:41:41.000000000 +0100
@@ -145,6 +145,12 @@
raise ValueError('Invalid placeholder in string: line %d, col %d' %
(lineno, colno))

+ def partial_substitute(self, *args, **kwargs):
+ return Template( self.safe_substitute(*args, **kwargs) )
+
+ def partial_replace(self, *args, **kwargs):
+ self.template = self.safe_substitute(*args, **kwargs)
+
def substitute(self, *args, **kws):
if len(args) > 1:
raise TypeError('Too many positional arguments')

 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      02-14-2005

a) Patches are more likely to be looked at if placed on the SF patch tracker.

b) I don't quite see the point, given how easy these are to spell using the
basic safe_substitute. You're replacing one liners with one-liners.

Cheers,
Nick.

--
Nick Coghlan | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      02-14-2005


Nick Coghlan wrote
> a) Patches are more likely to be looked at if placed on the SF patch
> tracker.


see your own b), I wanted to discuss them first.

> b) I don't quite see the point, given how easy these are to spell using
> the basic safe_substitute. You're replacing one liners with one-liners.


Still, when I first tried out the Template class, I immediately stumbled over
the fact that the substitute methods always return strings, and never Template
objects. While it makes sense for their primary purpose, it totally restricts
the usage of this module to one-show operations.

Being able to partially evaluate the Template is something that is absolutely
missing in the implementation. I consider that a bug that should be fixed for
2.5 at latest.

Stefan
 
Reply With Quote
 
Nick Craig-Wood
Guest
Posts: n/a
 
      02-14-2005
Nick Coghlan <(E-Mail Removed)> wrote:
>
> a) Patches are more likely to be looked at if placed on the SF patch tracker.
>
> b) I don't quite see the point, given how easy these are to spell using the
> basic safe_substitute. You're replacing one liners with one-liners.


c) add a documentation patch

d) add a test suite patch

--
Nick Craig-Wood <(E-Mail Removed)> -- http://www.craig-wood.com/nick
 
Reply With Quote
 
Nick Coghlan
Guest
Posts: n/a
 
      02-15-2005
Stefan Behnel wrote:
>
>
> Nick Coghlan wrote
>
>> a) Patches are more likely to be looked at if placed on the SF patch
>> tracker.

>
>
> see your own b), I wanted to discuss them first.


Fair enough.

> Still, when I first tried out the Template class, I immediately stumbled
> over the fact that the substitute methods always return strings, and
> never Template objects. While it makes sense for their primary purpose,
> it totally restricts the usage of this module to one-show operations.


Except that it is easy to turn a string into a template using a constructor. . .

However, now that I think more about your 'partial_substitute' method it does
make sense as an alternative constructor:

@classmethod
def from_template(*args, **kwds):
cls, orig = args[:2]
return cls(orig.safe_substitute(*args[2:], **kwds))

Usable as:
new_templ = Template.from_template(orig_templ, *args, **kwds)

Versus:
new_templ = Template(orig_templ.safe_substitute(*args, **kwds))

Mutating the template in place seems questionable. Rebinding the name with the
above constructor would be more appropriate, IMO.

Hmm - I'm unconvinced, but it's probably still worth posting a patch and sending
it in Barry Warsaw's direction.

Cheers,
Nick.

--
Nick Coghlan | (E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Partial 1.0 - Partial classes for Python Thomas Heller Python 13 02-08-2007 08:12 PM
Re: Partial 1.0 - Partial classes for Python J. Clifford Dyer Python 0 02-08-2007 05:29 PM
newbie: allow deny vs deny allow Jeff ASP .Net 2 09-19-2006 02:12 AM
Why? Partial Class within a Partial class Billy ASP .Net 2 02-01-2006 09:10 AM
Location element in the Web.config file. Allow System Admin whole directory, allow others specific page Ryan Taylor ASP .Net Security 1 09-09-2004 06:52 PM



Advertisments