python学习-44 程序的解耦 (不是特别懂的,回头…

2019-07-24 09:29:48来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

import os

def file_handler(backend_data,res=None,type='fetch'):                  # 查询功能
    if type == 'fetch':
         with open('test_new.txt','r') as  read_f:
             ret = []
             tag = False
             for read_line in  read_f:
                if read_line.strip() == backend_data:
                    tag = True
                    continue

                if tag and read_line.startswith('backend'):
                    break

                if tag:
                    print(read_line,end='')
                    ret.append(read_line)
         return ret
    if type == 'change':                                          # 修改功能
        with open('test_new.txt', 'r') as read_f, \
                open('test_new.txt_new', 'w') as write_f:
            tag = False
            has_write = False
            for read_line in read_f:
                if read_line.strip() == backend_data:
                    tag = True
                    continue
                if tag and read_line.startswith('backend'):
                    tag = False
                if not tag:
                    write_f.write(read_line)
                else:
                    if not has_write:
                        for record in res:
                            write_f.write(record)
                        has_write = True

        os.rename('test_new.txt', 'test_new.txt.bak')
        os.rename('test_new.txt_new', 'test_new.txt')
        os.remove('test_new.txt.bak')

def fetch(data):
    print('您输入的信息是:',data)
    backend_data= 'backend %s' % data
    return file_handler(backend_data)

def change(data):
    print('开始修改功能')
    print('用户输入的信息是:',data)
    backend=data[0]['backend']
    backend_data='backend %s' %backend
    old_server_record ='%s server %s %s\n'%(*9,data[0]['record']['server'])
    new_server_record = '%s server %s %s\n' % (*9, data[1]['record']['server'])
    print('您想要修改的记录:',old_server_record)
    res=fetch(backend)
    print(res)
    if not res or old_server_record not in res:
        return '您要修改的信息不存在'
    else:
        index = res.index(old_server_record)
        res[index]=new_server_record

    res.insert(0,'%s\n' %backend_data)
    file_handler(backend_data,res=res,type='change')


if __name__== '__main__':
    msg='''
    1:查询
    2:修改
    5:退出
    '''
    msg_dic={
        '1':fetch,
        '2':change
    }
    while True:
        print(msg)
        choose = input('请输入您要的选项:').strip()
        if not choose: continue
        if choose == '5':break

        data = input('请输入您的信息:').strip()
        if choose != '1':
            data = eval(data)


        res = msg_dic[choose](data)            # 查询到的结果放到一个列表里输出
        print('最终的结果:',res)

 


原文链接:https://www.cnblogs.com/liujinjing521/p/11234476.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:PYTHON类的继承

下一篇:编程小白的第一本python入门书(高清版)PDF下载