首页 > 其他分享 >29:函数查询,添加,修改,删除

29:函数查询,添加,修改,删除

时间:2024-08-29 17:47:51浏览次数:5  
标签:list 29 查询 record 添加 file line data backend

#_*_coding:utf-8_*_
import os
def file_handle(filename,backend_data,record_list=None,type='fetch'): #type:fetch append change
    new_file=filename+'_new'
    bak_file=filename+'_bak'
    if type == 'fetch':
        r_list = []
        with open(filename, 'r') as f:
            tag = False
            for line in f:
                if line.strip() == backend_data:
                    tag = True
                    continue
                if tag and line.startswith('backend'):
                    break
                if tag and line:
                    r_list.append(line.strip())
            for line in r_list:
                print(line)
            return r_list
    elif type == 'append':
        with open(filename, 'r') as read_file, \
                open(new_file, 'w') as write_file:
            for r_line in read_file:
                write_file.write(r_line)

            for new_line in record_list:
                if new_line.startswith('backend'):
                    write_file.write(new_line + '\n')
                else:
                    write_file.write("%s%s\n" % (' ' * 8, new_line))
        os.rename(filename, bak_file)
        os.rename(new_file, filename)
        os.remove(bak_file)
    elif type == 'change':
        with open(filename, 'r') as read_file, \
                open(new_file, 'w') as write_file:
            tag=False
            has_write=False
            for r_line in read_file:
                if r_line.strip() == backend_data:
                    tag=True
                    continue
                if tag and r_line.startswith('backend'):
                    tag=False
                if not tag:
                    write_file.write(r_line)
                else:
                    if not has_write:
                        for new_line in record_list:
                            if new_line.startswith('backend'):
                                write_file.write(new_line+'\n')
                            else:
                                write_file.write('%s%s\n' %(' '*8,new_line))
                        has_write=True
        os.rename(filename, bak_file)
        os.rename(new_file, filename)
        os.remove(bak_file)

def fetch(data):
    backend_data="backend %s" %data
    return file_handle('haproxy.conf',backend_data,type='fetch')
def add(data):
    backend=data['backend']
    record_list=fetch(backend)
    current_record="server %s %s weight %s maxconn %s" %(data['record']['server'],\
                                                         data['record']['server'],\
                                                         data['record']['weight'],\
                                                         data['record']['maxconn'])
    backend_data="backend %s" %backend

    if not record_list:
        record_list.append(backend_data)
        record_list.append(current_record)
        file_handle('haproxy.conf',backend_data,record_list,type='append')
    else:
        record_list.insert(0,backend_data)
        if current_record not in record_list:
            record_list.append(current_record)
        file_handle('haproxy.conf',backend_data,record_list,type='change')
def remove(data):
    backend=data['backend']
    record_list=fetch(backend)
    current_record="server %s %s weight %s maxconn %s" %(data['record']['server'],\
                                                         data['record']['server'],\
                                                         data['record']['weight'],\
                                                         data['record']['maxconn'])
    backend_data = "backend %s" % backend
    if not record_list or current_record not in record_list:
        print('\033[33;1m无此条记录\033[0m')
        return
    else:
        #处理record_list
        record_list.insert(0,backend_data)
        record_list.remove(current_record)
        file_handle('haproxy.conf',backend_data,record_list,type='change')
def change(data):
    backend=data[0]['backend']
    record_list=fetch(backend)

    old_record="server %s %s weight %s maxconn %s" %(data[0]['record']['server'],\
                                                         data[0]['record']['server'],\
                                                         data[0]['record']['weight'],\
                                                         data[0]['record']['maxconn'])

    new_record = "server %s %s weight %s maxconn %s" % (data[1]['record']['server'], \
                                                        data[1]['record']['server'], \
                                                        data[1]['record']['weight'], \
                                                        data[1]['record']['maxconn'])
    backend_data="backend %s" %backend

    if not record_list or old_record not in record_list:
        print('\033[33;1m无此内容\033[0m')
        return
    else:
        record_list.insert(0,backend_data)
        index=record_list.index(old_record)
        record_list[index]=new_record
        file_handle('haproxy.conf',backend_data,record_list,type='change')
def qita(data):
    pass


if __name__ == '__main__':
    msg='''
    1:查询
    2:添加
    3:删除
    4:修改
    5:退出
    6:其他操作
    '''
    menu_dic={
        '1':fetch,
        '2':add,
        '3':remove,
        '4':change,
        '5':exit,
        '6':qita,
    }
    while True:
        print(msg)
        choice=input("操作>>: ").strip()
        if len(choice) == 0 or choice not in menu_dic:continue
        if choice == '5':break

        data=input("数据>>: ").strip()

        #menu_dic[choice](data)==fetch(data)
        if choice != '1':
            data=eval(data)
        menu_dic[choice](data) #add(data)




