Differences between revisions 2 and 3
Revision 2 as of 2010-12-20 22:30:12
Size: 3297
Editor: SteveLudtke
Comment:
Revision 3 as of 2010-12-20 22:36:31
Size: 3305
Editor: SteveLudtke
Comment:
Deletions are marked like this. Additions are marked like this.
Line 20: Line 20:
{{{ {{{#!python
Line 54: Line 54:
  parser.add_option("--verbose", "-v", dest="verbose", action="store", metavar="n",type="int", default=0, help="verbose level [0-9], hig
ner number means higher level of verboseness")
  parser.add_option("--verbose", "-v", dest="verbose", action="store", metavar="n",type="int", default=0, help='verbose level [0-9], hig
ner number means higher level of verboseness')

EMAN2 Python Programming Style Guide

This document describes the structure of programs distributed with EMAN2. If you plan to write a program for inclusion with EMAN2 in the 'bin' directory, you must follow the following general guidelines.

Program naming and options

All programs must be e2<program>.py This helps distinguish them from non-eman2 programs and from SPARX programs which are sx<program>.py

Where possible, the same options should be used across programs. For example, the '--verbose' option is required for all programs. See StandardParms for details.

Program sample code

This little example shows how all EMAN2 programs are expected to be structured.

Each program should include:

   1 #!/usr/bin/env python
   2 
   3 # Example Author block:
   4 # Author: Steven Ludtke (sludtke@bcm.edu), 10/27/2010 - rewritten almost from scratch
   5 # Author: David Woolford (woolford@bcm.edu), 9/7/2007 (woolford@bcm.edu)
   6 # Copyright (c) 2000-2010 Baylor College of Medicine
   7 
   8 # Official copyright notice. EMAN2 is distributed under a joint GPL/BSD license. Please copy
   9 # this statement from one of the other programs. You must agree to use this license if your
  10 # code is distributed with EMAN2. While you may use your own institution for the copyright notice
  11 # the terms of the GPL/BSD license permit us to redistribute it.
  12 
  13 # import block, any necessary import statements
  14 from EMAN2 import *
  15 from optparse import OptionParser
  16 import math
  17 
  18 # main() block. Each program will have a single function called main() which is executed when the
  19 # program is used from the command-line. Programs must also be 'import'able themselves, so you
  20 # must have main()
  21 def main():
  22 
  23   progname = os.path.basename(sys.argv[0])
  24   usage = """%prog <output> [options]
  25 
  26   This is the main documentation string for the program, which should define what it does an how to use it.
  27   """  
  28 
  29   # You MUST use OptionParser to parse command-line options
  30   parser = OptionParser(usage=usage,version=EMANVERSION)
  31         
  32   parser.add_option("--input", type="string", help="The name of the input particle stack", default=None)
  33   parser.add_option("--oneclass", type="int", help="Create only a single class-average. Specify the number.",default=None)
  34   parser.add_option("--verbose", "-v", dest="verbose", action="store", metavar="n",type="int", default=0, help='verbose level [0-9], hig
  35 ner number means higher level of verboseness')
  36 
  37   (options, args) = parser.parse_args()
  38 
  39   # Now we have a call to the function which actually implements the functionality of the program
  40   # main() is really just for parsing command-line arguments, etc.  The actual program algorithms 
  41   # must be implemented in additional functions so this program could be imported as a module and
  42   # the functionality used in another context
  43   data=EMData.read_images(args[1])
  44 
  45   results=myfunction(data,options.mine1,options.mine2)
  46 
  47   for im in results: im.write_image(args[2],-1)
  48 
  49 def myfunction(data,mine1,mine2):
  50   # do some stuff
  51   ret = [i*5.0 for i in data]
  52 
  53   return ret
  54 
  55 # This block must always be the last thing in the program and calls main()
  56 # if the program is executed, but not if it's imported
  57 if __name__ == "__main__":
  58     main()

EMAN2PythonStyleGuide (last edited 2022-05-06 18:14:36 by SteveLudtke)