top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

breaking up really long lines of code in Python.

0 votes
735 views

This 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?

posted Jun 21, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

PEP8 is your definitive guide for style questions:

and this is an interesting set of notes:

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")
answer Jun 21, 2013 by anonymous
0 votes

There are guidelines in the PEP8 document:

http://www.python.org/dev/peps/pep-0008/

Check out the section entitled 'Code lay-out'.

answer Jun 21, 2013 by anonymous
Similar Questions
0 votes

I've got a simple script that counts the number of words in a data set (it's more complicated than that, but that's one of the functions), but there are so many words that the output is too much to see in the command prompt window. What I'd like to be able to do is incorporate the "More..." feature that help libraries have, but I have no idea how to do it. I also don't know if I'm asking the question correctly because a Google search yielding nothing.

+1 vote

Here is something I found curious about python loops.

This loop run each character in a string

def avoids(word,letters):
 flag = True
 for letter in letters:
 if(letter in word):
 flag = False
 return flag

The loop below (at the bottom) runs each line of the file

fin = open('wordplay.txt');
user_input = raw_input('Enter some characters: ')
count = 0
for line in fin:
 word = line.strip()
 if(avoids(word, user_input)):
 count += 1;

This is just too convenient. Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?

Any comment is greatly appreciate.

+4 votes

I have been trying to set up a python, django, mysql, virtualenvwrapper and git development project and am really confused. All of the documentation seems to ignore the apt-get installation methods used by Debian Linux and its derivatives. Does pip install the same as apt-get; I don't think so. If I use virtualenvwrapper, how does this fit with the normal debian (wheezy) installation. I also need git which just confuses the situation even more. Must I give up the automatic updating system that Debian provides when setting up the development environment?

The documentation centers on Windows, Mac and generic Linux distributions and ignores the automation of the Debian installation. All of the documentation I have found concentrates on the installation of individual packages or on the use of python-django and is very sketchy on the overall virtualenv(wrapper), git, python-django file structure and installation order.

+3 votes

I need python code snippet for LTE/UMTS for automation testing.Example of scenario looking for LTE attach ,detach, MO/MT call .
I am looking for UE side scripts.

...