In: Computer Science
Python, filing and modules
Modify the driver to write the statistics to a file named employee-stats.txt rather than to the console.
driver.py code:
from employee import Employee
employees = []
# Load an employee object for each employee specified in
'employees.txt' into the employees list.
employee.txt
Jescie Craig VP 95947
Marny Mckenzie Staff 52429
Justin Fuller Manager 72895
Herman Love VP 108204
Hasad Wilkins Staff 96153
Aubrey Eaton Staff 76785
Yvonne Wilkinson Staff 35823
Dane Carlson Manager 91524
Emma Tate Staff 72691
Bryar Bonner Manager 48754
Allegra Jefferson Manager 93610
Richard Stokes Manager 78085
Kimberley Reese Staff 80363
Christian Collins Staff 48864
Colette Casey Staff 27617
Eagan Duran Manager 82666
Aladdin Nash Manager 50925
Jeremy Johnson Staff 58550
Lillith Dawson Manager 59160
Clinton Santos Staff 37320
Tanner Workman Staff 68775
Ashely Mcgee Manager 70635
Blake Peters Manager 44196
Judah Beasley Staff 63186
Alexis Lindsay Staff 36208
Audra Randall Manager 89997
Edward Pope Staff 66393
Jamalia Mccarty Manager 46497
Elvis Rutledge Staff 41969
Paloma Moody Staff 47076
Dominic Long Staff 44557
Candace Moss Manager 85314
Clinton Hardy Staff 56247
Sara Dominguez Manager 62829
Yardley Crawford Manager 89149
Ahmed Hendricks Staff 65129
Fletcher Freeman Staff 34221
Rhiannon Horton Staff 51563
Reagan Hunter Manager 98812
Harper Mason Staff 57284
Owen Crawford Manager 95506
Lois Melendez Staff 72098
Abraham Pugh Staff 32891
Hiroko Hood Staff 60979
Cathleen Horn Manager 75033
Kim Hanson Manager 40919
Shad Contreras Manager 95000
Ezekiel Estes Staff 73942
Liberty Rodgers Staff 39964
Ayanna Gentry Staff 57357
Cassidy Sullivan Manager 49648
Leonard Dillard Staff 53930
Dylan Spencer Staff 72950
Hunter Mckay Staff 68908
Hyacinth Olson Manager 93401
Tanya Bryant Manager 76784
Lucian Suarez Staff 28799
Liberty Garner Staff 66861
Wayne Russell Manager 75801
Lesley Hale Staff 48976
Mona Mckee Manager 87712
Gisela Winters Manager 85863
Tad Curtis Staff 20106
Willow Armstrong Manager 70109
Lesley Brock Manager 75828
Rogan Mccullough Staff 36150
Vaughan Wall Manager 70485
Dillon Hayden Staff 69036
Quynn Fisher Staff 87038
Paul Donaldson Staff 76983
Alea Blankenship Manager 59578
Gil Bush Staff 77769
Odessa Hopper Staff 59715
Galvin Clayton Manager 89641
Ila Zamora Manager 33256
Frances Donovan Manager 81614
Latifah Charles Manager 38059
Jameson Wiley Staff 65428
Cameran Mccall Staff 22922
Cameron Avery Manager 39209
Riley Tillman Staff 34452
Quin Cox Staff 49874
Hayfa Gamble Staff 57834
Whilemina Mccall Staff 50688
Josiah Chandler Staff 64149
Logan Lindsey Staff 77778
Elaine Bullock Manager 36552
Heather Lucas Manager 78369
Carlos Hamilton Staff 47046
# open the file in read mode
file = open('employees.txt')
line = file.readline()
# loop continues till it reach the end of file
while line:
data = line.strip().split(' ')
employees.append(Employee(data[0],data[1],data[2],int(data[3])))
line = file.readline() # read the next line
# close the file
file.close()
file = open('employee-count.txt','w')
max_employee=employees[0]
min_employee=employees[0]
totals={}
counts={}
for i in range(len(employees)):
#Check for maximum salary
if(employees[i].salary>max_employee.salary):
max_employee=employees[i]
#Check for minimum salary
if(employees[i].salary<min_employee.salary):
min_employee=employees[i]
if(employees[i].rank in totals):
totals[employees[i].rank]+=employees[i].salary
counts[employees[i].rank]+=1
else:
totals[employees[i].rank]=employees[i].salary
counts[employees[i].rank]=1
print("Maximum Salary:",max_employee)
print("Minimum Salary:",min_employee)
print("Rank\tAverage Salary")
for k in totals.keys():
avg=totals[k]/counts[k]
st="{0:.2f}".format(avg)
print(k+"\t"+st)
employee.py
class Employee:
""" Represents an employee object.
Invariant:
- Salary is an integer.
"""
def __init__(self, first='John', last='Doe', rank='Staff',
salary=10000):
"""Instantiate a new employee object, defaulting to a basic John
Doe."""
self.first = first
self.last = last
self.rank = rank
self.salary = salary
def __str__(self):
"""Create a employee string with a simple format."""
return self.last + ', ' + self.first + ': ' + self.rank + ' ($' +
str(self.salary) + ')'
if __name__ == '__main__':
print(str(Employee()) == 'Doe, John: Staff ($10000)')
print(str(Employee('Jane', 'Doe', 'Czar', 99999)) == 'Doe, Jane:
Czar ($99999)')
PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING
Language : Python
IDE : Python IDLE
Input Filename : employee.txt
Output Filename : employee-stats.txt
:::::::::::::::::::::::::::::::::::::::::::: CODE ::::::::::::::::::::::::::::::::::::::::
driver.py
from employee import Employee
employees = []
# Load an employee object for each employee specified in
# 'employees.txt' into the employees list.
# open the file in read mode
file = open('employee.txt','r')
line = file.readline()
# loop continues till it reach the end of file
while line:
data = line.strip().split()
employees.append(Employee(data[0],data[1],data[2],int(data[3])))
line = file.readline() # read the next line
# close the file
file.close()
# open file in write mode to write employee stats
file = open('employee-stats.txt','w')
# to store maximum salaried employee
max_employee=employees[0]
# to store minimum salaried employee
min_employee=employees[0]
# to store employee salaries with particular rank
totals={}
# to store employee count of particular rank
counts={}
# loop to find minimum, maximum and rank totals &
counts
for i in range(len(employees)):
#Check for maximum salary
if(employees[i].salary>max_employee.salary):
max_employee=employees[i]
#Check for minimum salary
if(employees[i].salary<min_employee.salary):
min_employee=employees[i]
# find employee rank and update total salaries for rank
if(employees[i].rank in totals):
totals[employees[i].rank]+=employees[i].salary
counts[employees[i].rank]+=1
else:
totals[employees[i].rank]=employees[i].salary
counts[employees[i].rank]=1
# write maximum salary to file with newline at end
file.write("Maximum Salary: "+str(max_employee)+"\n")
# write minimum salary to file with newline at end
file.write("Minimum Salary: "+str(min_employee)+"\n")
# write heading to file with newline at end
file.write("Rank\tAverage Salary\n")
# loop for all ranks
for k in totals.keys():
# calculate average
avg=totals[k]/counts[k]
# create string with 2 decimal point for average
st="{0:.2f}".format(avg)
# write rank and average to file with newline at end
file.write(k+"\t"+st+"\n")
# close file
file.close()
_________________________________________________________________
Employee.py
class Employee:
""" Represents an employee object.
Invariant:
- Salary is an integer.
"""
def __init__(self, first='John', last='Doe', rank='Staff',
salary=10000):
"""Instantiate a new employee object, defaulting to a basic John
Doe."""
self.first = first
self.last = last
self.rank = rank
self.salary = salary
def __str__(self):
"""Create a employee string with a simple format."""
return self.last + ', ' + self.first + ': ' + self.rank + ' ($' +
str(self.salary) + ')'
if __name__ == '__main__':
print(str(Employee()) == 'Doe, John: Staff ($10000)')
print(str(Employee('Jane', 'Doe', 'Czar', 99999)) == 'Doe, Jane:
Czar ($99999)')
:::::::::::::::::::::::::::::::::::::::::::: OUTPUT
::::::::::::::::::::::::::::::::::::::::
employee-stats.txt
:::::::::::::::::::::::::::::::::::::: CODE in EDITOR ::::::::::::::::::::::::::::::::::
driver.py
Employee.py
_________________________________________________________________
Dear Friend, Feel Free to Ask Any Doubts by Commenting. ASAP i'll respond when i'm available.
I'm on a critical Situation. Please Do Not Forget To Give A Thumbs UP +1. It will Helps me A Lot.
Thank YOU :-)