Skip to content

Instantly share code, notes, and snippets.

@phanthaihuan
Forked from calebrob6/template.py
Created August 14, 2021 17:45
Show Gist options
  • Save phanthaihuan/d2f365d10158469360080b1f412d1691 to your computer and use it in GitHub Desktop.
Save phanthaihuan/d2f365d10158469360080b1f412d1691 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