Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22552

Re: string interpolation for python

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; '(python': 0.05; 'argument': 0.05; 'things.': 0.05; 'def': 0.06; 'escape': 0.07; 'executable': 0.07; 'expressions': 0.07; 'feature.': 0.07; 'function,': 0.07; 'indices': 0.07; 'parser': 0.07; 'strings.': 0.07; 'suppose': 0.07; 'python': 0.09; '*is*': 0.09; '>>>>': 0.09; 'curve': 0.09; 'format:': 0.09; 'notation': 0.09; 'operator,': 0.09; 'page?': 0.09; 'skip:k 40': 0.09; 'subject:string': 0.09; 'symbols': 0.09; '(in': 0.11; 'subject:python': 0.11; 'essential': 0.14; 'arguments:': 0.16; 'eval': 0.16; 'expression.': 0.16; 'expressions,': 0.16; 'expressions.': 0.16; 'formatting.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'magic': 0.16; 'prefer.': 0.16; 'printf': 0.16; 'someone,': 0.16; 'suggested,': 0.16; 'windows).': 0.16; 'yingjie': 0.16; '{0}': 0.16; 'language': 0.16; 'have:': 0.17; 'string,': 0.17; 'examples': 0.18; '(or': 0.18; 'code,': 0.19; 'string': 0.19; 'wrote:': 0.19; 'least': 0.20; '3.2': 0.21; 'element': 0.21; 'explicit': 0.21; 'keys': 0.21; 'maybe': 0.22; 'header:In-Reply- To:1': 0.22; 'bit': 0.22; 'case,': 0.23; 'thus': 0.23; 'together.': 0.23; 'that.': 0.24; 'pass': 0.24; 'message- id:@mail.gmail.com': 0.24; 'function': 0.26; 'skip:> 10': 0.27; 'translated': 0.27; 'seems': 0.27; 'common': 0.28; 'values': 0.28; 'consequence': 0.28; "d'aprano": 0.28; 'once,': 0.28; 'steven': 0.28; 'strings,': 0.28; 'subject:skip:i 10': 0.28; 'though.': 0.28; 'turns': 0.28; 'yes.': 0.28; "i'm": 0.29; 'embedded': 0.30; 'query': 0.30; 'received:209.85.210.174': 0.30; 'received:mail- iy0-f174.google.com': 0.30; 'initial': 0.30; '(and': 0.30; 'chris': 0.31; 'point': 0.31; 'array': 0.31; 'itself': 0.31; 'passed': 0.31; "can't": 0.32; 'received:209.85': 0.32; 'received:google.com': 0.32; "aren't": 0.32; 'probably': 0.32; 'code': 0.32; 'could': 0.32; 'actual': 0.33; 'really': 0.33; 'received:209.85.210': 0.33; 'handle': 0.33; 'something': 0.34; 'am,': 0.34; 'mapping': 0.34; 'version': 0.34; 'mean': 0.34; 'to:addr:python-list': 0.35; 'possible': 0.35; 'done': 0.35; 'received:209': 0.35; 'useful': 0.35; 'should': 0.35; 'end': 0.35; 'paying': 0.35; "skip:' 20": 0.35; 'skip:m 40': 0.35; 'takes': 0.36; 'them,': 0.36; 'doing': 0.36; 'being': 0.36; 'several': 0.37; 'but': 0.37; 'why': 0.37; 'dynamic': 0.37; 'subject:: ': 0.37; 'it.': 0.38; 'some': 0.39; 'too': 0.39; 'to:addr:python.org': 0.39; '(that': 0.62; 'is.': 0.62; 'skip:$ 10': 0.62; 'skip:y 20': 0.62; '2012': 0.62; 'kind': 0.63; 'more': 0.63; 'legal': 0.64; 'taking': 0.68; 'hey,': 0.72; 'everybody': 0.78; 'actually,': 0.84; 'complexity': 0.84; 'distinguish': 0.84; 'everything.': 0.84; '3.3': 0.91; 'everything,': 0.91; 'magical': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=N9oLdaD3+dXFfR4a9wKRD9SoM+R1z5X7SJyOl2K/XV4=; b=cskOxn4NL4QQDIHXOxWnmKJAYUjgwhOAdCeDeCdxLvKb9NLJcmeiZ2DpSEo0irfYF+ WG+4usiU94XUN0qEI6BBrMoqJC1O6OJKiRskrFL7AFxd1AnRzOECwRYLgre+5GgWz9RL YqaDwXS4DqXpP29m132oxWN41SdZaJcW1+TbF3Z3010oMiV0ufkFxfxQCWeoWvw63YzM JT9nsAH9CJLz1jc3ngf3YM8CzJpgawSxoatHOhMmfwRweuYGOa52G4foT4YWZHnnFlk9 9m2OPFzzZ/8bHmyA4YW5qQUsfbGRngswSd+t/Cqj7fwk72q0qQQj7KJwDda/rVvn8dUn 5/Fw==
MIME-Version 1.0
In-Reply-To <1333381766.14038.YahooMailNeo@web121504.mail.ne1.yahoo.com>
References <CABgq=FyhR+Ldujj3YKRBpVXRVoeoayuXZviUUcNx-gXnFiHLSw@mail.gmail.com> <CACoeR0xV=28j6rFHm1ep3gzH9C0sbDqyxRRwX-JrVm3G+rrXww@mail.gmail.com> <1333174946.18436.YahooMailNeo@web121506.mail.ne1.yahoo.com> <jl6ma0$ptg$1@dough.gmane.org> <mailman.1200.1333352587.3037.python-list@python.org> <4f7962b0$0$29981$c3e8da3$5496439d@news.astraweb.com> <1333357906.6147.YahooMailNeo@web121503.mail.ne1.yahoo.com> <CAPTjJmqtMhS8jp3D+SNiOAp-v9QzP5tkQzCqmO_Re7oLepOWwQ@mail.gmail.com> <1333367201.47465.YahooMailNeo@web121506.mail.ne1.yahoo.com> <CAPTjJmoH9uCOTQ2M6fhQjUnydAGGBhhFo+++2i9HbUEFg5KJ2g@mail.gmail.com> <1333381766.14038.YahooMailNeo@web121504.mail.ne1.yahoo.com>
Date Tue, 3 Apr 2012 08:38:13 +1000
Subject Re: string interpolation for python
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1243.1333406295.3037.python-list@python.org> (permalink)
Lines 108
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1333406295 news.xs4all.nl 6908 [2001:888:2000:d::a6]:50201
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22552

Show key headers only | View raw


On Tue, Apr 3, 2012 at 4:56 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Tue, 03 Apr 2012 00:58:38 +1000, Chris Angelico wrote:
>
>> I can't find a way to use an argument more than once,
>> without switching to "dictionary mode" and using keys for everything.
>
> Ack.
>
> In this case, you can use format:
>
>>>> "Hello {0}, what's up with {1}? Hey, {0} I'm speaking to you!".format
> ('George', 'Melissa')
> "Hello George, what's up with Melissa? Hey, George I'm speaking to you!"

Yes, it's possible with that version but not with % formatting. (Is
your "Ack" short for "Acknowledgement", or is it an ejaculation of
disgust?)

On Tue, Apr 3, 2012 at 1:49 AM, Yingjie Lan <lanyjie@yahoo.com> wrote:
> Then we can have:
>>>> d"sin($x$) = $ sin(x):0.3f $"
> get translated to:
>>>> ''.format_join(["sin(%s",x,") = %0.3f", sin(x)])
> This seems to be at least as good in performance.

Or don't bother with the initial string, and simply pass everything as
arguments:

def yingjie_format(*args):
    it=iter(args)
    return ''.join(s%next(it,None) for s in it)
yingjie_format("sin(%s",x,") = %0.3f", sin(x))

Tested and working (Python 3.2 on Windows). Okay, the generator's a
bit cryptic, but you could do the same with a more explicit loop if
you prefer.

> Syntax highlighting: can be done more creatively.
> For dynamic strings, string parts are like normal
> strings, but the embedded expressions are like
> normal expressions :)

