Re: [Python-Dev] Extracting variables from string.Template objects
On Fri, Jan 04, 2008, Isaac Morland wrote: On Fri, 4 Jan 2008, Aahz wrote: Also, on a related issue, does it make sense to scan the template string for invalid escape sequences in Template.__init__? For the applications I can imagine of string.Template, I would prefer to get an error upon creating the Template object rather than arbitrarily later when I try to .substitute with it. No, create an is_valid() method at best. I'm curious as to why. Is it to avoid changing the behaviour of existing code (i.e., backwards compatibility), or do you see a design problem with having the Template constructor reject invalid template strings? Mostly the former, though I'm not sure about the performance implications of scanning on instance creation. -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ Weinberg's Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Extracting variables from string.Template objects
I found myself wanting to obtain the variables from a string.Template object. The situation is that part of input consists of a filename template, and I want to make sure that all the parameter values I have are actually represented in the filename template. So: def templateVariables (template): Extract the variable names from a string.Template. Returns a tuple of all variable names found in the template, in the order in which they occur. If an invalid escape sequence occurs, the same error will be raised as if an attempt was made to expand the template. result = [] for match in template.pattern.finditer (template.template): if match.group ('invalid') is not None: # Raises ValueError template._invalid (match) if match.group ('escaped') is not None: continue # The or None should be moot. It is there to ensure equivalent # treatment for an empty 'named' and an empty 'braced'. result.append (match.group ('named') or match.group ('braced') or None) return tuple (result) Note that almost all the work is actually done by calling in to parts of the template object, so I think this should work properly with subclasses and other less common cases. I would like to add this as a method of string.Template, which I think amounts to changing template to self and putting it in the Template class in string.py rather than on its own. If the general idea is approved I would be happy to make a patch. Also, on a related issue, does it make sense to scan the template string for invalid escape sequences in Template.__init__? For the applications I can imagine of string.Template, I would prefer to get an error upon creating the Template object rather than arbitrarily later when I try to .substitute with it. Isaac Morland CSCF Web Guru DC 2554C, x36650WWW Software Specialist ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Extracting variables from string.Template objects
On Fri, Jan 04, 2008, Isaac Morland wrote: I would like to add this as a method of string.Template, which I think amounts to changing template to self and putting it in the Template class in string.py rather than on its own. If the general idea is approved I would be happy to make a patch. Make a patch regardless of whether you get any approval -- someone may later have a different opinion, and writing a patch records your work. Also, on a related issue, does it make sense to scan the template string for invalid escape sequences in Template.__init__? For the applications I can imagine of string.Template, I would prefer to get an error upon creating the Template object rather than arbitrarily later when I try to .substitute with it. No, create an is_valid() method at best. -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ Weinberg's Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com