Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm trying to adhere to PEP 8, with a 78 character limit on the length of my lines.

I have the following statement:

startTime = time.strptime(request.GET.get('st', (dt.datetime.now() - dt.timedelta(days=1)).strftime("%d/%b/%Y:%H:%M:%S")), "%d/%b/%Y:%H:%M:%S")

How should I format it so that it adheres to PEP8 (where should I break it into new lines?)

share|improve this question

closed as off-topic by user16764, MichaelT, mattnz, Bart van Ingen Schenau, psr Jul 25 '13 at 21:17

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

4  
Please do not cross post - stackoverflow.com/questions/17831382/… –  ChrisF Jul 24 '13 at 11:05
    
Okay; I wasn't sure what site to post it on, and the views were very low for a while on here, so I posted it on SO to try and get any answer. –  Todd Davies Jul 24 '13 at 12:08
2  
If you think you've got the wrong site flag the question for migration (if it has answers) or just delete and repost if it hasn't. –  ChrisF Jul 24 '13 at 12:10
    
Sure. I'll be sure to do that next time. Thanks! –  Todd Davies Jul 24 '13 at 12:24

1 Answer 1

up vote 4 down vote accepted

You shouldn't break it into new lines, since the code itself is wrong.

First, you have code duplication. The compiler won't blame you and won't qualify it as an error, but programmers who will have to maintain your code later surely will.

By removing code duplication, instead of:

startTime = time.strptime(request.GET.get('st', (dt.datetime.now() - dt.timedelta(days = 1)).strftime("%d/%b/%Y:%H:%M:%S")), "%d/%b/%Y:%H:%M:%S")

you get:

const dateFormat = "%d/%b/%Y:%H:%M:%S"
startTime = time.strptime(request.GET.get('st', (dt.datetime.now() - dt.timedelta(days = 1)).strftime(dateFormat)), dateFormat)

Another error is that you're doing too much in a single line. Who are you writing for? The compiler, or other developers (including yourself in six months)?

  • If you're writing for a compiler, indeed, collapsing everything into a big mess may give you a few microseconds advantage of compile time and a few microseconds of advantage every time the app is running. Or maybe there is no gain. Or maybe such code is slower. You have to profile it to know.

  • If you're writing for humans, and you should, then don't be greedy with whitespace, newlines, intermediary variables and functions. For example, all those parentheses together are a maintenance nightmare. I could hardly find what is closed where. It's also hard to understand what the code is doing just by looking at it.

What about:

const dateFormat = "%d/%b/%Y:%H:%M:%S"
dayAgo = (dt.datetime.now() - dt.timedelta(days = 1)).strftime(dateFormat)
webResponse = request.GET.get('st', dayAgo)
startTime = time.strptime(webResponse, , dateFormat)

Note that standards are good for they are. They are here for you, not the opposite. They should help you, not force you to spend hours doing meaningless changes.

If a standard tells that the line should be 80 characters long, but you have a perfectly valid case of a readable line with 81 characters in it, screw the standard.

In this particular case, the original line was indeed badly written, and it was a case where the standard gave you a hint that something was wrong. This is not always the case.

share|improve this answer
1  
No such thing as const in Python. –  Thijs van Dien Jul 24 '13 at 10:33
    
@ThijsvanDien: I'm unaware of the syntax used by Python, so my answer is language-agnostic. Feel free to change the syntax so it matches the question tags. Note: so there's no way to specify that a variable is a constant in Python? –  MainMa Jul 24 '13 at 10:40
    
I like parentheses!, says the lisper. –  Florian Margaine Jul 24 '13 at 11:24
    
Interesting to answer a language-specific style question in a language-agnostic way. ;) –  Thijs van Dien Jul 24 '13 at 11:51
1  
The code still does double, unnecessary work. Instead of formatting 'yesterday' with the date format, than parsing it back to a time tuple, the OP would be better off testing if 'st' in requests.GET is True, then parse that value, and otherwise just use (dt.datetime.now() - dt.timedelta(days = 1)).timetuple() instead. At the very least that saves formatting a datetime object when st is present in the request. –  Martijn Pieters Jul 24 '13 at 12:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.