Skip to content

Instantly share code, notes, and snippets.

@billtaichi
Last active September 10, 2022 13:06
Show Gist options
  • Save billtaichi/5866974 to your computer and use it in GitHub Desktop.
Save billtaichi/5866974 to your computer and use it in GitHub Desktop.
Example class that uses argparse for python
# -*- coding: utf-8 -*-
import argparse
parser = argparse.ArgumentParser(description='Run Load',
epilog="Thanks for using PF dataloads.")
parser.add_argument('-u', '--url',
help='Base URL')
parser.add_argument('-l', '--logon',
help='Logon user name')
parser.add_argument('-o', '--logonorgid',
help='Logon org id')
parser.add_argument('-p', '--password',
help='Password for logon')
parser.add_argument('-r', '--regionid',
help='RegionID to add reqs in')
parser.add_argument('-x', '--excelpath',
help='Path to Excel file')
parser.add_argument('-s', '--sheetname',
help='Sheetname in Excel file to use for dataload')
parser.add_argument('-g', '--logfilename',
help="Name of logfile (include extension)")
def results():
return parser.parse_args()
def printVars():
res = results()
print("url = {}".format(res.url))
print("logon = {}".format(res.logon))
print("password = {}".format(res.password))
print("logonorgid = {}".format(res.logonorgid))
print("regionid = {}".format(res.regionid))
print("excelpath = {}".format(res.excelpath))
print("sheetname = {}".format(res.sheetname))
print("logfilename = {}".format(res.logfilename))
def main():
printVars()
if __name__ == '__main__':
main()
#Example of use
from urlparse import urlparse, urlsplit
import pdb
vrs = {'url': 'http://vms.peopleclick.com',
'logon': 'pc-autoload',
'password': 'welcome1',
'logonorgid': 'i4255',
'regionid': '9260',
'excelpath': 'U:\dataloadscripts\dataloadscripts\PolyoneEngageContractors\POLY1PRODENGAGEMENTS.xls',
'sheetname': 'ME',
'logfilename': 'test.log',
'currentregion': 'ALL'
}
#main program here############################
#############################################
#***************************************************************************************************
def printvrs():
print("****Current command vrs*****")
for k, v in vrs.iteritems():
print("{0} = {1}".format(k, v))
print("*****************************")
def main(vrs):
"""
Sets the variables and starts the load process
:param vrs: the vrs dictionary for the processing class
"""
if not args.results().url is None:
vrs['url'] = args.results().url
if not args.results().logon is None:
vrs['logon'] = args.results().logon
if not args.results().password is None:
vrs['password'] = args.results().password
if not args.results().regionid is None:
vrs['regionid'] = args.results().regionid
if not args.results().excelpath is None:
vrs['excelpath'] = args.results().excelpath
if not args.results().sheetname is None:
vrs['sheetname'] = args.results().sheetname
if not args.results().logfilename is None:
vrs['logfilename'] = args.results().logfilename
printvrs()
if __name__ == '__main__':
try:
main(vrs)
vh = vl.VmsHandler(vrs['url'],
vrs['logon'],
vrs['password'],
vrs['logonorgid'],
vrs['regionid'],
vrs['excelpath'],
vrs['sheetname'],
vrs['logfilename'])
ProcessData(vh)
except Exception as e:
print(e.message)
@Mahrjose
Copy link

Not class -_-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment