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

From OpenWetWare

(Difference between revisions)
Jump to: navigation, search
(Typical template for Python script: add input attribute + example of error message)
(About programming: add tuto scientific python)
(45 intermediate revisions not shown.)
Line 6: Line 6:
| colspan="2"|
| colspan="2"|
<!-- ##### DO NOT edit above this line unless you know what you are doing. ##### -->
<!-- ##### DO NOT edit above this line unless you know what you are doing. ##### -->
-
==Typical template for Python script==
+
==About programming==
-
It is always rewarding on the long term to start any script 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 here is my typical template for any Python script:
+
* '''Resources''': most of the time, it's not necessary to buy a book, search online instead!
 +
** [http://software-carpentry.org/ Software Carpentry] to learn about tests, versioning, Makefile, regular expressions, etc
 +
** [http://www.tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide] by Mendel Cooper
 +
** my own page on [http://openwetware.org/wiki/User:Timothee_Flutre/Notebook/Postdoc/2011/11/07 R]
 +
** [http://www.cplusplus.com/doc/tutorial/ C++ tutorial], a must-read (and a [http://www.mycplus.com/featured-articles/best-free-programming-courses-online/ list] of the best free C/C++ resources online)
 +
** Python tutorials: for [http://www.tutorialspoint.com/python/index.htm everyone], [http://scipy-lectures.github.io/ scientists] ([http://www.sam.math.ethz.ch/~raoulb/teaching/PythonTutorial/index.html another]), [http://www.jchr.be/python/manuel.htm in French]
 +
** [http://resrc.io/list/10/list-of-free-programming-books/ list] of free programming books
-
    #!/usr/bin/env python
+
* '''Templates''': it is always rewarding on the long term to start any piece of computer software with a minimum amount of generic code (command-line options, help message, license, usage of gzipped files, running time, etc). But it's a pain to write all this every time, right? And often we know how to do something in one language but not in another. So below are my typical templates for any C++/Python/R/Bash program, as well as Beamer presentation.
-
   
+
** '''C++''': download the file [http://github.com/timflutre/quantgen/blob/master/myprogram.cpp myprogram.cpp], as well as [http://github.com/timflutre/quantgen/blob/master/utils_io.cpp utils_io.cpp] along with its header [http://github.com/timflutre/quantgen/blob/master/utils_io.hpp utils_io.hpp].
-
    # Author: Timothee Flutre
+
** '''Python''': download the file [http://github.com/timflutre/quantgen/blob/master/myprogram.py myprogram.py]
-
    # License: GPL-3
+
** '''R''': download the file [http://github.com/timflutre/quantgen/blob/master/myprogram.R myprogram.R]
-
    # Aim: does this and that
+
** '''Bash''': download the file [http://github.com/timflutre/quantgen/blob/master/myprogram.bash myprogram.bash]
-
    # help2man -o MyClass.man ./MyClass.py
+
** '''Latex-Beamer''': download the file [http://github.com/timflutre/quantgen/blob/master/myslides.tex myslides.tex]
-
    # 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()
+
 +
* '''Language-independent user documentation''': I'm a firm believer that it is necessary to add some user documentation, even  minimal, to any program. An easy way to do this is to simply generate such documentation from the "help" message, as long as it is "properly" formatted (see [http://www.gnu.org/s/help2man/ help2man]). The following commands work for any programming language:
 +
 +
<nowiki>
 +
help2man -N -o myprogram.man ./myprogram
 +
man ./myprogram.man
 +
groff -mandoc myprogram.man > myprogram.ps
 +
ps2pdf myprogram.ps myprogram.pdf
 +
</nowiki>
 +
 +
* '''Benchmarking''':
 +
** via the command-line: [http://man7.org/linux/man-pages/man1/time.1.html time] (see also [https://github.com/jhclark/memusg memusg])
 +
** in R: [http://cran.r-project.org/web/packages/rbenchmark/index.html rbenchmark]
<!-- ##### DO NOT edit below this line unless you know what you are doing. ##### -->
<!-- ##### DO NOT edit below this line unless you know what you are doing. ##### -->

Revision as of 08:52, 19 February 2014

Project name Main project page
Previous entry      Next entry

About programming

  • Templates: it is always rewarding on the long term to start any piece of computer software with a minimum amount of generic code (command-line options, help message, license, usage of gzipped files, running time, etc). But it's a pain to write all this every time, right? And often we know how to do something in one language but not in another. So below are my typical templates for any C++/Python/R/Bash program, as well as Beamer presentation.
  • Language-independent user documentation: I'm a firm believer that it is necessary to add some user documentation, even minimal, to any program. An easy way to do this is to simply generate such documentation from the "help" message, as long as it is "properly" formatted (see help2man). The following commands work for any programming language:
help2man -N -o myprogram.man ./myprogram
man ./myprogram.man
groff -mandoc myprogram.man > myprogram.ps
ps2pdf myprogram.ps myprogram.pdf


Personal tools