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


Groups > comp.lang.python > #60670

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <mr.cerutti@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'output': 0.05; 'subject:Python': 0.06; 'nested': 0.07; 'removes': 0.07; 'input,': 0.09; 'variable,': 0.09; 'cc:addr:python-list': 0.11; 'csv': 0.16; 'input:': 0.16; 'nest': 0.16; 'stack:': 0.16; 'statements,': 0.16; 'subject:breaking': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'input': 0.22; 'otherwise,': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'space.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'usually': 0.31; 'indentation': 0.31; 'file': 0.32; 'this.': 0.32; 'another': 0.32; 'could': 0.34; 'received:google.com': 0.35; 'there': 0.35; 'subject:?': 0.36; 'two': 0.37; 'skip:o 20': 0.38; 'nov': 0.38; 'needed': 0.38; 'pm,': 0.38; 'bad': 0.39; 'how': 0.40; 'skip:c 50': 0.60; 'save': 0.62; 'temporary': 0.65; 'it!': 0.67; 'all;': 0.84; 'subject:long': 0.84; 'victor': 0.84; 'to:none': 0.92; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=fcKXjGz1diXfkBqpsSDlX8N6iqSxojSUZiTKSAv4YVw=; b=uAp6QkoPlXPtV4DrLfon8jmV0DhSCbbwVoO4zuDuiEqOMSEz/BZPFUVBFtI8lQk8r9 gPgJxp65z5r5WFv5GoP7pkf74KlLu4Jj59aOi0oUAgfW4GrdsjakIQJfQWszrDQnwKCm K6yxmSmaM26jRbv+TzwdcYt9x2oq72Oyuy3ixfMIzSBxFVvbm0QaajZvmv68KTrS+Qde MM0kgKH1xoORTcygY/4z25GoPRCNxFEX1O1NGGYbmEx3m9K7APqpvgPeKh+dY9cc8YQw EjE79g1faYJD1sEzhdkRk9TYNJjU8zseMxoxQD71yUabq+9Pnq+AQ8u8w9KY8DFrnxQy xo8A==
MIME-Version 1.0
X-Received by 10.194.109.167 with SMTP id ht7mr34519929wjb.5.1385608193904; Wed, 27 Nov 2013 19:09:53 -0800 (PST)
In-Reply-To <a98cfbef-bc48-41e8-98de-e616f201ffbe@googlegroups.com>
References <a98cfbef-bc48-41e8-98de-e616f201ffbe@googlegroups.com>
Date Wed, 27 Nov 2013 22:09:53 -0500
Subject Re: Python and PEP8 - Recommendations on breaking up long lines?
From Neil Cerutti <mr.cerutti@gmail.com>
Cc 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 <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.3344.1385608200.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385608200 news.xs4all.nl 15939 [2001:888:2000:d::a6]:36081
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60670

Show key headers only | View raw


tOn Wed, Nov 27, 2013 at 8:57 PM, Victor Hooi <victorhooi@gmail.com> wrote:
>             with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as output:

There are two nice and clean solutions for this.

I usually nest them.

with open(self.full_path, 'r') as input:
  with open(self.ouptut_csv, 'ab') as output:

I use a non-standard (shortened) indentation for nested with
statements, to save space. I don't feel bad about this at all; the
with statement's syntax makes me do it!

Otherwise, contextlib.ExitStack is another way to separate them.

with contextlib.ExitStack() as stack:
    input = stack.enter_context(open(self.full_path, 'r'))
    writer = csv.writer(stack.enter_context(open(self.output_csv)))

When working with a csv file I like how it removes the output
temporary file object variable, though if you needed it for some
reason you could keep it.

-- 
Neil Cerutti

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