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


Groups > comp.lang.python > #48886

Re: n00b question on spacing

References <CAJ=2b07ETuSuo2+3Xu6vMOJA+q1JwUFTezO3LaYLG8wXd+FLBQ@mail.gmail.com> <24094189.2779.1371851333304.JavaMail.rgacote@ip-225.appropriatesolutions.com>
Date 2013-06-21 18:00 -0400
Subject Re: n00b question on spacing
From "Yves S. Garret" <yoursurrogategod@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3671.1371852066.3114.python-list@python.org> (permalink)

Show all headers | View raw


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

On Fri, Jun 21, 2013 at 5:48 PM, Ray Cote
<rgacote@appropriatesolutions.com>wrote:

>
> ------------------------------
>
> *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
>

Thanks for your reply Ray.  My concern was that if I were to break with
something like this:
      log.msg("Item written to MongoDB database %s/%s"
    %(settings['MONGODB_DB'], settings['MONGODB_COLLECTION']),
    level = log.DEBUG, spider = spider)

I would get some undefined behaviour (the % is a little before the log.msg).

I'll read the links that you provided in order to learn more.

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


Thread

Re: n00b question on spacing "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-06-21 18:00 -0400

csiph-web