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


Groups > comp.lang.python > #60688

Re: Python and PEP8 - Recommendations on breaking up long lines?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'programmer': 0.03; 'output': 0.05; 'that?': 0.05; 'subject:Python': 0.06; 'exception.': 0.09; 'input,': 0.09; 'runtime': 0.09; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'subject:breaking': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'input': 0.22; 'import': 0.22; 'error': 0.23; 'header:In-Reply-To:1': 0.27; 'room': 0.29; "doesn't": 0.30; "d'aprano": 0.31; 'steven': 0.31; 'actual': 0.34; 'could': 0.34; 'but': 0.35; 'really': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; '2nd': 0.60; 'more': 0.64; 'fails,': 0.84; 'partial': 0.84; 'received:50.22': 0.84; 'subject:long': 0.84
Date Thu, 28 Nov 2013 08:04:13 -0600
From Tim Chase <python.list@tim.thechases.com>
To python-list@python.org
Subject Re: Python and PEP8 - Recommendations on breaking up long lines?
In-Reply-To <5296bf7d$0$29995$c3e8da3$5496439d@news.astraweb.com>
References <a98cfbef-bc48-41e8-98de-e616f201ffbe@googlegroups.com> <mailman.3340.1385607612.18130.python-list@python.org> <5296bf7d$0$29995$c3e8da3$5496439d@news.astraweb.com>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Get-Message-Sender-Via boston.accountservergroup.com: authenticated_id: tim@thechases.com
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3354.1385647374.18130.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385647374 news.xs4all.nl 15898 [2001:888:2000:d::a6]:44017
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60688

Show key headers only | View raw


On 2013-11-28 03:58, Steven D'Aprano wrote:
>>      input = open(self.full_path)
>>      output = open(self.output_csv, 'ab')
>>      with input as input, output as output:
>>          ...
> 
> That's really clever! Why didn't I think of that?

Because if the 2nd output fails, the input doesn't get closed as
it {w,sh}ould in a with-statement? :-)

You could work around this with:

  from functools import partial
  in_ = partial(open, self.full_path)
  out_ = partial(open, self.output_csv, 'ab')
  with in_() as input, out_() as output:
    do_stuff()

There's still room for programmer error if you don't have an
"output_csv" property on self and you get an AttributeError, but
that's more of a "dumb programmer error" rather than an actual
runtime exception.

-tkc


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


Thread

Python and PEP8 - Recommendations on breaking up long lines? Victor Hooi <victorhooi@gmail.com> - 2013-11-27 17:57 -0800
  Re: Python and PEP8 - Recommendations on breaking up long lines? Victor Hooi <victorhooi@gmail.com> - 2013-11-27 18:03 -0800
    Re: Python and PEP8 - Recommendations on breaking up long lines? Ben Finney <ben+python@benfinney.id.au> - 2013-11-28 13:55 +1100
    Re: Python and PEP8 - Recommendations on breaking up long lines? Terry Reedy <tjreedy@udel.edu> - 2013-11-27 22:03 -0500
    Re: Python and PEP8 - Recommendations on breaking up long lines? Ned Batchelder <ned@nedbatchelder.com> - 2013-11-27 22:05 -0500
    Re: Python and PEP8 - Recommendations on breaking up long lines? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-11-28 08:12 +0200
      Re: Python and PEP8 - Recommendations on breaking up long lines? Chris Angelico <rosuav@gmail.com> - 2013-11-28 17:22 +1100
    Re: Python and PEP8 - Recommendations on breaking up long lines? Roy Smith <roy@panix.com> - 2013-11-28 10:08 -0500
  Re: Python and PEP8 - Recommendations on breaking up long lines? Ben Finney <ben+python@benfinney.id.au> - 2013-11-28 13:47 +1100
    Re: Python and PEP8 - Recommendations on breaking up long lines? Steven D'Aprano <steve@pearwood.info> - 2013-11-28 03:02 +0000
      Re: Python and PEP8 - Recommendations on breaking up long lines? Ben Finney <ben+python@benfinney.id.au> - 2013-11-28 14:14 +1100
  Re: Python and PEP8 - Recommendations on breaking up long lines? Terry Reedy <tjreedy@udel.edu> - 2013-11-27 21:55 -0500
  Re: Python and PEP8 - Recommendations on breaking up long lines? Ned Batchelder <ned@nedbatchelder.com> - 2013-11-27 21:59 -0500
    Re: Python and PEP8 - Recommendations on breaking up long lines? Steven D'Aprano <steve@pearwood.info> - 2013-11-28 03:58 +0000
      Re: Python and PEP8 - Recommendations on breaking up long lines? Tim Chase <python.list@tim.thechases.com> - 2013-11-28 08:04 -0600
      Re: Python and PEP8 - Recommendations on breaking up long lines? Chris Angelico <rosuav@gmail.com> - 2013-11-29 01:21 +1100
      Re: Python and PEP8 - Recommendations on breaking up long lines? Ned Batchelder <ned@nedbatchelder.com> - 2013-11-28 12:26 -0500
  Re: Python and PEP8 - Recommendations on breaking up long lines? Ben Finney <ben+python@benfinney.id.au> - 2013-11-28 14:06 +1100
  Re: Python and PEP8 - Recommendations on breaking up long lines? Neil Cerutti <mr.cerutti@gmail.com> - 2013-11-27 22:09 -0500
  Re: Python and PEP8 - Recommendations on breaking up long lines? Ethan Furman <ethan@stoneleaf.us> - 2013-11-27 19:15 -0800
  Re: Python and PEP8 - Recommendations on breaking up long lines? Steven D'Aprano <steve@pearwood.info> - 2013-11-28 03:57 +0000
    Re: Python and PEP8 - Recommendations on breaking up long lines? Steven D'Aprano <steve@pearwood.info> - 2013-11-28 04:03 +0000
  Re: Python and PEP8 - Recommendations on breaking up long lines? MRAB <python@mrabarnett.plus.com> - 2013-11-28 12:43 +0000
  Re: Python and PEP8 - Recommendations on breaking up long lines? Walter Hurry <walterhurry@lavabit.com> - 2013-11-28 17:38 +0000
  Re: Python and PEP8 - Recommendations on breaking up long lines? Roel Schroeven <roel@roelschroeven.net> - 2013-11-28 19:37 +0100
  Re: Python and PEP8 - Recommendations on breaking up long lines? Nick Mellor <thebalancepro@gmail.com> - 2013-12-04 17:47 -0800

csiph-web