Whats the best way to refactor this code to clean it up:
1) Selects from the db adding a column for the percentage difference between two columns
2) Loops through the values of the columns
3) If the date is in the past
4) If the price is greater than 500 and the percentage difference is less than 1st argument set flag to 1
5) Else if the price is less than 500 and the percentage difference is less than 2nd argument set flag to 1
6) Otherwise keep the flag as 0
def calculateEmployeeSpend(read_cursor, flag_higher_amount, flag_lower_budget):
read_cursor.execute("SELECT distinct b.employee_id, b.amount, "
"s.spend, b.date, b.amount - s.spend as spend_left, "
"100.0*(b.amount - s.spend) / b.amount As PercentDiff FROM employee_budget_upload "
"As b JOIN employee_budget_spent As s ON b.employee_id = s.employee_id where b.amount != 0")
for employee_id, amount, spend, date, spend_left, percent_diff in read_cursor:
flag=0
date_of_amount = dt.strptime(date, "%d/%m/%Y")
if date_of_amount <= dt.now():
if amount > 500 and percent_diff < int(flag_higher_amount):
flag=1
if amount < 500 and percent_diff < int(flag_lower_amount):
flag=1
Edit:
I have changed the ifs to one if:
if amount > 500 and percent_diff < int(flag_higher_amount) or amount < 500 and percent_diff < int(flag_lower_amount):
flag=1
flagbecomes1anyway?