码迷,mamicode.com
首页 > 其他好文 > 详细

员工信息表-装逼版

时间:2018-07-31 15:23:25      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:rate   方便   动作   opera   抓取   port   用户   输出   log   

import os
file_name = 员工信息表
noun_list = [staff_id, name, age, phone, dept, enroll_date]


def file_operation(file):
    ‘‘‘
    先进行文件操作,把文件内容加载到内存,以方便调用的形式储存文件信息
    :param file_name:
    :return:
    ‘‘‘

    file_info = open(file, r, encoding=utf-8)
    data_dic = {staff_id: [], name: [], age: [], phone: [], dept: [], enroll_date: []}
    for line in file_info:
        info_id, info_name, info_age, info_tel, info_dept,info_time=line.split(,)
        # 按照上述的顺序依次把信息存入value的空列表中
        data_dic[staff_id].append(info_id)
        data_dic[name].append(info_name)
        data_dic[age].append(info_age)
        data_dic[phone].append(info_tel)
        data_dic[dept].append(info_dept)
        data_dic[enroll_date].append(info_time)
    file_info.close()
    return data_dic


def file_save():
    f = open(临时存放,w,encoding=UTF-8)
    for id_index, id in enumerate(data[staff_id]):
        info_list = []
        for noun in noun_list:
            info_list.append(data[noun][id_index])
        f.write(,.join(info_list))
    f.close()
    os.replace(临时存放,file_name)


def catch_order():   #获取用户指令  # step:1
    while True:
        user_order = input(请输入指令:\n--->\t).strip()  # 交互第一步,让用户输入指令,首尾去空格
        if not user_order: continue
        input_order(user_order)  # 把用户指令传入(指令分析)的函数  # 2


