Hi All, I'm a python newbie so please be kind. I've been reading book after book and have written a script or two but this is my first real "program". Just looking for any suggestions and pointers. I've done some work with bash scripts and php (not OOP) a while a go. I'm not a programmer but would like to possibly expand in to it (right now an IT guy). Am I on the right track so far?
The program should be documented enough to explain. """ Created on Tue Dec 21 13:39:41 2010 @author: jason Usage: cmd_drug_testing.py [options]... Will select a random employee from the local database (located in the current directory) and display the name by default. This program (Drug Testing) was written to help select employees for drug testing. Program usage: -h, --help Displays this help info -l, --list-emplys Lists all employees in the database -s, --select Select employee for testing -r, --remove-emply Delete employee from database, usage: -d employee_name or id -e, --edit-emply Edit employee data in database, usage: -e employee_name or id field_to_change new_value -i, --insert-emply Insert new employee in database: must fit this format -- "id:or '','lastname, firstname', 'email',0" -f Display database name and full path This program was written by, Jason S. For the use of AA. """ # import our needed modules import sqlite3 as sqlite import sys, getopt #set global vars global cursor global conn def usage(): """ this just prints the usage in the doc string""" print __doc__ def dbconnect(sql): """handel the connction to the sqlite db """ dbConnection = sqlite.connect("drug-test.db") #set the cursor cursor = dbConnection.cursor() try: #execute the sql passed from the other funtions and return the results result = cursor.execute(sql) dbConnection.commit() except sqlite.Error, e: result = e.args[0] return result def listEmployees(sql): #list all records in the database listemp = dbconnect(sql) for lst in listemp: print "%s, %s, %s, %s" % (lst[0],lst[1],lst[2],lst[3]) def selectOneEmployee(sql): #select a random record from the database listemp = dbconnect(sql) for empl in listemp: print empl[0] def removeOneEmployee(sqlchk,sqldel): #delete one employee by ID number chk = dbconnect(sqlchk) if chk.fetchone() != None: #check to make sure the ID is valid in the database emply = dbconnect(sqlchk) emply = emply.fetchall() print "trying to remove employee %s" % (emply) try: dbconnect(sqldel) except sqlite.Error, e: result = e.args[0] print result else: print "Employees ID is invalid, please check the ID number" def insertEmployee(sql): #insert a new employee into the database print sql try: dbconnect(sql) except sqlite.Error, e: result = e.args[0] print result def main(argv): """The purpose of this program is to select an empployee from this database for random drug testing. This program can also perform maintainance on same database.""" if argv == []: sql = "SELECT name FROM employees ORDER BY RANDOM() LIMIT 1;" print "The following employee has been selected\n" selectOneEmployee(sql) try: #get the options passed by the user opts, args = getopt.getopt(argv, "hlsr:e:i:d",["Help","list-emplys","select","remove-emply=","edit-emply=","insert-emply="]) except getopt.GetoptError: usage() sys.exit(2) #check throught the options and respond accordingly for opt, arg in opts: if opt in ("-h", "--help"): usage() sys.exit() elif opt == '-d': global _debug _debug = 1 elif opt in ("-l", "--list-emplys"): sql = "select * from employees" listEmployees(sql) elif opt in ("-s","--select"): sql = "SELECT name FROM employees ORDER BY RANDOM() LIMIT 1;" print "The following employee has been selected\n" selectOneEmployee(sql) elif opt in ("-r","--remove-emply"): if arg == "": sys.exit("You must provice the ID for the employee to remove") sqlchk = "select * from employees where id = \"%s\"" % (arg) sqldel = "delete from employees where id = \"%s\"" % (arg) removeOneEmployee(sqlchk,sqldel) elif opt in ("-i", "--insert-emply"): sql = "insert into employees values(%s)" % (arg) insertEmployee(sql) if __name__ == "__main__": main(sys.argv[1:]) ########## END ################### Thanks everyone. Jason ..·><((((º> -- http://mail.python.org/mailman/listinfo/python-list