[issue46307] string.Template should allow inspection of identifiers

2022-01-12 Thread Alex Waygood


Change by Alex Waygood :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-11 Thread miss-islington


miss-islington  added the comment:


New changeset dce642f24418c58e67fa31a686575c980c31dd37 by Ben Kehoe in branch 
'main':
bpo-46307: Add string.Template.get_identifiers() method (GH-30493)
https://github.com/python/cpython/commit/dce642f24418c58e67fa31a686575c980c31dd37


--
nosy: +miss-islington

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-09 Thread Ben Kehoe

Ben Kehoe  added the comment:

That doesn’t really seem like a Pythonic way of extracting that
information? Nor does it seem like it would be an obvious trick for the
average developer to come up with. A method that provides the information
directly seems useful.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-09 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

The simplest way of collecting template names is to use a defaultdict:

>>> d = collections.defaultdict(str)
>>> string.Template('$a $b').substitute(d)
' '
>>> d.keys()
dict_keys(['a', 'b'])

You can use a custom mapping if you need special handling of absent keys.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-09 Thread Ben Kehoe

Ben Kehoe  added the comment:

The point is to be able to programmatically determine what is needed for a
successful substitute() call. A basic use case for this is better error
messages; calling substitute() with an incomplete mapping will tell you
only the first missing identifier it encounters; if you know all the
identifiers you can raise an error about all the missing identifiers.
Another error handling use case is checking whether the template is valid,
without needing to provide a complete mapping. A use case unrelated to
error handling that I’ve encountered a few times is interactive prompting
for template values, which you can only do if you can get a list of the
identifiers in the template.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-09 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

What are the use cases for this feature?

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-09 Thread Ben Kehoe


Ben Kehoe  added the comment:

Having slept on it, I realized that if I was presenting interactive prompts for 
a template, I would expect the prompts to be in order that the identifiers 
appear in the template. Accordingly, I've updated the PR to maintain ordering.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-08 Thread Ben Kehoe


Ben Kehoe  added the comment:

I opened a PR. By default, it raises an exception if there's an invalid 
identifier; there's a keyword argument raise_on_invalid to control that.

The implementation I have adds them to a set first, which means the order is 
not guaranteed. I'm of two minds about this: if there's a big template, you 
want to gather the identifiers in a set so uniqueness is checked immediately 
and O(1) and without duplication. On the other hand, if templates are never 
very big, you could build a list (in order) and check it, O(N) style, or build 
a duplicate list and set in parallel. Or build a big list and check it at the 
end.

I kind of think ordering doesn't matter? What would someone do with that 
information?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-08 Thread Ben Kehoe


Change by Ben Kehoe :


--
keywords: +patch
pull_requests: +28698
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/30493

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-08 Thread Barry A. Warsaw

Barry A. Warsaw  added the comment:

I think you’re right that the iterator API isn’t very helpful.  I also agree 
that you probably really want to answer the “why identifiers are in this 
template?” question.  As for repeats, there’s two ways to think about it.  You 
could return all the identifiers in the order in which they’re found in the 
template (and you can unique-ify them if you want by passing that list to 
set()).  But maybe you don’t really need that either.

get_identifiers() works for me!

On Jan 8, 2022, at 18:51, Ben Kehoe  wrote:
> 
> Ben Kehoe  added the comment:
> 
> Happy to make a PR! In my mind I had been thinking it would be the 
> get_identifiers() method with the implementation above, returning a list.
> 
> As for __iter__, I'm less clear on what that would look like:
> 
> t = string.Template(...)
> for identifier in t:
>  # what would I do here?
>  # would it include repeats if they appear more than once in the template?
> 
> I guess there are two ways to think about it: one is "what identifiers are in 
> this template?" which I think should return a list with no repeats, which I 
> can then iterate over or check if a value is in it. The other is, "what are 
> the contents of the template?" in the style of string.Formatter.parse().
> 
> Given that string.Template is supposed to be the "simple, no-frills" thing in 
> comparison to string.Formatter, I see less use for the latter option.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-08 Thread Ben Kehoe


Ben Kehoe  added the comment:

Happy to make a PR! In my mind I had been thinking it would be the 
get_identifiers() method with the implementation above, returning a list.

As for __iter__, I'm less clear on what that would look like:

t = string.Template(...)
for identifier in t:
  # what would I do here?
  # would it include repeats if they appear more than once in the template?

I guess there are two ways to think about it: one is "what identifiers are in 
this template?" which I think should return a list with no repeats, which I can 
then iterate over or check if a value is in it. The other is, "what are the 
contents of the template?" in the style of string.Formatter.parse().

Given that string.Template is supposed to be the "simple, no-frills" thing in 
comparison to string.Formatter, I see less use for the latter option.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-08 Thread Barry A. Warsaw


Barry A. Warsaw  added the comment:

I've never personally needed this, but I could see where it could come in handy.

I wonder if __iter__() would be the right API for that?  I wonder then if we 
should also implement __contains__()?

Would you be interested in creating a PR for the feature?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-08 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
nosy: +barry

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46307] string.Template should allow inspection of identifiers

2022-01-08 Thread Ben Kehoe


New submission from Ben Kehoe :

Currently, the only thing that can be done with a string.Template instance and 
a mapping is either attempt to substitute with substitute() and catch a 
KeyError if some identifier has not been provided in the mapping, or substitute 
with safe_substitute() and not know whether all identifiers were provided.

I propose adding a method that returns the identifiers in the template. Because 
the template string and pattern are exposed, this is already possible as a 
separate function:

def get_identifiers(template):
return list(
set(
filter(
lambda v: v is not None,
(mo.group('named') or mo.group('braced') 
 for mo in template.pattern.finditer(template.template))
)
)
)

However, this function is not easy for a user of string.Template to construct 
without learning how the template pattern works (which is documented but 
intended to be learned only when subclassing or modifying id patterns).

As a method on string.Template, this would enable use cases like more 
comprehensive error handling (e.g., finding all missing mapping keys at once) 
or interactive prompting.

--
components: Library (Lib)
messages: 410112
nosy: ben11kehoe
priority: normal
severity: normal
status: open
title: string.Template should allow inspection of identifiers
type: enhancement
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com