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


Groups > comp.lang.python > #60665

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'causing': 0.04; 'warnings': 0.04; 'output': 0.05; 'sufficient': 0.05; 'subject:Python': 0.06; '"""': 0.07; '"as': 0.07; 'assign': 0.07; 'context': 0.07; 'lines,': 0.07; 'string': 0.09; 'input,': 0.09; 'permissions': 0.09; 'quotes:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'python': 0.11; '(use': 0.16; 'clauses': 0.16; 'comments:': 0.16; 'exist."""': 0.16; 'guessing': 0.16; 'newlines': 0.16; 'parentheses': 0.16; 'pep8': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recognised': 0.16; 'statements,': 0.16; 'subject:breaking': 0.16; 'syntax,': 0.16; 'thoughts?': 0.16; 'url.': 0.16; 'wraps': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'have:': 0.19; 'pointed': 0.19; "python's": 0.19; 'split': 0.19; 'fit': 0.20; 'code,': 0.22; 'input': 0.22; 'example': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'convenient': 0.24; 'received:comcast.net': 0.24; 'cheers,': 0.24; 'file.': 0.24; 'looks': 0.24; '(or': 0.24; "i've": 0.25; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'wonder': 0.29; 'important.': 0.30; 'statement': 0.30; "i'm": 0.30; 'getting': 0.31; 'lines': 0.31; 'usually': 0.31; 'breaking': 0.31; 'comments,': 0.31; 'quotes': 0.31; 'though.': 0.31; 'way?': 0.31; 'file': 0.32; 'another': 0.32; 'open': 0.33; 'running': 0.33; 'third': 0.33; 'actual': 0.34; 'comment': 0.34; 'noticed': 0.34; 'could': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'marks': 0.36; 'right?': 0.36; 'shorter': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'example,': 0.37; 'two': 0.37; 'skip:o 20': 0.38; 'ben': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'expression': 0.60; 'further': 0.61; 'first': 0.61; 'name': 0.63; 'become': 0.64; 'more': 0.64; 'different': 0.65; 'between': 0.67; 'ending': 0.78; 'introduce': 0.78; 'potentially': 0.81; 'subject:long': 0.84; 'victor': 0.84; 'warning.': 0.84; 'warnings.': 0.84; 'messages:': 0.91; 'have.': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Python and PEP8 - Recommendations on breaking up long lines?
Date Wed, 27 Nov 2013 21:59:56 -0500
References <a98cfbef-bc48-41e8-98de-e616f201ffbe@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1
In-Reply-To <a98cfbef-bc48-41e8-98de-e616f201ffbe@googlegroups.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.3340.1385607612.18130.python-list@python.org> (permalink)
Lines 107
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385607612 news.xs4all.nl 15923 [2001:888:2000:d::a6]:53825
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60665

Show key headers only | View raw


On 11/27/13 8:57 PM, Victor Hooi wrote:
> Hi,
>
> I'm running pep8 across my code, and getting warnings about my long lines (> 80 characters).
>
> I'm wonder what's the recommended way to handle the below cases, and fit under 80 characters.

My recommendations usually amount to: write more statements, each 
shorter than what you have.

> First example - multiple context handlers:
>
>              with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as output:
>
> and in my case, with indents, the 80-character marks is just before the ending "as output".
>
> What's the standard recognised way to split this across multiple lines, so that I'm under 80 characters?
>
> I can't just split after the "as input," as that isn't valid syntax, and there's no convenient parentheses for me to split over.
>
> Is there a standard Pythonic way?

The important thing in a with statement is that the assigned name will 
be closed (or otherwise exited) automatically.  The open call is just 
the expression used to assign the name.  The expression there isn't 
really important.  This looks odd, but works the same as what you have:

     input = open(self.full_path)
     output = open(self.output_csv, 'ab')
     with input as input, output as output:
         ...

(Use different names for the two parts of the "as" clauses if you like.)

>
> Second example - long error messages:
>
>              self.logger.error('Unable to open input or output file - %s. Please check you have sufficient permissions and the file and parent directory exist.' % e)
>
> I can use triple quotes:
>
>              self.logger.error(
>                  """Unable to open input or output file - %s. Please check you
>                  have sufficient permissions and the file and parent directory
>                  exist.""" % e)
>
> However, that will introduce newlines in the message, which I don't want.
>
> I can use backslashes:
>
>              self.logger.error(
>                  'Unable to open input or output file - %s. Please check you\
>                  have sufficient permissions and the file and parent directory\
>                  exist.' % e)
>
> which won't introduce newlines.
>
> Or I can put them all as separate strings, and trust Python to glue them together:
>
>              self.logger.error(
>                  'Unable to open input or output file - %s. Please check you'
>                  'have sufficient permissions and the file and parent directory'
>                  'exist.' % e)
>
> Which way is the recommended Pythonic way?

Use the separate strings, but don't forget the spaces:

     self.logger.error(
         "Unable to open input or output file - %s. Please check you "
         "have sufficient permissions and the file and parent directory "
         "exist." % e
     )

>
> Third example - long comments:
>
>              """ NB - We can't use Psycopg2's parametised statements here, as
>              that automatically wraps everything in single quotes.
>              So s3://my_bucket/my_file.csv.gz would become s3://'my_bucket'/'my_file.csv.gz'.
>              Hence, we use Python's normal string formating - this could
>              potentially exposes us to SQL injection attacks via the config.yaml
>              file.
>              I'm not aware of any easy ways around this currently though - I'm
>              open to suggestions though.
>              See
>              http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error
>              for further information. """
>
> In this case, I'm guessing a using triple quotes (""") is a better idea with multi-line comments, right?

As Ben pointed out, using an actual comment is best.

> However, I've noticed that I can't seem to put in line-breaks inside the comment without triggering a warning. For example, trying to put in another empty line in between lines 6 and 7 above causes a warning.

I don't know what you mean about line-breaks causing warnings.

--Ned.

>
> Also, how would I split up the long URLs? Breaking it up makes it annoying to use the URL. Thoughts?
>
> Cheers,
> Victor
>

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