mandriva
 

MDS Style Guide for Python Code

Cédric Delfosse

Revision History
Revision $Revision: 4266 $ $Date: 2009-06-17 18:23:12 +0200 (Wed, 17 Jun 2009) $ $Author: cdelfosse $

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.

7. Python language version compatibility

The code must be compatible with Python 2.3. That's a rather old version, but we never had any problems that forced us to use a newer version.

8. Python additional library compatibility

The code must be compatible with these library versions:

  • Python wisted: 8.1.0

  • Python LDAP: 2.0

  • Python SQLAlchemy: 0.4

9. Python code copyright header

Here is the header that must be used:

# -*- coding: utf-8; -*-
#
# (c) 2004-2007 Linbox / Free&ALter Soft, http://linbox.com
# (c) 2007-2009 Mandriva, http://www.mandriva.com
#
# $Id$
#
# This file is part of Mandriva Management Console (MMC).
#
# MMC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# MMC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MMC.  If not, see <http://www.gnu.org/licenses/>.