~george-edison55/audataserver-proxy/trunk

2 by Stefano Palazzo
pep8, added small styleguide I had lying around
1
# The 60 second guide to being Pythonic, whatever that means
2
3
 - ## Write documentation as you go along
4
5
    - The first lone string in every method and class is called a "docstring".
6
      You can use it to explain *how to use* the method (not how it works).
7
8
        def f(x):
9
            ''' square x '''
10
            return x**2
11
12
 - ## Take the time to read PEP8
13
14
   These are the most important points it makes:
15
16
    - Two blank lines after top-level functions and classes,
17
      one blank line after methods. Sprinkle blank lines sparingly
18
19
    - Classes are UpperCamelCase, functions are underscore_separated_lowercase
20
      Don't mix the two.
21
22
    - Indentation is always four spaces. Don't be a hero. ;-)
23
24
    - Lines may never be longer than 79 characters.
25
      You can always insert a line break in between parantheses
26
27
        print(this_line_is_long(so_break(
28
            "it down", "at the parantheses")))
29
30
    - Always put spaces around operators (excepting perhaps "-a" and "~a")
31
32
        DON'T:  (x<<n)|(x>>(32-n))
33
        DO:     (x << n) | (x >> (32 - n))
34
35
    - **Install the "pep8" programm and run your code through it constantly**
36
37
 - ## Use advanced language features
38
39
    - Instead of writing "if x == False", write "if not x" or "if x"
40
41
    - Don't build a list if you don't have to, use lazy evaluation
42
43
      - Instead of "[f(x) for x in data]", write "(f(x) for x in data)",    
44
        these can be nested. f(x) will only be calculated when you actually
45
        get the item
46
47
    - **Carefully check the *standard library*. It's likely the code you're
48
      writing already ships with python, is written in C, and by someone
49
      with more computer science degrees than you can shake a stick at**
50
51
      For example: You will very probably never have to write a
52
      sorting algorithm in python. Use sorted(my_iterable) instead
53
54
Okay, 60 seconds was a bit optimistic.
55