Re: template strings for matching?

2008-10-09 Thread Tino Wildenhain
Joe Strout wrote: Catching up on what's new in Python since I last used it a decade ago, I've just been reading up on template strings. These are pretty cool! However, just as a template string has some advantages over % substitution for building a string, it seems like it would have

Re: template strings for matching?

2008-10-09 Thread skip
Joe templ = Template(The $object in $location falls mainly in the $subloc.) Joe d = templ.match(s) Joe and then d would either by None (if s doesn't match), or a Joe dictionary with values for 'object', 'location', and 'subloc'. Joe But I couldn't find anything like

Re: template strings for matching?

2008-10-09 Thread skip
Tino Yeah, its a bit hard to spot: Tino http://docs.python.org/library/stdtypes.html#string-formatting-operations That shows how to use the template formatting as it currently exists. To my knowledge there is no support for the inverse operation, which is what Joe asked about. Given

Re: template strings for matching?

2008-10-09 Thread Robin Becker
Joe Strout wrote: Catching up on what's new in Python since I last used it a decade ago, I've just been reading up on template strings. These are pretty cool! However, just as a template string has some advantages over % substitution for building a string, it seems like it would have

Re: template strings for matching?

2008-10-09 Thread Paul McGuire
Pyparsing makes building expressions with named fields pretty easy. from pyparsing import Word, alphas wrd = Word(alphas) templ = The + wrd(object) + in + wrd(location) + \ stays mainly in the + wrd(subloc) + . tests = \ The rain in Spain stays mainly in the plain. The snake in

Re: template strings for matching?

2008-10-09 Thread Tino Wildenhain
[EMAIL PROTECTED] wrote: Tino Yeah, its a bit hard to spot: Tino http://docs.python.org/library/stdtypes.html#string-formatting-operations That shows how to use the template formatting as it currently exists. To my knowledge there is no support for the inverse operation, which is

Re: template strings for matching?

2008-10-09 Thread Joe Strout
On Oct 9, 2008, at 7:05 AM, [EMAIL PROTECTED] wrote: Tino http://docs.python.org/library/stdtypes.html#string-formatting-operations That shows how to use the template formatting as it currently exists. To my knowledge there is no support for the inverse operation, which is what Joe

Re: template strings for matching?

2008-10-09 Thread Peter Otten
Joe Strout wrote: Catching up on what's new in Python since I last used it a decade ago, I've just been reading up on template strings. These are pretty cool! I don't think they've gained much traction and expect them to be superseded by PEP 3101 (see

Re: template strings for matching?

2008-10-09 Thread skip
Tino ??? can you elaborate? I don't see the problem. Tino %(foo)s % mapping Joe wants to go in the other direction. Using your example, he wants a function which takes a string and a template string and returns a dict. Here's a concrete example: s = My dog has fleas fmt = My

template strings for matching?

2008-10-09 Thread Joe Strout
Catching up on what's new in Python since I last used it a decade ago, I've just been reading up on template strings. These are pretty cool! However, just as a template string has some advantages over % substitution for building a string, it seems like it would have advantages over

Re: template strings for matching?

2008-10-09 Thread Joe Strout
Wow, this was harder than I thought (at least for a rusty Pythoneer like myself). Here's my stab at an implementation. Remember, the goal is to add a match method to Template which works like Template.substitute, but in reverse: given a string, if that string matches the template, then

Re: template strings for matching?

2008-10-09 Thread MRAB
On Oct 9, 5:20 pm, Joe Strout [EMAIL PROTECTED] wrote: Wow, this was harder than I thought (at least for a rusty Pythoneer   like myself).  Here's my stab at an implementation.  Remember, the   goal is to add a match method to Template which works like   Template.substitute, but in reverse: