str.format_from_mapping
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d). I like the idea. It's particularly handy when converting from %-formatting. Eric.
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith <eric@trueblade.com> wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)?
2010/10/31 Antoine Pitrou <solipsis@pitrou.net>:
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith <eric@trueblade.com> wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)?
It allows arbitrary mappings. -- Regards, Benjamin
On Sun, 31 Oct 2010 16:02:08 -0500 Benjamin Peterson <benjamin@python.org> wrote:
2010/10/31 Antoine Pitrou <solipsis@pitrou.net>:
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith <eric@trueblade.com> wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)?
It allows arbitrary mappings.
Ah, yes, sorry. I should have read the issue before posting. It's a bit embarassing to add a method for that, though, while formatting regular dicts doesn't need it. Regards Antoine.
On 10/31/2010 2:02 PM, Benjamin Peterson wrote:
2010/10/31 Antoine Pitrou<solipsis@pitrou.net>:
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith<eric@trueblade.com> wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)? It allows arbitrary mappings.
Other than the language moratorium, why are arbitrary mappings not allowed for the (**d) syntax?
On 10/31/2010 6:28 PM, Glenn Linderman wrote:
On 10/31/2010 2:02 PM, Benjamin Peterson wrote:
2010/10/31 Antoine Pitrou<solipsis@pitrou.net>:
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith<eric@trueblade.com> wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)? It allows arbitrary mappings.
Other than the language moratorium, why are arbitrary mappings not allowed for the (**d) syntax?
An arbitrary mapping would be converted to a dict. That disallows using a subclass with __missing__ defined, among other things. See the discussion in the issue.
On 10/31/2010 3:32 PM, Eric Smith wrote:
On 10/31/2010 6:28 PM, Glenn Linderman wrote:
On 10/31/2010 2:02 PM, Benjamin Peterson wrote:
2010/10/31 Antoine Pitrou<solipsis@pitrou.net>:
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith<eric@trueblade.com> wrote:
> What are your thoughts on adding a str.format_from_mapping (or similar > name, maybe the suggested "format_map") to 3.2? See > http://bugs.python.org/issue6081 . This method would be similar to > "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), > but of course would use str.format syntax: "{foo} > {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)? It allows arbitrary mappings.
Other than the language moratorium, why are arbitrary mappings not allowed for the (**d) syntax?
An arbitrary mapping would be converted to a dict.
Yes, but why convert? I suppose it has something to do with the case where there are named parameters, in addition to a **kwargs formal, and the mixing and matching that happens as a result.
2010/10/31 Glenn Linderman <v+python@g.nevcal.com>:
On 10/31/2010 3:32 PM, Eric Smith wrote:
On 10/31/2010 6:28 PM, Glenn Linderman wrote:
On 10/31/2010 2:02 PM, Benjamin Peterson wrote:
2010/10/31 Antoine Pitrou<solipsis@pitrou.net>:
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith<eric@trueblade.com> wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)?
It allows arbitrary mappings.
Other than the language moratorium, why are arbitrary mappings not allowed for the (**d) syntax?
An arbitrary mapping would be converted to a dict.
Yes, but why convert?
Because callees always get a dictionary *copy* of the arguments. -- Regards, Benjamin
2010/10/31 Glenn Linderman <v+python@g.nevcal.com>:
On 10/31/2010 2:02 PM, Benjamin Peterson wrote:
2010/10/31 Antoine Pitrou <solipsis@pitrou.net>:
On Sun, 31 Oct 2010 16:39:44 -0400 Eric Smith <eric@trueblade.com> wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I must be missing something, but what's the difference with XXX.format(**d)?
It allows arbitrary mappings.
Other than the language moratorium, why are arbitrary mappings not allowed for the (**d) syntax?
They are; they're just copied into a dictionary. -- Regards, Benjamin
On Mon, Nov 1, 2010 at 10:53 AM, Barry Warsaw <barry@python.org> wrote:
On Oct 31, 2010, at 04:39 PM, Eric Smith wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2?
+1 for the shorter name.
+1 for a format_map() method that takes a single mapping argument (Eric's patch on the issue). -1 for the most recent patch attached to that issue that allows further positional arguments after the mapping object. (Raymond mentioned it on the issue, but I'll mention it here as well: this addition would fall under the "case-by-case exemption" clause in the moratorium PEP, since it adds a new method to a builtin type) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On 10/31/10 4:39 PM, Eric Smith wrote:
What are your thoughts on adding a str.format_from_mapping (or similar name, maybe the suggested "format_map") to 3.2? See http://bugs.python.org/issue6081 . This method would be similar to "%(foo)s %(bar)s" % d, where d is a dict (or rather any mapping object), but of course would use str.format syntax: "{foo} {bar}".format_from_mapping(d).
I like the idea. It's particularly handy when converting from %-formatting.
Eric.
I've updated the issue with tests, minimal docs, and a name change to str.format_map. Having heard no objections and some support, I'll commit this shortly. -- Eric.
participants (6)
-
Antoine Pitrou -
Barry Warsaw -
Benjamin Peterson -
Eric Smith -
Glenn Linderman -
Nick Coghlan