mandriva
 

MDS Style Guide for Python Code

Cédric Delfosse

Revision History
Revision $Revision: 57 $ $Date: 2007-10-04 18:24:20 +0200 (Thu, 04 Oct 2007) $ $Author: cedric $

Abstract

Coding conventions for the Python code of all MDS components


1. Introduction

A lot of MDS components are written in Python, among them the MMC agent and its Python plugin.

This document sets the coding conventions for the Python code of all MDS components.

This document is totally based on Guido Van Rossum "Style Guide for Python Code" document (see http://www.python.org/dev/peps/pep-0008/): you must read it too. This document only emphases on important coding conventions.

2. Code layout

Indentation: use 4 spaces per indentation level, no tabs allowed. It's ok with Emacs Python mode.

Encoding: the source code must always use the UTF-8 encoding.

3. Whitespace in Expressions and Statements

      Yes: spam(ham[1], {eggs: 2})
      No:  spam( ham[ 1 ], { eggs: 2 } )

      Yes: if x == 4: print x, y; x, y = y, x
      No:  if (x == 4): print x, y; x, y = y, x
      No:  if x == 4 : print x , y ; x , y = y , x

      
      Yes: spam(1)
      No:  spam (1)
      
      Yes: dict['key'] = list[index]
      No:  dict ['key'] = list [index]
      
      Yes:      
      x = 1
      y = 2
      long_variable = 3
      
      No:
      x             = 1
      y             = 2
      long_variable = 3
    

4. Comments

They are written in english.

They always start with a capitalized first word.

There is always a space between the # and the begin of the comment.

5. Docstrings

All functions and classes must have a docstring.

The docstring must be written in the Epytext Markup Language format. We use epydoc to generate the API documentation. See http://epydoc.sourceforge.net/epytext.html and http://epydoc.sourceforge.net/fields.html for more information.

6. Naming conventions

Module name: short, lowercase names, without underscores

Class Names: CapitalizedWords

Functions Names: mixedCase for instance method, lower_case_with_underscores for other.