[Python-ideas] Template.match or similar (was Re: Where/how to propose...)

Terry Reedy tjreedy at udel.edu
Tue Oct 14 05:50:09 CEST 2008


Joe Strout wrote:
> On Oct 13, 2008, at 2:43 PM, Terry Reedy wrote:
> 
>> Given that I have never used the Template class and am using 3.0 
>> str.format, that I believe the former was the bridge that led to the 
>> latter, and am not enamored of writing REs, I think adding str.match 
>> would be a great idea.  To the extent possible,
>>  s  == form.format(form.match(s)) and
>>  args == form.match(form.format(args).
>> Note that args can either be a sequence or mapping.
>>
>> This might encourage more people to switch faster from % to .format. 
>> Given that Guido wants this to happen, he might look favorably
> 
> Well, I'm all for that, but I've never used the 3.0 str.format, and find 
> the documentation on it somewhat unenlightening.

You probably read too much of it.  I probably read it all but only 
learned the basics, intending to review more on a need-to-know basis.

> Can you mock up an example of how this might work?

My ideas and opinions.

1. Start with the simplest possible tasks.

temp_p1 = 'Testing {0}'
temp_k1 = 'Testing {word}'
p1 = ('matches',) # or perhaps use list instead of tuple
k1 = {'word':'matches'}
text1 = 'Testing matches'
form_p1 = temp_p1.format(*p1)
form_k1 = temp_k1.format(**k1)
print(form_p1 == form_k1==text1)
#prints True today with 3.0c1

#tests
temp_p1.match(text1) == p1
temp_k1.match(text1) == k1

(Come to think of it, easiest would have no literal text, but I already 
wrote the above.)

Now, write the simplest code that makes these tests pass.
Easy. Use str.startswith() to match the literal, non-field text.

Add
text1e='Bad matches'
and consider what exception to raise on non-literal match.

2. Now complicate the task.  Add text after the substitution field.
Write test and then code with str.endswith
Still do not need re, but might also want to do re version.

3. Add more fields: two positional, two keyword, one of each.
Probably need to 'refactor' and use re.

4,5,6.  Add field attributes, int formatting, and float formatting, 
depending on what translated to re's.

> 
> In simple form it looks very similar to Template.substitute, but it 
> leaves lots of room for getting not-so-simple.  Does that present a 
> problem, in that a great number of format strings would not be useable 
> in reverse?

Not to me.  The most common substitute is straight string interpolation. 
  I suspect that that and int and float field formatting will cover 80% 
of use cases.  For others... 'use the re module'.

 >  Or would we simply say: if you want to use the reverse
> (match) operation, then you have to restrict yourself to simple named 
> fields?

If necessary, but I suspect more is reasonably possible.  But yes, 'you 
have to use a subset of possible formats'.

   Or, would we define a slightly different syntax for use with
> match, that lets you specify numeric conversions or whatever, and give 
> up the idea that these are truly inverse operations?

No, not yet another syntax ;-).

Terry Jan Reedy




More information about the Python-ideas mailing list