• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

一个python实现的employee类

python 水墨上仙 2684次浏览

模拟C和Pascal的结构体实现的Employee类,可以从cvs文件读取信息并搜索

'''Class_StructEmp2_file.py
mimic a C Structure or Pascal Record using a class
load the data from a csv type file like this:
John Q. Johnson,computer research,3500
Loyd Tetris,Human Resources,6000
Mark Marksman,external development,4800
tested 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 = salary
def 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 file
fname = "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 instance
for 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 function
for 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 month
for 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 name
for emp in sorted(record_list, key=by_department):
    print("%s works in %s" % (emp.name, emp.dept))

”’result&nbsp–>
Employee&nbspnames:
John&nbspQ.&nbspJohnson
Loyd&nbspTetris
Mark&nbspMarksman
———————————–
Employee&nbspnames&nbspsorted&nbspby&nbsplast&nbspname:
John&nbspQ.&nbspJohnson
Mark&nbspMarksman
Loyd&nbspTetris
———————————–
High&nbsppaid&nbspemployees:
Loyd&nbspTetris&nbspgets&nbspmore&nbspthen&nbsp$4000&nbspa&nbspmonth
Mark&nbspMarksman&nbspgets&nbspmore&nbspthen&nbsp$4000&nbspa&nbspmonth
———————————–
Sorted&nbspby&nbspdepartment:
John&nbspQ.&nbspJohnson&nbspworks&nbspin&nbspcomputer&nbspresearch
Mark&nbspMarksman&nbspworks&nbspin&nbspexternal&nbspdevelopment
Loyd&nbspTetris&nbspworks&nbspin&nbspHuman&nbspResources
”’


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明一个python实现的employee类
喜欢 (0)
加载中……