# [{'backend':'www.happyman20.org','record':{'server':'2.2.2.3','weight':20,'maxconn':3000}},{'backend':'www.happyman10.org','record':{'server':'10.10.0.10','weight':9999,'maxconn':33333333333}}]

  

标签:list,29,查询,record,添加,file,line,data,backend
From: https://www.cnblogs.com/liu-zhijun/p/18387280

相关文章

  • 8.29 模拟赛
    S---【云智计划】---7月11日---模拟测#30div2【补题】-比赛-梦熊联盟(mna.wang)S---【云智计划】---7月11日---模拟测#30div1【补题】-比赛-梦熊联盟(mna.wang)预计\(100+70+0+50+45\),实际\(90+50+0+45+25\)。比赛复盘A一眼可做。分析了一下推出了一个三维偏序......
  • pageHelper分页插件导致的查询慢的问题优化
    首先在yml中配置mybatis:configuration:log-impl:org.apache.ibatis.logging.stdout.StdOutImpl会进行sql语句打印问题:进行分页查询时pageHelper自动生成的count语句相当于在查询语句外包一层selectcount(1)from(你的查询语句)对于你的查询语句的返回条件中有较......
  • ElasticSearch学习笔记(三)RestClient操作文档、DSL查询文档、搜索结果排序
    文章目录前言5RestClient操作文档5.4删除文档5.4修改文档5.5批量导入文档6DSL查询文档6.1准备工作6.2全文检索查询6.3精准查询6.4地理坐标查询6.5复合查询6.5.1相关性算分6.5.2布尔查询7搜索结果处理7.1排序7.1.1普通字段排序7.1.2地理坐标排序......
  • 08.29
    QOJ141A没必要传度数\(<8\)的点。因为双染色是容易的,A把两种颜色压缩成一种颜色,B再把每种颜色双染色,就是合法的八染色了。每个点给度数和贡献至少\(8\),占\(2\)bit,考虑到度数和的上限为\(2m\),至多需要\(m/2\)bit。std::vector<int>Alice(intn,intm,std::vecto......
  • Mybatis-puls中select查询方法报错Can not find table primary key in Class
    1、项目参数springboot2.6.13jdk8Mybatis-Plus3.5.42、问题描述Mybatis-puls中select查询方法报错CannotfindtableprimarykeyinClass,org.apache.ibatis.binding.BindingException:Invalidboundstatement(notfound):com.example.dao.FLowerDao.selectById3、......
  • T240829 【用Liouville定理证明代数学基本定理】
    [T240829]代数学基本定理:在复平面上次数大于\(1\)的一元多项式至少有一个零点.引理(Liouville)有界整函数\(f(z)\)必为常数.证:设\(|f(z)|\)有上界\(M\).即\(\forallz\in\C,~|f(z)|\leM\).于是由Cauchy不等式,对\(\foralla\in\C\),有\[0\le|f'(a)|\le......
  • Mybatis-puls中select查询方法返回为空null
    1、项目参数springboot2.6.13jdk8Mybatis-Plus3.5.42、问题描述在3.5.4版本的MP中使用select方法查询到数据,却返回为空实体类publicclassFlower{@TableId(value="flower_id",type=IdType.INPUT)privateintflower_id;privateStringflower_name;......
  • MySQL 使用pt-osc添加索引Lock wait timeout exceeded管窥
    1.pt-osc工具1.1.pt-osc简介pt-osc是pt-online-schema-change的简写,pt-online-schema-change是percona-toolkit工具包中用于在线变更DDL的工具1.2.pt-osc原理1.3.pt-toolkit安装#yuminstallperl-ExtUtils-CBuilderperl-ExtUtils-MakeMakercpan#yumload-transaction......
  • SAP B1 基础实操 - 查询管理器(基础版)
    一、功能介绍很多时候我们需要将不同表单的信息汇总在一张表格内方便查看,在SAPB1中,我们通过【查询管理器】写SQL代码的方式查询数据并汇总。如上图,在表单中,上半黄底为SQL代码区域,下半灰底表格为根据SQL代码查询得到的结果。本篇以一个简单案例讲述查询管理器的基本......
  • YOLOv8添加DCNv4可变性卷积(windows系统成功编译),全网最详细教程
    原论文摘要引入了可变形卷积v4(DCNv4),这是一种为广泛视觉应用设计的高效且有效的操作算子。DCNv4通过两项关键增强解决了其前身DCNv3的局限性:1.移除空间聚合中的softmax归一化,以增强其动态特性和表达能力;2.优化内存访问以最小化冗余操作,从而加速计算。这些改进使得DC......