3

I recently started working with Python/Django and I heard about the PEP 8 conventions. After having read PEP8 I had a better sense how to 'style' my code but I learned programming with Java and I used to just do whatever I like. Could you suggest how to put my example into PEP-8? much appreciated.

result = urllib.urlretrieve(
                            "https://secure.gravatar.com/avatar.php?"+
                            urllib.urlencode({
                                            'gravatar_id': hashlib.md5(email).hexdigest(),
                                            'size': srt(size)
                                            })
                            )
yamm
  • 1,523
  • 1
  • 15
  • 25
  • 4
    This question appears to be off-topic because it is about codereview.stackexchange.com – Saullo G. P. Castro Sep 08 '14 at 13:29
  • mhh i thought this is a specific programming problem but im new to this forum so i guess you have your reasons for putting this thread on hold. Anyway i got the answers i was looking for. Thanks a lot. – yamm Sep 09 '14 at 06:34
  • I also find myself confused whether a question belongs here or in codereview or in http://programmers.stackexchange.com/, the latter for me has an almost identical purpose as StackOverflow... – Saullo G. P. Castro Sep 09 '14 at 07:27

3 Answers3

7

Try downloading code style linters such as pep8 (a program which checks your code to see if it matches PEP 8 requirements) or pylint. You can find a more comprehensive list and comparison of Python style checkers here: What are the comprehensive lint checkers for Python?

In fact, there's a pep8 checker available online: http://pep8online.com/

If we run your code through that, it tells you:

Code    Line  Column    Text 
E126    2     29        continuation line over-indented for hanging indent
E225    2     70        missing whitespace around operator
E126    4     45        continuation line over-indented for hanging indent
E501    4     80        line too long (90 > 79 characters)
W292    7     30        no newline at end of file 

The fixed version of your code would look more like this:

result = urllib.urlretrieve(
    "https://secure.gravatar.com/avatar.php?" +
    urllib.urlencode({
        'gravatar_id': hashlib.md5(email).hexdigest(),
        'size': srt(size)
    })
)

In essence, the main PEP 8 violation you had was that you were indenting too much. A single indent is fine -- you don't need to align to the opening paren of the function calls. Python is also insistent that your lines don't go beyond 80 characters, but fixing the over-indentation also fixed that issue.

Community
  • 1
  • 1
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • Deleted my answer - as you beat me to it and included info. about the checker... – Jon Clements Sep 08 '14 at 13:33
  • The only reason I'd use a line break after the opening paren is because the line would otherwise be too long, and then indenting it all the way to its original column doesn't help much... – RemcoGerlich Sep 08 '14 at 13:35
1

Use more variables. Not only are lines easier to read, the complete code is easier to understand:

base = "https://secure.gravatar.com/avatar.php"
params = urllib.urlencode({'gravatar_id': hashlib.md5(email).hexdigest(),
                           'size': srt(size)})
url = "{}?{}".format(base, params)
result = urllib.urlretrieve(url)
Jace Browning
  • 11,699
  • 10
  • 66
  • 90
0

This may not be suggested by PEP8, but for readability you could break it up like this:

base = "https://secure.gravatar.com/avatar.php?"
params = urllib.urlencode({'gravatar_id': hashlib.md5(email).hexdigest(),
                           'size': srt(size)})
result = urllib.urlretrieve(base+params)    

Note that autopep8 is a utility for formatting Python code to conform with PEP8. In this case, it transforms your original code to

result = urllib.urlretrieve(
    "https://secure.gravatar.com/avatar.php?" +
    urllib.urlencode({
    'gravatar_id': hashlib.md5(email).hexdigest(),
    'size': srt(size)
    })
)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677