Project

General

Profile

Actions

Python Coding Standards » History » Revision 2

« Previous | Revision 2/7 (diff) | Next »
Neil Voss, 03/19/2010 02:19 PM


Python Coding Standards

This document is a list of python coding standards. To add a new standard copy the template below and modify it.

see also http://emg.nysbc.org/wiki/index.php/Appionscripts_formatting_rules


Name of Coding Standard

Definition

What is the coding standard

Justification

Why is the coding standard important

Example

GOOD:

this is a good example code

BAD:

this is a bad example code


Python Coding Standards for AMI


Use tabs instead of spaces

Definition

Use tabs instead of spaces for inline code

Justification

It is important to be consistent. People like different sizes of columns, so like 8 spaces, others 4, 3, or 2. With tabs each individual can customize their viewer.

Example

GOOD:

if True:
<tab>while True:
<tab><tab>print "tab" 
<tab>break

BAD:

if True:
   while True:
        print "tab" 
   break


Checking beginning or ending of strings

Definition

Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.

Justification

startswith() and endswith() are cleaner and less error prone.

Example

GOOD:

if foo.startswith('bar'): 

BAD:

if foo[:3] == 'bar':


Never use from module import *

Definition

Never use from module import *, use import module instead

Justification

It is hard to track where functions come from when import * is used

Example

GOOD:

import numpy
a = numpy.ones((3,3))

BAD:

from numpy import *
a = ones((3,3))


Appion image data naming conventions

Definition

  1. never use 'image' or 'img'
  2. use 'imgdict' for image dictionaries
  3. use 'imgarray' for image numarrays
  4. use 'imgname' for image filenames
  5. use 'imgtree' for the main list of image dictionaries
  6. use 'imglist' for a list of image data

Justification

If you are consistent with your names people can read your code.

Example

GOOD:

for imgdict in imgtree:
    imgarray = imgdict['image']
    imgname = imgdict['filename']

BAD:

for image in imgs:
    array = image['image']
    name = image['filename']

Updated by Neil Voss over 14 years ago · 2 revisions