Setting Up Aptana for Python Coding
As programmers we always need to keep in mind that our code will have to be read by other programmers (and us) in a later time. This means that instead of just focusing only on coding something that works, we have to be able to make it readable and maintainable. Some of my thoughts about this have been covered on a previous post, so now we will focus on how we can use Aptana to help us in this task, after going through some general tips on how to write more readable code in Python
Before we begin I feel I should first note that “beautiful” code isn’t always efficient code. After writing a fairly readable code base then it’s the time to see if some chunks can be optimized, and if this leads to a somewhat obscure code, then it’s the time to use in-line comments that explain what does it do and why has it turn so “ugly” (if you read my previous post, you’ll see that I believe that this type of comments, I believe shouldn’t exist in anyone’s code, since the code itself should be self-explanatory, but I guess this is one acceptable exception). Also, I will not cover writing idiomatic code for Python, since this depends on how “advanced” your team is on Python and this is supposed to be a catch-all guide.
Use 4 spaces per indentation level and never mix spaces with tabs.
Use up to 79 characters per line. If you ever have to use an old laptop to do your job you will be thankful about this. Also if you are using a big 27″ monitor, you will be able to have readable tabs next to each other, avoiding the useless whitespace caused by a few extra long lines. You can split long lines by using parentheses, brackets and braces taking advantage of Python’s implied line continuation. A backslash can also be used when implicit continuation is not applicable.
For top level functions and class declarations one line should be used. For methods inside a function two lines. Single blank lines can also be used inside a function to separate logical sections.
Expressions and Statements
No spaces immediately inside parentheses, brackets or braces.No spaces immediately before a comma, semicolon or colon.
No spaces immediately before open parentheses, brackets or braces.
Use one space (only) around comparison operators.No spaces around = when used to indicate a keyword argument or a default parameter value.
Avoid multiple statements in one line.
Exceptions: CamelCase ending with Error
Global Variables: lower_case
Functions and Methods Arguments: lower_case, using self as first argument for instance methods and cls for class methods
Method Names and Instance Variables: lower_case, _lower_case for non-public
Setting Up Aptana
First of all make sure that pythonNature and PyDev Builder are selected on Project Natures on the project’s properties.
Spaces, Indentation and print margin column (visual aid for the 80 characters per line limit)
Keep in mind that in both PyDev and PyLint I have deactivated specific errors that kept my Problems tab cleaner for the current state of the project. They are not meant to be part of the guide. You will have to decide if you want to disable specific warnings depending on your project
Bonus (Indentation Guides)
In order to have indentation guides to your workspace you must install the PDT tools Indent Guide plugin from here