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


Groups > comp.lang.python > #33104

Re: Format specification mini-language for list joining

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <tobia.conforto@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'argument': 0.04; 'subject:skip:s 10': 0.05; 'alignment': 0.07; 'classes.': 0.07; 'padding': 0.07; 'pretend': 0.07; "object's": 0.09; 'pointless': 0.09; 'subject:language': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'language': 0.14; '(the': 0.15; 'anyways,': 0.16; 'classes)': 0.16; 'cryptic.': 0.16; 'formatting.': 0.16; 'hierarchy': 0.16; 'operators,': 0.16; 'readable': 0.16; 'subject:joining': 0.16; 'wrote:': 0.17; 'define': 0.20; 'do.': 0.21; 'supposed': 0.21; 'defined': 0.22; 'cc:2**0': 0.23; 'somewhere': 0.24; 'thus': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'supported': 0.26; '(as': 0.27; 'object,': 0.27; 'subject:list': 0.28; 'colon': 0.29; 'proposing': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; "skip:' 10": 0.30; 'lists': 0.31; "can't": 0.34; 'changed': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'list': 0.35; 'said,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'method': 0.36; "i'll": 0.36; 'should': 0.36; 'thank': 0.36; 'being': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'object': 0.38; 'subject:-': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'first': 0.61; 'kind': 0.61; "you'll": 0.62; 'back': 0.62; '(that': 0.62; 'customized': 0.64; 'charset:windows-1252': 0.65; 'box,': 0.69; 'lack': 0.71; 'obvious': 0.71; "it'd": 0.84; 'it\x92s': 0.84; 'received:209.85.213.184': 0.91; 'temps': 0.91; 'old.': 0.95
Newsgroups comp.lang.python
Date Sat, 10 Nov 2012 13:11:10 -0800 (PST)
In-Reply-To <mailman.3537.1352567636.27098.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=2.229.207.3; posting-account=f2kSKwoAAACp-DC0NIIB3ltNkhvqmHjp
References <92ada057-5a72-4186-bd22-4ccb600b8e45@googlegroups.com> <7xwqxt765m.fsf@ruckus.brouhaha.com> <mailman.3537.1352567636.27098.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 2.229.207.3
MIME-Version 1.0
Subject Re: Format specification mini-language for list joining
From Tobia Conforto <tobia.conforto@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Message-ID <mailman.3543.1352581872.27098.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352581872 news.xs4all.nl 6945 [2001:888:2000:d::a6]:48635
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33104

Show key headers only | View raw


Kwpolska wrote:
> > out = 'Temperatures: {0:", ":.1f} Celsius'.format(temps)
> 
> [...] your format is cryptic.

Thank you for your criticism, I'll think it over. The reason I find it readable (-enough) is because even without knowing what format language is supported by the temps object, you can tell that "it" (the 0th argument in this case) is what's going to be serialized in that place.

Everything after the first colon is game anyways, meaning you'll have to look it up in the docs, because it's defined somewhere in the class hierarchy of the object being serialized. The fact that 99% of classes don't define a __format__ method and thus fall back on object's implementation, with it's alignment and padding operators, is IMHO irrelevant. It's still something you can't pretend to know out of the box, because it's supposed to be customizable by classes.

Knowing this, if you know that the temps object is a list of floats, then I think it'd be pretty obvious what the ", " and the :.1f should do.

> As I said, it’s hard to even get this one changed 
> because str.format is 4 years old.

Again, I beg to differ. I'm not proposing any change to format (that would be madness). What I'm proposing is the addition of a customized __format__ method to a few types, namely lists and sequences, that currently lack it (as do 99% of classes) and fall back to object's implementation. Which is kind of pointless with lists, as joining is by far the thing most often done to them when formatting.

Tobia

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


Thread

Format specification mini-language for list joining Tobia Conforto <tobia.conforto@gmail.com> - 2012-11-10 02:26 -0800
  Re: Format specification mini-language for list joining Paul Rubin <no.email@nospam.invalid> - 2012-11-10 08:51 -0800
    Re: Format specification mini-language for list joining Kwpolska <kwpolska@gmail.com> - 2012-11-10 18:13 +0100
      Re: Format specification mini-language for list joining Tobia Conforto <tobia.conforto@gmail.com> - 2012-11-10 13:11 -0800
      Re: Format specification mini-language for list joining Tobia Conforto <tobia.conforto@gmail.com> - 2012-11-10 13:11 -0800
  Re: Format specification mini-language for list joining Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-10 16:55 +0000

csiph-web