一个python实现的employee类,python实现employee,'''Class_Str


'''Class_StructEmp2_file.pymimic a C Structure or Pascal Record using a classload the data from a csv type file like this:John Q. Johnson,computer research,3500Loyd Tetris,Human Resources,6000Mark Marksman,external development,4800tested with Python27 and Python32  by  vegaseat'''class Employee():    """    mimics a C Structure or Pascal Record    """    def __init__(self, name, dept, salary):        self.name = name        self.dept = dept        self.salary = salarydef by_last_name(record):    """    helper function to sort by last name    assume names are "first middle last"    """    return record.name.split()[-1]def by_department(record):    """    helper function to sort by department case-insensitive    """    return record.dept.lower()# load the data filefname = "employee_data.txt"with open(fname, "r") as fin:    employee_data = fin.readlines()record_list = []for line in employee_data:    line = line.rstrip()    #print(line, type(line), line.split(','))  # test    name, dept, salary = line.split(',')    #print(name, dept, salary)  # test    record_list.append(Employee(name, dept, salary))print("Employee names:")# explore the record_list by instancefor emp in record_list:    print(emp.name)print('-'*35)print("Employee names sorted by last name:")# sort the records by last name using a helper functionfor emp in sorted(record_list, key=by_last_name):    print(emp.name)print('-'*35)print("High paid employees:")# list the folks that get more than $4000 salary per monthfor emp in record_list:    if float(emp.salary) > 4000:        print("%s gets more then $4000 a month" % emp.name)print('-'*35)print("Sorted by department:")# sort the records by case insensitive department namefor emp in sorted(record_list, key=by_department):    print("%s works in %s" % (emp.name, emp.dept))

'''result --;>Employee names;:John Q.; Johnson;Loyd Tetris;Mark Marksman;


Employee names; sorted; by; last; name;:John Q.; Johnson;Mark Marksman;Loyd Tetris;


High paid; employees;:Loyd Tetris; gets; more; then; $4000 a; month;Mark Marksman; gets; more; then; $4000 a; month;


Sorted by; department;:John Q.; Johnson; works; in; computer; research;Mark Marksman; works; in; external; development;Loyd Tetris; works; in; Human; Resources;'''

评论关闭