Skip to content

Instantly share code, notes, and snippets.

@calebrob6
Last active October 28, 2024 04:04
Show Gist options
  • Save calebrob6/eb98725187425c527567 to your computer and use it in GitHub Desktop.
Save calebrob6/eb98725187425c527567 to your computer and use it in GitHub Desktop.
General python program template
#!/usr/bin/env python
import sys,os
import time
import argparse
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
def doArgs(argList, name):
parser = argparse.ArgumentParser(description=name)
parser.add_argument('-v', "--verbose", action="store_true", help="Enable verbose debugging", default=False)
parser.add_argument('--input', action="store", dest="inputFn", type=str, help="Input file name", required=True)
parser.add_argument('--output', action="store", dest="outputFn", type=str, help="Output file name", required=True)
return parser.parse_args(argList)
def main():
progName = "Template"
args = doArgs(sys.argv[1:], progName)
verbose = args.verbose
inputFn = args.inputFn
outputFn = args.outputFn
print "Starting %s" % (progName)
startTime = float(time.time())
if not os.path.isfile(inputFn):
print "Input doesn't exist, exiting"
return
outputBase = os.path.dirname(outputFn)
if outputBase!='' and not os.path.exists(outputBase):
print "Output directory doesn't exist, making output dirs: %s" % (outputBase)
os.makedirs(outputBase)
print "Finished in %0.4f seconds" % (time.time() - startTime)
return
if __name__ == '__main__':
#sys.argv = ["programName.py","--input","test.txt","--output","tmp/test.txt"]
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment