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


Groups > comp.lang.python > #33812

Re: Yet another Python textbook

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@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; 'example:': 0.03; 'argument': 0.04; 'subject:Python': 0.05; 'formatting': 0.07; 'wrapper': 0.07; '0),': 0.09; '22,': 0.09; 'braces': 0.09; 'docs.': 0.09; 'method:': 0.09; 'spec': 0.09; 'specifiers': 0.09; 'def': 0.10; "skip:' 30": 0.15; 'value.': 0.15; '"format': 0.16; "'hello": 0.16; 'colon.': 0.16; 'curly': 0.16; 'invoking': 0.16; 'overriding': 0.16; 'reversed': 0.16; 'skip:) 30': 0.16; 'spec):': 0.16; 'specifier': 0.16; 'string:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'documented': 0.17; 'skip:{ 20': 0.17; 'string,': 0.17; 'thu,': 0.17; '>>>': 0.18; 'module': 0.19; 'skip:p 30': 0.20; '(b)': 0.22; 'latter': 0.22; 'example': 0.23; 'seems': 0.23; 'raise': 0.24; 'second': 0.24; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'values': 0.26; 'am,': 0.27; 'rules': 0.27; 'message-id:@mail.gmail.com': 0.27; 'optional': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'basic': 0.30; 'code': 0.31; 'implement': 0.32; 'anywhere': 0.33; 'goes': 0.33; '(a)': 0.33; '11,': 0.33; 'to:addr:python-list': 0.33; '(c)': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'built-in': 0.35; 'fail': 0.35; 'nov': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'skip:{ 10': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'beyond': 0.37; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'passed': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'world': 0.63; 'more': 0.63; 'here': 0.65; 'self.value': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=QB9lD3YS8MNQEDOu5WYkJw66gG7ubyg/J21pldI1xzs=; b=Vpxon8mGVNs0pOcIn+lR4nORYk4c58gz0feedyiAYM3i4gALcI/4/8AVaWi3BctVMc +JRLXkLMBQns/Ax56sqgZyzTEjF2NpKSYX8/Wbag31YU9zLs16KU5UY8AV/ZAURCb+Mt 7R5YvZKPe48oouVXgVaMY/4vEKom3syyWT/FmcA1Pgp3KwijCtw0GRrHMS2f05fFLm5Q EMHdsv8j5RpP8DMoq3NzgXfXQJCg9xTYA/w/TYIU4u4vOi5HTCkH4DZYjzGhFFhaXGg1 pyc3aeKKAmQoYeEm9a1ulhbwLohKa85gS1g392ZbGuU6rKVxdBaUFDqz7LScdTwoAEXh ckfg==
MIME-Version 1.0
In-Reply-To <50AE1986.2080605@ncf.ca>
References <CAB89DN0nusHZU32zP5UbTSUH8P7aXJomssBT-Jge7LEhaOtphg@mail.gmail.com> <CALwzid=3b_Gv3XR5iYvo7Pc0ZjXERB60daT9AjVoOG1OAP9vNg@mail.gmail.com> <CAB89DN0M5NzpoLRtcXRtM4Vs4xrOfw0wZq64GkU-DKy=Ei8HJQ@mail.gmail.com> <mailman.34.1353398989.29569.python-list@python.org> <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <mailman.96.1353445247.29569.python-list@python.org> <50AD0962.5080002@ncf.ca> <mailman.180.1353536254.29569.python-list@python.org> <50AE1986.2080605@ncf.ca>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 22 Nov 2012 11:27:10 -0700
Subject Re: Yet another Python textbook
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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>
Newsgroups comp.lang.python
Message-ID <mailman.210.1353608868.29569.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353608868 news.xs4all.nl 6975 [2001:888:2000:d::a6]:44206
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33812

Show key headers only | View raw


On Thu, Nov 22, 2012 at 5:24 AM, Colin J. Williams <cjw@ncf.ca> wrote:
> From my reading of the docs, it seems to me that the three following should
> be equivalent:
>
>   (a) formattingStr.format(values)
> with
>   (b) format(values, formattingStr)
> or
>   (c) tupleOfValues.__format__(formattingStr
>
> Example:
> print('{:-^14f}{:^14d}'.format(-25.61, 95 ))
> print(format((-25.61, 95), '{:-^14f}{:^14d}'))
> (-25.61, 95 ).__format__('{:-^14f}{:^14d}')
>
> The second fails, perhaps because values can only be a single value.
> The third fails, the reason is unclear.

The latter two (which are more or less equivalent) fail because they are
intended for invoking the formatting rules of a single value.  The
string argument to each of them is not a format string, but a "format
specification", which in a format string is only the part that goes
inside the curly braces and after the optional colon.  For example, in
this format string:

>>> 'Hello world {0!s:_>4s}'.format(42)
'Hello world __42'

The format specifier here is "_>4s":

>>> format('42', '_>4s')
'__42'

The valid format specifiers depend upon the type of the object being formatted:

>>> format(42, '04x')
'002a'

>>> format(datetime(2012, 11, 22, 11, 17, 0), 'The time is %Y %d %m %H:%M:%S')
'The time is 2012 22 11 11:17:00'

Custom types can implement custom format specifications by overriding
the __format__ method:

>>> class Foo:
...     def __init__(self, value):
...         self.value = value
...     def __format__(self, spec):
...         if spec == 'a':
...             return str(self.value)
...         if spec == 'b':
...             return ''.join(reversed(str(self.value)))
...         raise ValueError("Unknown format code {!r}".format(spec))
...
>>> format(Foo(42), 'a')
'42'
>>> format(Foo(42), 'b')
'24'

The same format specifications can then also be passed to str.format:

>>> '{0:a} reversed is {0:b}'.format(Foo(42))
'42 reversed is 24'

Unfortunately, there does not seem to be a good reference to the
format specifications available for built-in types beyond basic
strings and numbers.  I only knew about the datetime example because
it is used in an example in the str.format docs.  The
datetime.__format__ implementation (which seems to be just a thin
wrapper of datetime.strftime) does not seem to be documented anywhere
in the datetime module docs.

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


Thread

Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-20 19:09 +1100
  Re: Yet another Python textbook wxjmfauth@gmail.com - 2012-11-20 06:57 -0800
    Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-21 08:00 +1100
      Re: Yet another Python textbook wxjmfauth@gmail.com - 2012-11-21 06:49 -0800
      Re: Yet another Python textbook wxjmfauth@gmail.com - 2012-11-21 06:49 -0800
      Re: Yet another Python textbook "Colin J. Williams" <cjw@ncf.ca> - 2012-11-21 12:03 -0500
        Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-22 09:17 +1100
          Re: Yet another Python textbook "Colin J. Williams" <cjw@ncf.ca> - 2012-11-22 07:24 -0500
            Re: Yet another Python textbook Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-22 11:27 -0700
              Re: Yet another Python textbook "Colin J. Williams" <cjw@ncf.ca> - 2012-11-22 17:41 -0500
                Re: Yet another Python textbook Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-23 03:26 +0000
            Re: Yet another Python textbook Terry Reedy <tjreedy@udel.edu> - 2012-11-22 17:12 -0500
        Re: Yet another Python textbook Dave Angel <d@davea.name> - 2012-11-21 17:58 -0500
        Re: Yet another Python textbook Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-21 16:11 -0700
        Re: Yet another Python textbook Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-21 23:26 +0000
        Re: Yet another Python textbook Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-21 23:32 +0000
        Re: Yet another Python textbook Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-21 17:19 -0700
        Re: Yet another Python textbook Terry Reedy <tjreedy@udel.edu> - 2012-11-21 23:04 -0500
    Re: Yet another Python textbook Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-20 21:55 +0000
    Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-21 09:25 +1100

csiph-web