Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Code review request

Reply
Thread Tools

Code review request

 
 
Jason Staudenmayer
Guest
Posts: n/a
 
      12-22-2010
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 -- "idr '','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



...·><((((º>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is code review? (Java code review) www Java 51 05-15-2007 01:10 PM
Code Review Request pitoniakm@msn.com Java 2 06-30-2005 02:26 PM
Request for Code Review Ben Hanson C++ 19 07-03-2004 11:27 PM
Request for code review P Kenter C++ 4 06-02-2004 05:26 PM
Re: Accessing Request.InputStream / Request.BinaryRead *as the request is occuring*: How??? Brian Birtle ASP .Net 2 10-16-2003 02:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57