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


Groups > comp.lang.python > #48885

Re: n00b question on spacing

Date 2013-06-21 17:48 -0400
From Ray Cote <rgacote@appropriatesolutions.com>
Subject Re: n00b question on spacing
Newsgroups comp.lang.python
Message-ID <mailman.3670.1371851743.3114.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

----- Original Message -----

> From: "Yves S. Garret" <yoursurrogategod@gmail.com>
> To: python-list@python.org
> Sent: Friday, June 21, 2013 5:17:28 PM
> Subject: n00b question on spacing

> Hi, I have a question about breaking up really long lines of code in
> Python.

> I have the following line of code:
> log.msg("Item wrote to MongoDB database %s/%s"
> %(settings['MONGODB_DB'], settings['MONGODB_COLLECTION']),
> level=log.DEBUG, spider=spider)

> Given the fact that it goes off very far to the right on my screen is
> not terribly
> pleasing to my eyes (and can be rude for other developers).

> I was thinking of splitting it up like so:
> log.msg("Item wrote to MongoDB database %s/%s"
> %(settings['MONGODB_DB'], settings['MONGODB_COLLECTION']),
> level=log.DEBUG, spider=spider)

> Is this ok? Are there any rules in Python when it comes to breaking
> up long lines of
> code?

> --
> http://mail.python.org/mailman/listinfo/python-list

Hi Yves: 
PEP8 is your definitive guide for style questions: 
<http://www.python.org/dev/peps/pep-0008/> 

and this is an interesting set of notes: 
<http://stackoverflow.com/questions/5931297/how-would-you-properly-break-this-line-to-match-pep8-rules> 

Basic rule is to break within parenthesis -- after that it becomes a matter of personal taste/style. 

In your specific example, I would do something like: 
log.msg( 
"Item wrote to MongoDB database %s %s" % ( 
settings['MONGODB_DB'], 
settings['MONGODB_COLLECTION]), 
level=log.DEBUG, 
spider=spider) 

Though you might want to: 
a) start your string right after the log.msg( 
b) put more than one settings on the same line 
c) put the last two parameters on the same line. 

I find that once I start breaking up lines for length, that I prefer to break up everything. 

Also remember when entering long lines of text that strings concatenate within parenthesis. 
So, 
("a, b, c" 
"d, e, f" 
"g, h, i") 

Is the same as ("a, b, cd, e, fg, h, i") 

--Ray 

-- 

Ray Cote, President 
Appropriate Solutions, Inc. 
We Build Software 
603.924.6079 

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


Thread

Re: n00b question on spacing Ray Cote <rgacote@appropriatesolutions.com> - 2013-06-21 17:48 -0400
  Re: n00b question on spacing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-22 01:27 +0000

csiph-web