def print_login(msg,login_type=info):
    if login_type == info:
        print(\033[31;1m%s\033[0m %msg)
    elif login_type == error:
        print(\033[41;1m%s\033[0m %msg)


def input_order(order):   # step:2
    ‘‘‘
    获取并分析用户输入的指令
    :return:
    ‘‘‘
    judge = {find: find_info, add: add_info, del: del_info, update: update_info}
    if order.split()[0] in (find, add, del, update):  # 查看指令的开头写的动作,是否在支持的范围内
        if wherein order:
            left_order, right_order = order.split(where)   # 以where为界,把信息分为两半,左半是动作,右半是条件
            catch_info = judge_right(right_order)  # 把右半边的筛选条件传入(判断where)的函数里面  # 3 -> 5
           # 把抓取的信息和具体的操作指令传入操作函数中
            info_action = order.split()[0]
            if info_action in judge:
                judge[info_action](catch_info, left_order)
        else:
            catch_info = []
            for id_index, id in enumerate(data[staff_id]):
                info_list = []
                for noun in noun_list:
                    info_list.append(data[noun][id_index])
                catch_info.append(info_list)
            info_action = order.split()[0]
            if info_action in judge:
                judge[info_action](catch_info, order)

    else:
        print_login(输入有误,无法获取具体指令!)


def judge_greater(n, c):   # 判断语句中有大于号的情况 # step:4
    ‘‘‘
    判断大于的情况
    :param n: 这是要筛选的对象名称(年龄,职位...)
    :param c: 这是要筛选的具体条件(IT,22, 2017...)
    :return:  把筛选出的内容
    ‘‘‘
    staff_info_1 = []
    for index,condition in enumerate(data[n]):
        if float(condition) > float(c):  # 如果原有信息大于传进来的具体条件
            staff_info_2 = []
            for noun in noun_list:
                staff_info_2.append(data[noun][index])
            staff_info_1.append(staff_info_2)
    return staff_info_1


def judge_smaller(n, c):   # 判断语句中有小于号的情况 # step:4
    ‘‘‘
    判断小于的情况
    :param n: 这是要筛选的对象名称(年龄,职位...)
    :param c: 这是要筛选的具体条件(IT,22, 2017...)
    :return:
    ‘‘‘
    pass
    staff_info_1 = []
    for index,condition in enumerate(data[n]):
        if float(condition) < float(c):
            staff_info_2 = []
            for noun in noun_list:
                staff_info_2.append(data[noun][index])
            staff_info_1.append(staff_info_2)
    return staff_info_1


def judge_equal(n, c):   # 判断语句中有等于号的情况 # step:4
    ‘‘‘
    判断等于的情况
    :param n: 这是要筛选的对象名称(年龄,职位...)
    :param c: 这是要筛选的具体条件(IT,22, 2017...)
    :return:
    ‘‘‘
    staff_info_1 = []
    for index,condition in enumerate(data[n]):
        if condition == c:  # 如果原有信息大于传进来的具体条件
            staff_info_2 = []
            for noun in noun_list:
                staff_info_2.append(data[noun][index])
            staff_info_1.append(staff_info_2)
    return staff_info_1


def judge_like(n, c):   # 判断语句中有like的情况 # step:4
    ‘‘‘
    判断相似的情况
    :param n: 这是要筛选的对象名称(年龄,职位...)
    :param c: 这是要筛选的具体条件(IT,22, 2017...)
    :return:
    ‘‘‘
    staff_info_1 = []
    for index, condition in enumerate(data[n]):
        if c in condition:  # 如果原有信息大于传进来的具体条件
            staff_info_2 = []
            for noun in noun_list:
                staff_info_2.append(data[noun][index])
            staff_info_1.append(staff_info_2)
    return staff_info_1


def judge_right(right_order):  # step: 3
    ‘‘‘
    分析右半边的筛选条件,并过滤数据输出符合条件的信息
    :param right_order:
    :return:
    ‘‘‘
    judge = {>: judge_greater, <: judge_smaller, =: judge_equal, like: judge_like}
    # 把符号和对应的函数名存成字典,循环字典来判断语句中的筛选符号是什么
    for judge_k,judge_func in judge.items():
        if judge_k in right_order:
            noun, condition = (right_order.split(judge_k))
            noun = str(noun).strip()
            condition = str(condition).strip()
            # 把名词,条件按照相应输入的判断符号来区分开
            catch_info = judge_func(noun,condition)  # 把获取来的名词和筛选条件传进匹配函数,来进行筛选  # 4
            return catch_info
    else:
        print_login(输入有误,缺少明确的判断符号(>,<,=,like...)!)
        #走到这就说明指令里面没有明确的判断符,无法进行下一步判断


def find_info(catch_info, left_order):
    ‘‘‘
    find name,age from staff_table where age > 22
    find * from staff_table where dept = IT
    find * from staff_table where enroll_date like 2013
    :param catch_info:
    :param left_order:
    :return:
    ‘‘‘
    count = 0
    if * not in left_order:
        noun_line = left_order.split(from)[0][4:].split(,)
        noun_point = [i.strip() for i in noun_line]
        for row_info in catch_info:
            for noun in noun_point:
                    noun_index = noun_list.index(noun)
                    print(noun, row_info[noun_index])
                    count += 1
    else:
        print(catch_info)
    count = len(catch_info)
    print_login(本次共涉及%s条信息%(count), info)


def add_info(catch_info,left_order):
    ‘‘‘
    add staff_table Alex Li,25,134435344,IT,2015‐10‐29
    :param catch_info: [[‘1‘, ‘Alex Li‘, ‘22‘, ‘13651054608‘, ‘IT‘, ‘2013‐04‐01\n‘], [‘3‘, ‘Rain Wang‘, ‘21‘, ‘13451054608‘, ‘IT‘, ‘2017‐04‐01\n‘], [‘4‘, ‘Mack Qiao‘, ‘44‘, ‘15653354208‘, ‘Sales‘, ‘2016‐02‐01\n‘], [‘5‘, ‘Rachel Chen‘, ‘23‘, ‘13351024606‘, ‘IT‘, ‘2013‐03‐16\n‘], [‘6‘, ‘Eric Liu‘, ‘19‘, ‘18531054602‘, ‘Marketing‘, ‘2012‐12‐01\n‘], [‘7‘, ‘Chao Zhang‘, ‘21‘, ‘13235324334‘, ‘Administration‘, ‘2011‐08‐08\n‘], [‘8‘, ‘Kevin Chen‘, ‘22‘, ‘13151054603‘, ‘Sales‘, ‘2013‐04‐01\n‘], [‘9‘, ‘Shit Wen‘, ‘20‘, ‘13351024602‘, ‘IT‘, ‘2017‐07‐03\n‘], [‘10‘, ‘Shanshan Du‘, ‘26‘, ‘13698424612‘, ‘Operation‘, ‘2017‐07‐02‘]]
    :param left_order:  add staff_table Alex Li,25,134435344,IT,2015‐10‐29
    :return:
    ‘‘‘
    info_list = [i.strip() for i in ‘‘.join([i.strip() for i in left_order.split(staff_table)[1:]]).split(,)]
    if info_list[2] not in data[phone]:
        data[staff_id].append(str(int(data[staff_id][-1]) + 1))
        data[name].append(info_list[0])
        data[age].append(info_list[1])
        data[phone].append(info_list[2])
        data[dept].append(info_list[3])
        #info_list[4] = ‘%s\n‘%(info_list[4])
        data[enroll_date].append(%s\n%(info_list[4]))
        file_save()
    else:
        print(电话号码有重复,不可注册!, error)


def del_info(catch_info,left_order):
    ‘‘‘
    del from staff where staff_id=3
    删除的语法要写staff_id,写id是找不到对应函数的
    :param catch_info:
    :param left_order: del from staff where id=3
    :return:
    ‘‘‘
    count = 0
    print(你将要删除:, catch_info)
    for info in catch_info:
        index = data[staff_id].index(info[0])
        for noun in noun_list:
            del data[noun][index]
        count += 1
    print_login(本次共涉及%s条信息%(count), info)
    file_save()


def update_info(catch_info,left_order):
    ‘‘‘
    update staff_table set dept=Market where dept = IT
    update staff_table set age=25 where name = Alex Li
    :param catch_info:
    :param left_order:
    :return:
    ‘‘‘
    count = 0
    noun = [i.strip() for i in left_order.split(set)[1].strip().split(=)]
    print(你要修改 %s 为 %s%(noun[0], noun[1]))
    for info in catch_info:
        index_id = data[staff_id].index(info[0])
        for n in noun_list:
            data[noun[0]][index_id] = noun[1]
        count += 1
    print_login(本次共涉及%s条信息%(count), info)
    file_save()


data = file_operation(file_name)  # 获得文件内容
catch_order()   # 执行操作

 

员工信息表-装逼版

标签:rate   方便   动作   opera   抓取   port   用户   输出   log   

原文地址:https://www.cnblogs.com/quegai18/p/9395422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!