1
votes

PEP8 tells me that the maximum line length should be 79. This sounds a bit like punched cards, and I'm used to longer lines, but as I learn Python, I'm trying to conform to the standard style.

Consider this line:

partsList[r][newPurchaseNotes] += partsList[r+1][newPurchaseNotes]

When indented 4 stops (using 4-space tabs per PEP8), it overflows. (If I use PEP8 underscore separators rather than Java-style mixed case, it's worse.)

If I break it into two lines like this:

partsList[r][newPurchaseNotes] 
    += partsList[r+1][newPurchaseNotes]

...it is a syntax error (unexpected indent). Breaking it like this:

partsList[r][newPurchaseNotes] += 
    partsList[r+1][newPurchaseNotes]

...it is ALSO a syntax error (invalid syntax).

Here are two obvious solutions, neither of which I like:

  • use shorter names for variables
  • break the one statement into two with intermediate variables:

    s = partsList[r+1][newPurchaseNotes]
    partsList[r][newPurchaseNotes] += s
    

In researching this forum, I did find suggestions for reducing indentation levels for loops and for conditionals. In my case, I had if nested in if nested in a while inside a function. I changed the logic to reduce one level of if statements, but it was not enough to keep the line in 79 characters.

2
The reason you didn't get code formatted text originally, was because the indentation you gave it indicated it was a continuation of the above bullet point. It needed to be indented even moreSpoonMeiser
Thanks. I noticed that the problem was gone (more indentation added, the comment about why it was not formatted was removed) when I went in to edit it. How did that happen? Can others besides me edit the text?Mark Colan
Yes, anyone with enough rep can edit it, but the edits will be reviewed and potentially rolled back if they aren't actual improvementsSpoonMeiser
Thanks again. Obviously I am a stackoverflow newbie as well as a Python newbie.Mark Colan

2 Answers

2
votes

Generally, while PEP-8 calls for 79-character lines, a more-accepted rule of thumb is 100 characters.

That said, this particular line seems relatively verbose and repeats a fair amount of logic between the two statements. What's the goal? It's quite possible that given a bit more context, this particular line can be simplified substantially.

EDIT: Code sample:

So, the goal is to essentially merge line items that share certain attributes. This seems like a great way to use an object:

class LineDeduper(object):

    def __init__(self, indices, addition_points):
        self.items = {}
        self.indices = indices
        self.addition_points = addition_points

    def add(self, item):
        mergekey = tuple(item[x] for x in self.indices)
        if mergekey in self.items:
            self.merge(mergekey, item)
        else:
            self.items[mergekey] = item

    def merge(self, key, item):
        for idx, val in enumerate(item):
            if idx in self.addition_points:
                self.items[key][idx] += val

    def to_list(self):
        return [x for x in self.items.values()]

Just initialize with lists of the relevant deduplication indices, as well a list of the indices that need to be added together, and add each line from the CSV. Once done, you can easily get out the results.

1
votes

Python doesn't use semi-colons to denote the end of lines. Use a backslash to indicate the line is continued on the next line:

partsList[r][newPurchaseNotes] \
    += partsList[r+1][newPurchaseNotes]

The other way you can break a statement over multiple lines in python, is if the break happens within parentheses or brackets. In your example here, you could break the line between [ and ], but that would be really ugly.

One possibility is:

partsList[r][newPurchaseNotes] = (
    partsList[r][newPurchaseNotes] +
    partsList[r+1][newPurchaseNotes]
)

I'm not sure that that's an improvement though.