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


Groups > comp.lang.python > #45496

Re: How to write fast into a file in python?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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; 'operator': 0.03; 'languages,': 0.04; 'syntax': 0.04; 'attribute': 0.07; 'binary': 0.07; 'method.': 0.07; 'subject:file': 0.07; 'lookup': 0.09; 'override': 0.09; 'parameter': 0.09; 'skip:% 20': 0.09; 'subject:into': 0.09; 'subject:How': 0.10; 'def': 0.12; '"%s"': 0.16; '*almost*': 0.16; 'cleaner': 0.16; 'clunky': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guarded': 0.16; 'hard-coded': 0.16; 'naming': 0.16; 'notation': 0.16; 'operator.': 0.16; 'portable': 0.16; 'shortcut': 0.16; 'tuple,': 0.16; 'tuple.': 0.16; 'subject:python': 0.16; 'flexibility': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '(not': 0.18; 'bit': 0.19; 'basically': 0.19; '>>>': 0.22; 'features,': 0.24; 'skip:% 10': 0.24; 'skip:{ 20': 0.24; "i've": 0.25; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'faster,': 0.31; 'object.': 0.31; 'steven': 0.31; 'vote.': 0.31; 'anyone': 0.31; 'skip:d 20': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'keyword': 0.36; 'object,': 0.36; 'doing': 0.36; 'method': 0.36; 'subject:?': 0.36; 'handle': 0.38; 'needed': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'does': 0.39; 'extremely': 0.39; 'to:addr:python.org': 0.39; "you're": 0.61; 'happen': 0.63; 'more': 0.64; 'between': 0.67; 'biggest': 0.67; 'potentially': 0.81; 'flexibility,': 0.84; 'deal,': 0.93; 'differences': 0.93; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=pK7Fo7nom1ISSslX7o7eSwJ/cUmX8EbxQF9bpD4IQDs=; b=BBusTcvh0m8KK6Go4ekH1fC+X2gEGINnwEB4BqOqmQQRacIIMPXCSQDNlTECcjE+ps YZ7dhfYlD+uDN15gsAKBrRgAttUDmr3PgBAw2NQKb9QWV8rn1oqaBkHuO1ybEbunvwZ4 Rg67nqNo8RrPsYTMeUX4u4T/E+Q0p1skrbpX6I9O1eP5IeyWaT0fSazt04FeyIPusaKT n/Ep51N4jNyqTPu+LYkj6UlQTN1WFEDi00O7tapkP1H95Qzhb+l/UJYJab2hccttnSgv oQwZjZM2srrCRVVO01sO9KWNEpUdiXHyweCs0pWZglq+Pw9cIGKnAISxfErcl7GJ3xLI y7iQ==
MIME-Version 1.0
X-Received by 10.52.155.133 with SMTP id vw5mr6066963vdb.43.1368854888104; Fri, 17 May 2013 22:28:08 -0700 (PDT)
In-Reply-To <5196fd08$0$29997$c3e8da3$5496439d@news.astraweb.com>
References <e9dcd255-b892-40a0-ae6b-0995a61a270f@googlegroups.com> <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <mailman.1785.1368804103.3114.python-list@python.org> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> <mailman.1788.1368814763.3114.python-list@python.org> <5196fd08$0$29997$c3e8da3$5496439d@news.astraweb.com>
Date Sat, 18 May 2013 15:28:08 +1000
Subject Re: How to write fast into a file in python?
From Chris Angelico <rosuav@gmail.com>
To 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.1794.1368854897.3114.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368854897 news.xs4all.nl 15864 [2001:888:2000:d::a6]:35026
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45496

Show key headers only | View raw


On Sat, May 18, 2013 at 2:01 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Consider if x is an arbitrary object, and you call "%s" % x:
>
> py> "%s" % 23  # works
> '23'
> py> "%s" % [23, 42]  # works
> '[23, 42]'
>
> and so on for *almost* any object. But if x is a tuple, strange things
> happen

Which can be guarded against by wrapping it up in a tuple. All you're
seeing is that the shortcut notation for a single parameter can't
handle tuples.

>>> def show(x):
	return "%s" % (x,)

>>> show(23)
'23'
>>> show((23,))
'(23,)'
>>> show([23,42])
'[23, 42]'

One of the biggest differences between %-formatting and str.format is
that one is an operator and the other a method. The operator is always
going to be faster, but the method can give more flexibility (not that
I've ever needed or wanted to override anything).

>>> def show_format(x):
	return "{}".format(x) # Same thing using str.format
>>> dis.dis(show)
  2           0 LOAD_CONST               1 ('%s')
              3 LOAD_FAST                0 (x)
              6 BUILD_TUPLE              1
              9 BINARY_MODULO
             10 RETURN_VALUE
>>> dis.dis(show_format)
  2           0 LOAD_CONST               1 ('{}')
              3 LOAD_ATTR                0 (format)
              6 LOAD_FAST                0 (x)
              9 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             12 RETURN_VALUE

Attribute lookup and function call versus binary operator. Potentially
a lot of flexibility, versus basically hard-coded functionality. But
has anyone ever actually made use of it?

str.format does have some cleaner features, like naming of parameters:

>>> "{foo} vs {bar}".format(foo=1,bar=2)
'1 vs 2'
>>> "%(foo)s vs %(bar)s"%{'foo':1,'bar':2}
'1 vs 2'

Extremely handy when you're working with hugely complex format
strings, and the syntax feels a bit clunky in % (also, it's not
portable to other languages, which is one of %-formatting's biggest
features). Not a huge deal, but if you're doing a lot with that, it
might be a deciding vote.

ChrisA

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


Thread

How to write fast into a file in python? lokeshkoppaka@gmail.com - 2013-05-16 20:20 -0700
  Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-17 03:51 +0000
  Re: How to write fast into a file in python? lokeshkoppaka@gmail.com - 2013-05-16 21:35 -0700
    Re: How to write fast into a file in python? Dave Angel <davea@davea.name> - 2013-05-17 07:58 -0400
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 18:20 +0300
      Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-17 16:42 +0000
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 20:25 +0300
      Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-17 17:47 +0000
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 21:18 +0300
          Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-18 04:01 +0000
            Re: How to write fast into a file in python? Chris Angelico <rosuav@gmail.com> - 2013-05-18 15:28 +1000
            Re: How to write fast into a file in python? 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-18 04:09 -0700
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 21:33 +0300
        RE: How to write fast into a file in python? Fábio Santos <fabiosantosart@gmail.com> - 2013-05-18 08:49 +0100
        Re: How to write fast into a file in python? Chris Angelico <rosuav@gmail.com> - 2013-05-19 00:29 +1000
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-18 20:00 +0300
          Re: How to write fast into a file in python? Tim Roberts <timr@probo.com> - 2013-05-19 19:04 -0700
        Re: How to write fast into a file in python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-18 15:14 -0400
          Re: How to write fast into a file in python? Roy Smith <roy@panix.com> - 2013-05-18 15:37 -0400
          Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-18 22:23 +0000
        Re: How to write fast into a file in python? Fábio Santos <fabiosantosart@gmail.com> - 2013-05-18 22:19 +0100
        Re: How to write fast into a file in python? Dave Angel <davea@davea.name> - 2013-05-18 22:41 -0400
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 06:53 +0300
        Re: How to write fast into a file in python? MRAB <python@mrabarnett.plus.com> - 2013-05-19 16:44 +0100
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-20 13:34 +0300
    Re: How to write fast into a file in python? Dan Stromberg <drsalists@gmail.com> - 2013-05-18 12:38 -0700
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 08:31 +0300
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 08:42 +0300
    Re: How to write fast into a file in python? Chris Angelico <rosuav@gmail.com> - 2013-05-19 19:21 +1000
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 12:41 +0300

csiph-web