<!-- ##### 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!

* '''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].

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:

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: