[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 ___ ___

[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:

[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. --

[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

[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

[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 ___ ___

[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. --

[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

[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 ___

[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

[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

[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?

[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:

[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