[Python-3000] String formating operations in python 3k

Nick Coghlan ncoghlan at gmail.com
Mon Apr 3 23:40:21 CEST 2006


Ian Bicking wrote:
> Barry Warsaw wrote:
>> On Mon, 2006-04-03 at 23:43 +1000, Nick Coghlan wrote:
>>
>>
>>> What do you think of a "format" builtin function that accepts the 
>>> format as the first argument (similar to printf).
>>>
>>> The version on my harddrive permits positional arguments via $1, $2, 
>>> etc, as well as string formatting (by sticking the format code in 
>>> square brackets between the $ and the identifier). Keyword arguments 
>>> still work, naturally.
>>>
>>> And if you don't want formatting, you just leave out the square 
>>> brackets.
>>
>>
>> I'm not totally sure I would need a builtin.  If I look at the two
>> sources of $-strings in an app like Mailman, I'll see 1) literal human
>> readable/translatable strings in the source code, 2) human entered
>> strings that come from a web form.
>>
>> In the first case, all string formatting will funnel through one
>> bottleneck function, which will do the catalog lookup, frame variable
>> discovery, and substitution all in one fell swoop.  So there, the
>> builtin doesn't buy you much convenience.
> 
> Well, error messages are a common place I use %.  So:
> 
> assert path.startswith(prefix), (
>     "%r should start with %r" % (path, prefix))
> assert path.startswith(prefix), (
>     $"${repr(path)} should start with ${repr(prefix)}")
> assert path.startswith(prefix), (
>     "$path should start with $prefix".substitute(
>     path=repr(path), prefix=repr(prefix))
> 
> 
> The second example assumes that you can include full expressions, 
> otherwise that example would start looking really unpleasant.  As it is, 
> the first example still looks more-or-less the best, and it's too bad 
> $-based substitution doesn't include it.  I'm not sure how it would 
> include it, unless there was something like $=path, or $<path>, or 
> $`path` or something.  I dunno, none of those are very appealing.

Given a 'format' (or 'string.format') function that permits format specifiers 
and positional arguments*, the following would all be options:

# My favourite
assert path.startswith(prefix), (
     format("$[r]1 should start with $[r]2", path, prefix))

# Most self-explanatory
assert path.startswith(prefix), (
     format("$1 should start with $2", repr(path), repr(prefix)))

# A couple of wordier options
assert path.startswith(prefix), (
     format("$path should start with $prefix",
             path=repr(path), prefix=repr(prefix)))
assert path.startswith(prefix), (
     format("$[r]path should start with $[r]prefix", **locals())

Cheers,
Nick.

* (such a beast simply needs to take a string as the first argument, and 
convert it to a string.Template variant which has an extra optional field in 
the regex for a %-style format specifier, as well as using enumerate() on the 
arglist if no keywords are given)

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list