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


Groups > comp.lang.python > #22475

Re: string interpolation for python

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <lanyjie@yahoo.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; 'escape': 0.07; 'executable': 0.07; 'expressions': 0.07; 'objects,': 0.07; 'parser': 0.07; 'strings.': 0.07; 'suppose': 0.07; 'python': 0.09; 'curve': 0.09; 'formatting': 0.09; 'notation': 0.09; 'objects.': 0.09; 'page?': 0.09; 'skip:k 40': 0.09; 'subject:string': 0.09; 'subject:python': 0.11; '>>>': 0.14; 'essential': 0.14; 'to:name :python-list@python.org': 0.14; 'benefit.': 0.16; 'eval': 0.16; 'from:addr:lanyjie': 0.16; 'from:name:yingjie lan': 0.16; 'performance,': 0.16; 'received:124.205': 0.16; 'reply- to:addr:lanyjie': 0.16; 'reply-to:name:yingjie lan': 0.16; 'syntax.': 0.16; 'yingjie': 0.16; 'have:': 0.17; 'string': 0.19; 'least': 0.20; 'maybe': 0.22; 'header:In-Reply-To:1': 0.22; 'otherwise,': 0.23; 'that.': 0.24; 'cheers,': 0.26; 'right.': 0.26; 'skip:> 10': 0.27; 'translated': 0.27; 'seems': 0.27; 'common': 0.28; 'consequence': 0.28; 'issues.': 0.28; 'strings,': 0.28; 'subject:skip:i 10': 0.28; 'though.': 0.28; 'turns': 0.28; 'reply-to:addr:yahoo.com': 0.28; "i'm": 0.29; 'embedded': 0.30; 'list.': 0.31; 'possibly': 0.31; "aren't": 0.32; 'code': 0.32; 'could': 0.32; 'really': 0.33; 'method': 0.33; 'next': 0.34; 'meaning': 0.34; 'mean': 0.34; 'to:addr:python-list': 0.35; 'done': 0.35; 'list': 0.35; 'paying': 0.35; "skip:' 20": 0.35; 'skip:m 40': 0.35; 'does': 0.36; 'editor': 0.36; 'being': 0.36; 'ones': 0.37; 'but': 0.37; 'dynamic': 0.37; 'object': 0.37; 'subject:: ': 0.37; 'possible.': 0.37; 'skip:s 20': 0.37; 'performance': 0.38; 'too': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'allow': 0.40; '8bit%:8': 0.40; 'past': 0.40; 'learning': 0.60; '(that': 0.62; 'worth': 0.62; 'kind': 0.63; 'more': 0.63; 'here': 0.64; 'header:Reply-To:1': 0.67; 'taking': 0.68; 'lose': 0.71; 'everybody': 0.78; 'absolutely': 0.84; 'complexity': 0.84; 'difference.': 0.84; 'distinguish': 0.84; 'is\xa0': 0.84; '3.3': 0.91; 'received:98.138.226': 0.91; 'convinced': 0.93
X-Yahoo-Newman-Property ymail-3
X-Yahoo-Newman-Id 973194.17742.bm@omp1066.mail.ne1.yahoo.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1333381766; bh=9/ctMPUT6tG0fn+dYomyuhlyUlVRl7aFRw+T6t7K0Ec=; h=X-YMail-OSG:Received:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=qYM2x3HtkB1anLHuaGnezk9eQ2OWlIS3I7OhZgQE6RHs39hSiIRLmmY8GWPH02ndzioHkrKNk8UN35Se02I3hHP1CVrOQA/hZ4faE+7NlbE5I7SCVweVD4rM61EZ6jecCC8yt71kCTlKkl/jpn3lw7zBf4WDRx0gWdujKCicVfc=
DomainKey-Signature a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=3MU68kBLR3nor0PaeP8TSf5bqJ+nICgyoNz+6KHT0npSQqPyAe2Qg/HrQ2eGTA/as0okfP+qppaPNqmdf7u+350Pir/I/QPjwFLGjQl/qLS16lc/bemcE+GQMWL0RThAmLWhaJ/rt/8n5pXsiZYc7OpAxGtWCnOzsyMz4ZSb9sE=;
X-YMail-OSG 9LZxTpQVM1kbs_kgX8nQ.ixzVxQ7tvSl13BotVwludTH8uY FJ4WhYyHGNnONKR1qoJm2_SeQu7qOu0FC6IswUiNm0jqlXZBaXarpdGjdmuR gjUcCwNv1aEATF.P5AzwQAk8h.Gi6nsv8utYsjrDPAkCCP.ebYlRcTzZYlkP KZJEjkxf6sZik1a8O_IIP_ivZq3kE2GKkqkbYH9.LDfxlObFuvV7gcYzSkt5 ookiv6DqCto.9C3U5JlEwrAlVlSZTGhSO_qs1atMmVLjm0Tkw3BOUj7sv9fj FucxWhyduVqG1GHAt2FHUYGYtxqTHeOWeAsFBbZZ2z25AYRJwIwjVFFkg0dh UPUpp58dorPVrXDTRP7TYl2xtXqU6khnwOcVfCdMDiKsdDA8MTJLfieE_Hdb aC1FYE6.fcy0StFFZO28.dQk8w1NZOZKw36OE.ff3qDizDG5.mKHf4WkGn0g svcOZ.jsY5_1o8zp8dW4mJ_s-
X-Mailer YahooMailWebService/0.8.117.340979
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>
Date Mon, 2 Apr 2012 08:49:26 -0700 (PDT)
From Yingjie Lan <lanyjie@yahoo.com>
Subject Re: string interpolation for python
To "python-list@python.org" <python-list@python.org>
In-Reply-To <CAPTjJmoH9uCOTQ2M6fhQjUnydAGGBhhFo+++2i9HbUEFg5KJ2g@mail.gmail.com>
MIME-Version 1.0
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
Reply-To Yingjie Lan <lanyjie@yahoo.com>
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.1222.1333381885.3037.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1333381885 news.xs4all.nl 6852 [2001:888:2000:d::a6]:39705
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22475

Show key headers only | View raw


> Right, meaning that both have the same issues 
> of performance, need for

> str(), etc. There's absolutely no difference.


OK, performance. Here is a new solution:
 
Suppose we have a new string method
    str.format_join([...])
taking a list of strings and objects,
with even-indexed ones being strings,
odd-indexed ones being objects.
Each even-indexed string *ends* with a formatting
specification for the next object in the list.
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.


> And no benefit. You lose out on syntax highlighting 
> in your editor and gain nothing.

Gain: readability, terseness, hassle-free, and possibly
better performance if done right.

Syntax highlighting: can be done more creatively.
For dynamic strings, string parts are like normal 
strings, but the embedded expressions are like
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.

> It's powerful only if you use eval to allow full expression syntax.
> Otherwise, what does it have above str.format()?


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

> You may well be able to get past the compatibility issues. I'm not yet
> convinced that the new syntax is worth it, but it may be possible.
> 
> 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?

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.



Cheers,
Yingjie

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