My script looks for handshake files in an input folder to determine whether certain processes have completed. If it finds them, it will update the database with the current time stamp.
Things I am wondering:
- My
update_db
function takes 5 parameters. I have heard that in that case there is probably a better way to do it. Would this be a good candidate for creating aclass
and using an OO approach? - I am eventually going to be using a datetime to compare against the handshake timestamps, would it be difficult to somehow pull the file dates off our Linux server instead of creating them in the script?
- Finally, any inconsistencies, ways I can simplify/improve my code. I am a new programmer of only 6 months, and love to learn:)
Here's the code:
def main():
# Get current time stamp
now = datetime.now()
formatted_dt = '{0}/{1}/{2}-{3}:{4}:{5}'.format(now.month,
now.day,
now.year,
now.hour,
now.minute,
now.second)
# Connect to db
sqlite_file = '/bcs/lgnt/clientapp/csvbill/database/csv.db'
csv_connection = sqlite3.connect(sqlite_file)
c = csv_connection.cursor()
file_type = ['auditv2', 'handshake']
# Check for files that don't have first hshake
c.execute('SELECT pivot_id FROM aud_qty WHERE hshake1 is NULL')
piv_id_list = [str(x[0]) for x in c.fetchall()]
missing_list = update_db('CSV1000', file_type[0], piv_id_list, c, formatted_dt)
# hshake1_miss = check_hshake('CSV1000', file_type[0], piv_id_list)
# print(hshake1_miss)
csv_connection.commit()
csv_connection.close()
def update_db(proj_id, audit_type, piv_id_list, db_cursor, time_stamp):
# chdir('/prod/bcs/lgnp/input')
# prod_list = listdir('/prod/bcs/lgnp/input/')
# for testing
chdir('/bcs/lgnt/input')
prod_list = listdir('/bcs/lgnt/input/')
hshake_list = []
for i, e in enumerate(prod_list):
prod_list[i] = prod_list[i].split('_') # Field 6 is auditv2, Field 7 is the Pivot ID
# Get only the Pivot ID
for i, e in enumerate(prod_list):
try:
if prod_list[i][0] == proj_id and prod_list[i][6] == audit_type:
hshake_list.append(prod_list[i][7])
except IndexError:
pass
# pdb.set_trace()
for i, e in enumerate(hshake_list):
if hshake_list[i] in piv_id_list:
print(hshake_list[i])
# pdb.set_trace()
db_cursor.execute('UPDATE aud_qty SET hshake1 = ? WHERE pivot_id = ?',
(time_stamp, hshake_list[i]))
piv_id_list.remove(hshake_list[i])
print(piv_id_list)
return piv_id_list