So if they're exactly like normal expressions, why not simply use
normal expressions?

>> sprintf("UPDATE tablename SET modified=now()%{,%s=:%[0]s%} WHERE
>> key=%d",array_of_field_names,primary_key_value)
>> --> "UPDATE tablename SET modified=now(),foo=:foo,bar=:bar,quux=:quux
>> WHERE key=1234"
>>
>> You're still paying for no complexity you aren't actually using.
>> It's clear and readable.
>
> You are really good at that. Maybe not everybody is as
> experience as you, and I suppose the learning curve is
> kind of hard to climb.

Yes, it takes some learning to use it. But that's true of everything,
no less of your magical string interpolation. My point is that simple
examples should be (and are, with printf formatting) simple, such that
you only get those more complicated format strings when you're
actually doing a complicated job (in that case, taking each element of
an array and using it twice - actually, it was taking the indices of a
mapping that would end up being passed to the DB query function, thus
providing values to the :foo :bar variables).

> Those expressions are embedded, you don't need eval()
> to have the result though. Are we on the same page?

I can see three implementation paths:

1) Language feature. It really *is* just an expression. There's no way
that a user can provide them, so there's actually no similarity to
eval. But this requires that Python itself handle things.

2) Precompiler. It *becomes* an expression. Again, perfectly safe,
although I don't know how useful this really is.

