<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 7, 2015 at 7:31 AM, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 7 August 2015 at 22:12, Eric V. Smith <<a href="mailto:eric@trueblade.com">eric@trueblade.com</a>> wrote:<br>
> On 8/7/2015 7:52 AM, Eric V. Smith wrote:<br>
>> So (reverting to Python syntax, with the f-string syntax), in addition<br>
>> to converting directly to a string, there's a way to go from:<br>
>><br>
>> f'abc{expr1:spec1}def{expr2:spec2}ghi'<br>
>><br>
>> to:<br>
>><br>
>> ('abc{0:spec1}def{1:spec2}ghi', (value-of-expr1, value-of-expr2))<br>
>><br>
>> The general idea is that you now have access to an i18n-able string, and<br>
>> the values of the embedded expressions as they were evaluated "in situ"<br>
>> where the f-string literal was present in the source code.<br>
>> Y<br>
>> ou can imagine the f-string above evaluating to a call to:<br>
>><br>
>> __interpolate__('abc{0:spec1}def{1:spec2}ghi', (value-of-expr1,<br>
>> value-of-expr2))<br>
>><br>
>> The default implementation of __interpolate__ would be:<br>
>><br>
>> def __interpolate__(fmt_str, values):<br>
>> return fmt_str.format(*values)<br>
><br>
> I should add that it's unfortunate that this builds a string for<br>
> str.format() to use. The f-string ast generator goes through a lot of<br>
> hassle to parse the f-string and extract the parts. For it to then build<br>
> another string that str.format would have to immediately parse again<br>
> seems like a waste.<br>
><br>
> My current implementation of f-strings would take the original f-string<br>
> above and convert it to:<br>
><br>
> ''.join(['abc', expr1.__format__('spec1'), 'def',<br>
> expr2.__format__(spec2), 'ghi'])<br>
><br>
> Which avoids re-parsing anything: it's just normal function calls.<br>
> Making __interpolate__ take a tuple of literals and a tuple of (value,<br>
> fmt_str) tuples seems like giant hassle to internationalize, but it<br>
> would be more efficient in the normal case.<br>
<br>
</div></div>Perhaps we could use a variant of the string.Formatter.parse iterator<br>
format: <a href="https://docs.python.org/3/library/string.html#string.Formatter.parse" rel="noreferrer" target="_blank">https://docs.python.org/3/library/string.html#string.Formatter.parse</a><br>
?<br>
<br>
If the first arg was a pre-parsed format_iter rather than a format<br>
string, then the default interpolator might look something like:<br>
<br>
_converter = string.Formatter().convert_field<br>
<br>
def __interpolate__(format_iter, expressions, values):<br>
template_parts = []<br>
# field_num, rather than field_name, for speed reasons<br>
for literal_text, field_num, format_spec, conversion in format_iter:<br>
template_parts.append(literal_text)<br>
if field_num is not None:<br>
value = values[field_num]<br>
if conversion:<br>
value = _converter(value, conversion)<br>
field_str = format(value, format_spec)<br>
template_parts.append(field_str)<br>
return "".join(template_parts)<br></blockquote><div><br></div><div>Would __interpolate__ then be an operator / protocol,</div><div>or just a method of an r-string?</div><div><br></div><div>Benefits / (other use cases):</div><div>* implicit/explicit [shell,shlex,[SQL, SPARQL]] quoting (e.g. "" + repr(x)[1:-1] + "")<br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
My last il8n example called string.Formatter.parse() anyway, so it<br>
could readily be adapted to this model.<br>
<span class="im HOEnZb"><br>
Cheers,<br>
Nick.<br>
<br>
--<br>
Nick Coghlan | <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a> | Brisbane, Australia<br>
</span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div></div>