Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'parameters': 0.04; 'syntax': 0.04; 'warnings': 0.04; 'output': 0.05; 'sufficient': 0.05; 'subject:Python': 0.06; '"""': 0.07; 'context': 0.07; 'continuation': 0.07; 'ugly': 0.07; 'string': 0.09; 'comment,': 0.09; 'input,': 0.09; 'literal': 0.09; 'pep': 0.09; 'permissions': 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; '\xe2\x80\x94': 0.09; 'python': 0.11; 'advise:': 0.16; 'backslashes': 0.16; 'braces,': 0.16; 'brackets,': 0.16; 'comma.': 0.16; 'command.': 0.16; 'comments:': 0.16; 'exception:': 0.16; 'expecting': 0.16; 'finney': 0.16; 'fly': 0.16; 'great!': 0.16; 'indent': 0.16; 'line).': 0.16; 'multiples': 0.16; 'one-way': 0.16; 'pep8': 0.16; 'readable': 0.16; 'reasonably': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'statements,': 0.16; 'subject:breaking': 0.16; 'thoughts?': 0.16; 'url.': 0.16; 'wraps': 0.16; ':-)': 0.16; 'ignore': 0.16; 'code.': 0.18; 'else,': 0.19; "python's": 0.19; 'split': 0.19; 'fit': 0.20; 'code,': 0.22; 'input': 0.22; 'example': 0.22; 'programming': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'file.': 0.24; "haven't": 0.24; 'equivalent': 0.26; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'leave': 0.29; 'character': 0.29; 'ideal': 0.29; 'wonder': 0.29; "doesn't": 0.30; 'characters': 0.30; 'statement': 0.30; 'ticket.': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'comments': 0.31; 'lines': 0.31; 'breaking': 0.31; 'fine,': 0.31; 'implicit': 0.31; 'indentation': 0.31; 'though.': 0.31; 'wright': 0.31; 'writes:': 0.31; 'file': 0.32; 'text': 0.33; 'open': 0.33; 'running': 0.33; '(i.e.': 0.33; 'monday,': 0.33; 'reader': 0.33; 'style': 0.33; 'third': 0.33; 'comment': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'combination': 0.36; 'consistent': 0.36; 'next': 0.36; 'subject:?': 0.36; 'level': 0.37; 'easily': 0.37; 'skip:o 20': 0.38; 'sometimes': 0.38; 'thank': 0.38; 'ben': 0.38; 'tools,': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'most': 0.60; 'break': 0.61; 'further': 0.61; 'first': 0.61; 'back': 0.62; "you'll": 0.62; 'such': 0.63; 'become': 0.64; 'different': 0.65; 'love': 0.65; 'air': 0.66; 'applying': 0.72; 'carefully': 0.74; 'obvious': 0.74; 'special': 0.74; 'potentially': 0.81; 'distinguish': 0.84; 'subject:long': 0.84; 'terrible': 0.84; 'victor': 0.84; 'messages:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Python and PEP8 - Recommendations on breaking up long lines? Date: Thu, 28 Nov 2013 13:47:22 +1100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: rasputin.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:YJIUkdQo7ilzxKen656VpVEzpds= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 128 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385606855 news.xs4all.nl 15888 [2001:888:2000:d::a6]:45147 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60662 Victor Hooi writes: > I'm running pep8 across my code, and getting warnings about my long > lines (> 80 characters). Great! Thank you for working to make your code readable by keeping lines reasonably short. > I'm wonder what's the recommended way to handle the below cases, and > fit under 80 characters. In general, I advise: * Avoid line-end backslashes like the plague. Sometimes they're necessary, but try very hard to write code that doesn't need them. * Break a long line on existing open-parenthesis syntax (whether parens, brackets, braces, triple-quotes); early in the line is better, so the reader doesn't need to scan a long way back to the next line. * Ignore the code on the first line for aligning, and instead indent continuation lines to a consistent level (i.e. don't line up with some arbitrary character on the first line). * Use eight-column indentation for continuation lines (to clearly distinguish from four-column block indentation). > First example - multiple context handlers: I haven't used multiples in the same statement yet, and I'm annoyed that they simultaneously encourage long statements, and have no obvious way to break on an open parenthesis syntax. > with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as output: So in this case I don't see a way to avoid the ugly line-end backslash:: with \ open(self.full_path, 'r') as input, \ open(self.output_csv, 'ab') as output: Or:: with open(self.full_path, 'r') as input, \ open(self.output_csv, 'ab') as output: Both look terrible to me. I'd love to know of a way to avoid backslashes, while still breaking the line at the comma. > 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) Break at the first open-paren. Use implicit string literal continuation to break that literal across multiple lines inside the parens. Like so:: 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) Also, with such a large literal this is an ideal example of why named parameters are preferable:: self.logger.error( 'Unable to open input or output file - {error}.' ' Please check you have sufficient permissions' ' and the file and parent directory exist.'.format( error=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. """ That's not syntactically a comment, and I don't think pretending triple-quoted strings are comments is good practice. If nothing else, you'll need a special case if you want to enclose something with existing triple-quotes. Better to use the standard Python comment style:: # 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. which most programmer's text editors can do for you easily by applying or un-applying comments to a whole selected region of lines with a single command. The comments then are correctly identified as comments by all your programming tools, and you don't have to think about using a different comment style depending on the content — the same style is used for all comments. > Also, how would I split up the long URLs? Breaking it up makes it > annoying to use the URL. Thoughts? I'd try very hard to find an equivalent URL that isn't so long :-) but URLs in comments are a good example of a PEP 8 exception: if the line is over 80 characters because it contains a long URL in a comment, that's fine, as I'm not expecting the human reader to be scanning it carefully like other text in the code. -- \ “I fly Air Bizarre. You buy a combination one-way round-trip | `\ ticket. Leave any Monday, and they bring you back the previous | _o__) Friday. That way you still have the weekend.” —Steven Wright | Ben Finney