3) Functoin. As several have suggested, you could do some magic and
use d("this is a $dollar$ $interpolated$ string") to implement. For
this, you *will* need eval (or something like it).

>> Here's a recommendation: Write a parser for your notation that turns
>> it into executable Python code (that is, executable in Python 3.3
>> without any d"..." support).
>
> You mean a translator?

Yes. It translates your dollar-strings into something that's legal
Python 3.3 syntax - either calls to a function like I provided above,
or actual embedded expressions.

> The syntax is essential for compatibility.
> We must distinguish dynamic strings from common strings.
> They will live peacefully together.
> (escaping the '$' in normal strings breaks compatibility,
> and the consequence of forgetting to escape could be
> disastrous, so definitely not an option).
>
> May be d" is too tiny, $"..." is easier to pick out.

I don't like the use of symbols like that; can someone, glancing at
your code, tell whether $ is an operator, a name, or something else?
The original d is probably better for that.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 00:39 -0700
  Re: string interpolation for python Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-04-02 11:01 +0300
  Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 08:26 +0000
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-02 18:47 +1000
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 02:11 -0700
      Re: string interpolation for python Duncan Booth <duncan.booth@invalid.invalid> - 2012-04-02 10:19 +0000
      Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 11:54 +0000
        Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 05:40 -0700
          Re: string interpolation for python Laurent Claessens <moky.math@gmail.com> - 2012-04-02 15:02 +0200
            Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 07:25 -0700
            Re: Re: string interpolation for python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-04-02 15:36 -0500
          Re: string interpolation for python mwilson@the-wire.com - 2012-04-02 10:46 -0400
            Re: string interpolation for python mwilson@the-wire.com - 2012-04-02 11:34 -0400
              Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 09:02 -0700
      Re: string interpolation for python rusi <rustompmody@gmail.com> - 2012-04-02 06:04 -0700
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 02:17 -0700
      Re: string interpolation for python alex23 <wuwei23@gmail.com> - 2012-04-02 22:47 -0700
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-02 19:56 +1000
    Re: string interpolation for python Chris Rebert <clp2@rebertia.com> - 2012-04-02 03:23 -0700
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 04:46 -0700
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 05:00 -0700
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-03 00:58 +1000
      Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 18:56 +0000
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 08:49 -0700
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-03 08:38 +1000
      Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 23:18 +0000
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 18:57 -0700

csiph-web