User:Timothee Flutre/Notebook/Postdoc/2012/05/16

From OpenWetWare
Revision as of 08:06, 20 May 2012 by Timothee Flutre (talk | contribs) (→‎Typical templates for Python scripts and C++ programs: use nowiki instead of 4 spaces for Python code)
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Project name <html><img src="/images/9/94/Report.png" border="0" /></html> Main project page
<html><img src="/images/c/c3/Resultset_previous.png" border="0" /></html>Previous entry<html>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</html>Next entry<html><img src="/images/5/5c/Resultset_next.png" border="0" /></html>

Typical templates for Python scripts and C++ programs

It is always rewarding on the long term to start any piece of computer software with a minimum amount of generic code (verbose, command-line options, help message, license, etc). But it's a pain to write all this every time, right? So below are my typical templates for any Python script and C++ programs.

  • Python: it is assumed that the code below is copied into a file named "MyClass.py".
#!/usr/bin/env python

# Author: Timothee Flutre
# License: GPL-3
# Aim: does this and that
# help2man -o MyClass.man ./MyClass.py
# groff -mandoc MyClass.man > MyClass.ps

import sys
import os
import getopt
import time
import datetime
import math


class MyClass(object):
    
    def __init__(self):
        self.verbose = 1
        self.input = ""
        
        
    def help(self):
        msg = "`%s' does this and that.\n" % os.path.basename(sys.argv[0])
        msg += "\n"
        msg += "Usage: %s [OPTIONS] ...\n" % os.path.basename(sys.argv[0])
        msg += "\n"
        msg += "Options:\n"
        msg += " -h, --help\tdisplay the help and exit\n"
        msg += " -V, --version\toutput version information and exit\n"
        msg += " -v, --verbose\tverbosity level (0/default=1/2/3)\n"
        msg += " -i\tinput"
        msg += "\n"
        msg += "Examples:\n"
        print msg; sys.stdout.flush()
        
        
    def version(self):
        msg = "%s 0.1\n" % os.path.basename(sys.argv[0])
        msg += "\n"
        msg += "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
        msg += "This is free software; see the source for copying conditions.  There is NO\n"
        msg += "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
        print msg; sys.stdout.flush()
        
        
    def setAttributesFromCmdLine(self):
        try:
            opts, args = getopt.getopt( sys.argv[1:], "hVv:i:",
                                        ["help", "version", "verbose="])
        except getopt.GetoptError, err:
            sys.stderr.write("%s\n" % str(err))
            self.help()
            sys.exit(2)
        for o, a in opts:
            if o in ("-h", "--help"):
                self.help()
                sys.exit(0)
            elif o in ("-V", "--version"):
                self.version()
                sys.exit(0)
            elif o in ("-v", "--verbose"):
                self.verbose = int(a)
            elif o in ("-i"):
                 self.input = a
            else:
                assert False, "unhandled option"
                
                
    def checkAttributes(self):
        if self.input == "":
            msg = "ERROR: missing required argument -i"
            sys.stderr.write("%s\n\n" % msg)
            self.help()
            sys.exit(1)
            
            
    def run(self):
        self.checkAttributes()
        
        if self.verbose > 0:
            msg = "START %s" % time.strftime("%Y-%m-%d %H:%M:%S")
            startTime = time.time()
            print msg; sys.stdout.flush()
            
        # ... specific code ...
        
        if self.verbose > 0:
            msg = "END %s" % time.strftime("%Y-%m-%d %H:%M:%S")
            endTime = time.time()
            runLength = datetime.timedelta(seconds=
                                           math.floor(endTime - startTime))
            msg += " (%s)" % str(runLength)
            print msg; sys.stdout.flush()
            
            
if __name__ == "__main__":
    i = MyClass()
    i.setAttributesFromCmdLine()
    i.run()

  • C++: it is assumed that the code below is copied into a file named "myprogram.cpp" and that the file "utils.cpp" is present in the same directory.
/** \file myprogram.cpp
 *
 *  `myprogram' does this and that.
 *  Copyright (C) 2011,2012 Timothee Flutre
 *
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  g++ -Wall -fopenmp -O3 myprogram.cpp -lgsl -lgslcblas -o myprogram
 *  help2man -o .man ./myprogram
 *  groff -mandoc myprogram.man > myprogram.ps
*/

#include <cmath>
#include <ctime>
#include <getopt.h>

#include <iostream>
#include <string>
using namespace std;

#include "utils.cpp"

/** \brief Display the help on stdout.
*/
void help (char ** argv)
{
  cout << "`" << argv[0] << "'"
       << " does this and that." << endl
       << endl
       << "Usage: " << argv[0] << " [OPTIONS] ..." << endl
       << endl
       << "Options:" << endl
       << "  -h, --help\tdisplay the help and exit" << endl
       << "  -V, --version\toutput version information and exit" << endl
       << "  -v, --verbose\tverbosity level (0/default=1/2/3)" << endl
       << "  -i, --in\tinput" << endl
       << endl
       << "Examples:" << endl
       << "  " << argv[0] << " -i <input>" << endl
       << endl
       << "Remarks:" << endl
       << "  This is my typical template file for C++." << endl
    ;
}

/** \brief Display version and license information on stdout.
 */
void version (char ** argv)
{
  cout << argv[0] << " 0.1" << endl
       << endl
       << "Copyright (C) 2011,2012 T. Flutre." << endl
       << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" << endl
       << "This is free software; see the source for copying conditions.  There is NO" << endl
       << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." << endl
       << endl
       << "Written by T. Flutre." << endl;
}

/** \brief Parse the command-line arguments and check the values of the 
 *  compulsory ones.
 */
void
parse_args (
  int argc,
  char ** argv,
  string & input,
  int & verbose)
{
  int c = 0;
  while (1)
  {
    static struct option long_options[] =
      {
        {"help", no_argument, 0, 'h'},
        {"version", no_argument, 0, 'V'},
        {"verbose", required_argument, 0, 'v'},
        {"input", required_argument, 0, 'i'},
        {0, 0, 0, 0}
      };
    int option_index = 0;
    c = getopt_long (argc, argv, "hVv:i:",
                     long_options, &option_index);
    if (c == -1)
      break;
    switch (c)
    {
    case 0:
      if (long_options[option_index].flag != 0)
        break;
    case 'h':
      help (argv);
      exit (0);
    case 'V':
      version (argv);
      exit (0);
    case 'v':
      verbose = atoi(optarg);
      break;
    case 'i':
      input = optarg;
      break;
    case '?':
      printf ("\n"); help (argv);
      abort ();
    default:
      printf ("\n"); help (argv);
      abort ();
    }
  }
  if (input.empty())
  {
    fprintf (stderr, "ERROR: missing input (-i).\n\n");
    help (argv);
    exit (1);
  }
  if (! doesFileExist (input))
  {
    fprintf (stderr, "ERROR: can't find file '%s'.\n\n", input.c_str());
    help (argv);
    exit (1);
  }
}

int main (int argc, char ** argv)
{
  string input;
  int verbose = 1;
  
  parse_args (argc, argv, input, verbose);
  
  time_t startRawTime, endRawTime;
  if (verbose > 0)
  {
    time (&startRawTime);
    cout << "START " << argv[0] << " (" << time2string (startRawTime) << ")"
         << endl;
  }
  
  // ... specific code ...
  
  if (verbose > 0)
  {
    time (&endRawTime);
    cout << "END " << argv[0] << " (" << time2string (endRawTime)
         << ": elapsed -> " << elapsedTime(startRawTime, endRawTime)
         << ")" << endl;
  }
  
  return EXIT_SUCCESS;
}