Instructions
Requirements and Specifications
Source Code
import os
import re
import datetime
import sys
if __name__ == '__main__':
# PART 1
"""
homeFolder = 'C:\\'
userDir = os.getcwd()
desktopPath = os.path.join(homeFolder, 'Users', userDir, 'Desktop')
os.chdir(desktopPath)
"""
# PART 2
# First, print header
print("{:>20s}".format('-----------------------------------'))
print("{:>20s}".format('--- AIST 2120 ---'))
print("{:>20s}".format('--- Programming Assignment 6 ---'))
print("{:>20s}".format('--- CSV and JSON Files ---'))
print()
print(' ********** MOUNTAINER DOGS **********')
print(' ***** *****')
print('**** ANNUAL SALARY CALCULATIONS AND PROCESSING ****')
print()
print("The current working directory is ... ", os.getcwd())
# PART 3
print("BEGIN RPCOESSING .... ")
input_filename = 'EmpInfCur.txt'
# Open file
fin = open(input_filename, 'r')
# Read all lines
lines = fin.readlines()
# Skip first line because this one contains headers
lines.pop(0)
# Create a list to store the lines with correct data
correct_lines = list()
# Loop over the lines and if we found a line containing a SQL code, delete it
for i in range(len(lines)):
if re.search(r'(?=.*select)(?=.*from)', lines[i].lower()):
print("=> FOUND SQL INJECTION:", lines[i].strip())
print('\t\tSQL Removed')
print()
else:
correct_lines.append(lines[i])
# Now, get current month and year so we can create the output file name
date = datetime.datetime.now()
month = date.strftime("%B")[:3] # Get the first three letters only
# Get year
year = date.now().year
# Get day
day = date.now().day
# Get month id
month_id = date.now().month
# PART 4
# Create output filename
output_filename = "EmpPay" + month + str(year) + ".csv"
# Open file
fout = open(output_filename, 'w+')
# Write header
fout.write("NAME,CURRENT-SALARY,BONUS,NEW-COMPENSATION\n")
# Create variables to store the company total number of employees, salary, bonuses, etc
total_employees = 0
total_salary = 0
total_bonuses = 0
total_compensations = 0
# Now, extract employees name and salary
employees = list()
for line in correct_lines:
line = line.strip()
# The lines are separated by \t but some ones are separated by more than one \t, so
# using regex, we can remove all extra \t between words
line = re.sub('\t+','\t', line)
# Split
row = line.split('\t')
#print(row)
# Get name, jobtitle and salary
name = row[0]
job = row[1]
# Take the salary string and remove the commas
salary_str = row[2].replace(',', '')
salary = float(salary_str)
# Calculate bonuts as 5% of the salary
bonus = salary*0.05
# Create the line to be written in the output file
newline = "{0},{1:.2f},{2:.2f},{3:.2f}\n".format(name, salary, bonus, salary+bonus)
total_employees += 1
total_bonuses += bonus
total_salary += salary
total_compensations += (salary + bonus)
# Write to ouput file
fout.write(newline)
# Write totals
newline = "{0},{1:.2f},{2:.2f},{3:.2f}".format(total_employees, total_salary, total_bonuses, total_compensations)
fout.write(newline)
# Close file
fout.close()
# Now, read the output file
fin = open(output_filename, 'r')
print("UPDATED COMPENSATION VERIFIED from " + output_filename + ":")
print()
# Read lines
lines = fin.readlines()
# Skip first line
lines.pop(0)
# Get last line with totals
totals = lines.pop(len(lines)-1)
# Print header
print("{:>15s} {:>20s} {:>20s} {:>20s}".format("NAME", "CUR SAL", "BONUS", "TOTAL COMP"))
for line in lines:
# Strip
line = line.strip()
# Split
row = line.split(',')
name = row[0]
salary = float(row[1])
bonus = float(row[2])
compensation = float(row[3])
print("{:>15s} {:>20.0f} {:>20.0f} {:>20.0f}".format(name, salary, bonus, compensation))
print()
# Get totals
row = totals.split(',')
total_employees = int(row[0])
total_salary = float(row[1])
total_bonuses = float(row[2])
total_compensations = float(row[3])
# Print totals
print("{:>15s}".format("TOTALS"))
print("{:>15s} {:>5.0f} {:>20.0f} {:>20.0f} {:>20s} {:>5.0f}".format("Num. Emps.:", total_employees, total_salary, total_bonuses, "New COMP TOT:", total_compensations))
# Now, store date in a JSON file
jsonfile = open('dates.json', 'r+')
# Read content
content = jsonfile.read()
if len(content) > 0:
jsonfile.write(',\n')
jsonfile.write('{\n\t"day": ' + str(day) + ',\n')
jsonfile.write('\t"month": "' + month + '",\n')
jsonfile.write('\t"year": ' + str(year) + '\n}')
jsonfile.close()
print()
print(f"JSON FILE has been WRITTEN. It should contain {month_id}/{day}/{year}.")
print(f'*** SALARY OPERATIONS FOR {month_id}/{year} COMPLETE ***')
print()
print("Press any key to exit")
# Now, exit
sys.exit()