Re: [Python-ideas] [Python-Dev] User conversions in custom string.Formatter
Well, let's discuss in python-ideas.
I mean my custom formatter like:
class Formatter(string.Formatter):
def convert_field(self, value, conversion):
if 'q' == conversion:
return qname(value)
elif 't' == conversion:
return format_datetime(value)
else:
return super(Formatter, self).convert_field(value, conversion)
I can use it like:
s = "{0!q}, {1!t}".format(a, b)
!q and !t are my extensions to formatter conversions. It's good enough
but I prefer to see
s = "{0!qname}, {1!time}".format(a, b)
or something like that. It cannot break any existing rule (you still
can use "{0!qname:<20}" for example) but you can
use more readable names for converters.
There are only changed file is Objects/stringlib/string_format.h as I
can see (I don't count documentation and unitests).
On Wed, Mar 16, 2011 at 2:25 AM, Eric Smith
On 03/15/2011 08:07 PM, Andrew Svetlov wrote:
As PEP 3101 http://www.python.org/dev/peps/pep-3101/ says (and current Python does) user can specify conversions like "{0!s}". In custom formatters (derived from string.Formatter) he can override convert_field method to add custom conversions.
I experimented with that last month and found it very convenient.
From my perspective custom conversions are very close to 'filters'
from html template engines (jinja2, mako, django etc). While I like to see custom conversions simple and easy I don't wan't to bring 'cascading' and 'parametrization' to standard formatting.
But why don't relax spec a bit and allow to use any word (with 'identifier' semantix, I mean str.isidentifier()) as conversion name? Proposed update doesn't make any backward incompatibility (both python and public C API are not changed), it's clean and obvious. And it's very funny to use words instead single characters for custom user-specific conversions.
What do you think?
This should be in python-ideas, but I don't know if Andrew is subscribed, so I'll reply here until it gets out of control.
Andrew: Could you give a code sample of what you'd like to see?
Eric. _______________________________________________ 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/andrew.svetlov%40gmail.com
On 3/15/2011 8:41 PM, Andrew Svetlov wrote:
Well, let's discuss in python-ideas.
I mean my custom formatter like:
class Formatter(string.Formatter): def convert_field(self, value, conversion): if 'q' == conversion: return qname(value) elif 't' == conversion: return format_datetime(value) else: return super(Formatter, self).convert_field(value, conversion)
I can use it like:
s = "{0!q}, {1!t}".format(a, b)
That doesn't work. You need to do: s = Formatter().format("{0!q}, {1!t}", a, b) There's no way to extend the built in str.format to include user defined conversions.
!q and !t are my extensions to formatter conversions. It's good enough but I prefer to see
s = "{0!qname}, {1!time}".format(a, b)
or something like that. It cannot break any existing rule (you still can use "{0!qname:<20}" for example) but you can use more readable names for converters.
There are only changed file is Objects/stringlib/string_format.h as I can see (I don't count documentation and unitests).
Are you looking for those specific conversions, or some general purpose extension mechanism? I don't think a general purpose mechanism is worth the hassle, and I don't think your specific conversions are worth adding (not that I understand what they'd do, maybe you can convince everyone they're worthwhile). Note that you can get some of what you want by specifying a wrapper class that overrides __repr__ and doing: s = "{0!r}, {1!r}".format(QnameWrapper(a), DatetimeFormatWrapper(b))
participants (2)
-
Andrew Svetlov
-
Eric Smith