Python Coding Standards » History » Revision 4
« Previous |
Revision 4/7
(diff)
| Next »
Neil Voss, 03/19/2010 02:22 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, some 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¶
- never use 'image' or 'img'
- use 'imgdict' for image dictionaries
- use 'imgarray' for image numarrays
- use 'imgname' for image filenames
- use 'imgtree' for the main list of image dictionaries
- 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']
Use descriptive variables¶
Definition¶
Use descriptive variables, asdf
is not a variable.
Justification¶
No one understands shorthand variables.
Example¶
GOOD:
imgarray = mrc.read('leginon_image.mrc') particle1 = imgarray[47, 21] particle1 = imgarray[10, 15] class = [particle1, particle1]
BAD:
i = mrc.read('x.mrc') prtl1 = i[47, 21] prtl2 = i[10, 15] cls = [prtl1, prtl2]
Updated by Neil Voss almost 15 years ago · 4 revisions