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


Groups > comp.lang.python > #48885 > unrolled thread

Re: n00b question on spacing

Started byRay Cote <rgacote@appropriatesolutions.com>
First post2013-06-21 17:48 -0400
Last post2013-06-22 01:27 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#48885 — Re: n00b question on spacing

FromRay Cote <rgacote@appropriatesolutions.com>
Date2013-06-21 17:48 -0400
SubjectRe: n00b question on spacing
Message-ID<mailman.3670.1371851743.3114.python-list@python.org>

[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 

[toc] | [next] | [standalone]


#48894

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-22 01:27 +0000
Message-ID<51c4fd9a$0$29999$c3e8da3$5496439d@news.astraweb.com>
In reply to#48885
On Fri, 21 Jun 2013 17:48:54 -0400, Ray Cote wrote:

> 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")


Technically, you don't need the parentheses. You can also use backslash 
to continue the lines:


s = "a, b, c, " \
    "d, e, f, " \
    "g, h, i"
assert s == "a, b, c, d, e, f, g, h, i"


Or, if the strings are small enough, fit them on one line:

s = "a" "b" "c"

This *implicit concatenation* only works with string literals, not 
variables, but it works with any sort of quoting style:

s = "-'-" '-"-' r"\a\b"
assert s == "-'--\"-\\a\\b"


Like most such features, a little goes a long way. Don't over do it, it 
is quite possible to end up with an unreadable mess if you overuse it.



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web