CGRU/cgru — pull request #94 by eoyilmaz (2014-07-21 18:39:31+00:00)
Title: Fixing all Python code to comply with PEP8 rules

Hi,

Newly started contributing your great library.

I'm a Pipeline Developer and I'm also developing Stalker (which is presented at Libre Graphics Metting 2014 by Oliver Markovsky along with your libraries).

Anyway because we have switched to Afanasy as our render manager I wanted to start contributing the library. And because I'm mainly a Python developer I wanted to start from Python files.

What I've seen is that nearly non of the Python code included in the library is following PEP8 rules which will help a lot for other developers to contribute/read the code easily.

I've tried to summarize the problems that I've tried to fix (hopefully without breaking anything):

1- Nearly all of the exception clauses are too broad, they are not specifying any expected Error class, it is just written like:

try:
#-- commands --
except:
#-- commands --

Where it should be:

try:
# commands
except {{ExpectedExceptionClass}}:
# commands

2- Used Tabs instead of Spaces.
3- And when spaces are used there are 3 spaces instead of 4 or 8
4- "if" statements and the "True condition" commands have nearly always written in 1 line, which breaks the readability harshly.
5- Class names doesn't follow CamelCase naming convention.
6- Variable names doesn't follow "lowercase split with '_'" rule (my_variable instead of myVariable).
7- When something compared to None "is" should be used instead of "==" and "is not" instead of "!="
8- I've seen arguments with default values are set to an empty list, which will cause unexpected behaviour because lists are mutable:

def func1(a=[]): # this is wrong
a.append(1)
return a

a = func1
print a
# will return [1]
# but
b = func1
print b
# will return [1, 1]
# because it is the same list
a is b # will return True

# instead do this
def func1(a=None)
if a is None:
a = []
a.append(1)
return a

9- There are variables shadowing Python built-in names like (object, id etc.)
10- No DocStrings are used at all. Please add a multi line string at the very first line of a Class, method or function and explain what it does, what it accepts as parameters and what it returns at the end::

def func1(a, b):
"""This is a test function and does nothing

:param str a: The name of the file to be replaced (for example). Should
be a string, anything else will raise a TypeError.
:param int b: A positive integer value representing the number of tries,
anything negative will be clamped to 0.
"""
pass

class MyClass(object):
"""This is my important class and helps me doing something.

(Below we list the arguments that __init__ accepts)
:param a: --explain it--
:param b: --explain it--
"""

def __init__(self, a, b):
# we do not add a doc string to the __init__ method
pass

This will help a lot for the other developers to learn about your intention of writing the code. And there are great auto documentation tools like Sphinx, which will convert all these DocStrings in to HTML or PDF files.

11- Too many string concatenation. Instead of doing something like:

a + b + ' some string ' + c + d

Please use:

'%s%s some string %s%s' % (a, b, c, d)

or the new string formatting:

'%(a)s%(b)s some strring %(c)s%(d)s' % {
'a': a,
'b': b,
'c': c,
'd': d
}

# the second form will allow you to use parameters more than once

of for very long "cmd += something" kind of commands use a list as a buffer and then join them:

# so instead of
cmd = ''
cmd += a
# or even
cmd = cmd + b

# use
cmd_buffer = []
cmd_buffer.append(a)
cmd_buffer.append(b)

cmd = ' '.join(cmd_buffer)

# the second form will work hundreds of times faster almost on every
# language out there

12- No space in the code. Your code is drowning in to itself. Give a little space to your code by adding empty lines, I bet your eyes will not strain too much.

13- Use 80 columns in python files.

Excuse me if I'm going too far, I just want to contribute to your great library which we have already started using as our main render farm manager in Anima Istanbul.

Also I'm replaced the "afanasy.mel" UI with a Python version which is able to submit render layers as separate tasks.