Python-24_綜合練習-01_函式_檔案處理_解耦--查詢功能
阿新 • • 發佈:2018-11-29
--------------------------------------------- 一、查詢功能:---------------------------------------------
def fetch(data): print("\033[1;43m查詢功能\033[0m") # \033[1;43m---\033[0m 給---新增顏色,43代表顏色 print("\033[1;43m使用者資料\033[0m",data) backend_data="backend %s" %data with open("haproxy.conf","r") as read_f: arg=False res=[] for read_line in read_f: if read_line.strip()==backend_data: arg=True continue if arg and read_line.startswith("backend"): break if arg: print("\033[1;43m%s\033[0m"%read_line,end="") res.append(read_line) return res def add(): print("新增功能") def change(): print("修改功能") def delete(): print("刪除功能") msg=""" 1:查詢 2:新增 3:修改 4:刪除 5:退出 """ msg_dic={"1":fetch,"2":add,"3":change,"4":delete} while True: print(msg) choice=input("請輸入你需要的選項:").strip() if not choice:continue if choice=="5":break data=input("請輸入您的資料:").strip() msg_dic[choice](data) # 退出當前迴圈、退出全部迴圈: arg=True while arg: print("level1") choice=input("level1 >>>").strip() if choice == "quit": break if choice == "quit_all": arg = False while arg: print("level2") choice = input("level2 >>>").strip() if choice == "quit": break if choice == "quit_all": arg = False while arg: print("level3") choice = input("level3 >>>").strip() if choice == "quit": break if choice == "quit_all": arg = False
--------------------------------------------- 二、修改功能:---------------------------------------------
import os def fetch(data): print("\033[1;43m查詢功能\033[0m") # \033[1;43m---\033[0m 給---新增顏色,43代表顏色 print("\033[1;43m使用者資料\033[0m", data) backend_data = "backend %s" % data with open("haproxy.conf", "r") as read_f: arg = False res = [] for read_line in read_f: if read_line.strip() == backend_data: arg = True continue if arg and read_line.startswith("backend"): break if arg: print("\033[1;43m%s\033[0m" % read_line, end="") res.append(read_line) return res def add(): print("新增功能") def change(data): print("修改功能") print("使用者輸入資料%s" % data) backend = data[0]["backend"] # 檔案當中的一條記錄 backend_data="backend %s" %backend old_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[0]["record"]["server"], data[0]["record"]["server"], data[0]["record"]["weight"], data[0]["record"]["maxconn"]) new_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[1]["record"]["server"], data[1]["record"]["server"], data[1]["record"]["weight"], data[1]["record"]["maxconn"]) 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) with open("haproxy.conf","r") as read_f,\ open("haproxy.conf_new","w") as write_f: arg=False arg_write=False for read_line in read_f: if read_line.strip()==backend_data: arg=True continue if arg and read_line.startswith("backend"): arg=False if not arg: write_f.write(read_line) else: if not arg_write: for record in res: write_f.write(record) arg_write=True os.rename("haproxy.conf","haproxy.conf.bak") os.rename("haproxy.conf_new","haproxy.conf") os.remove("haproxy.conf.bak") def delete(): print("刪除功能") msg = """ 1:查詢 2:新增 3:修改 4:刪除 5:退出 """ msg_dic = {"1": fetch, "2": add, "3": change, "4": delete} while True: print(msg) choice = input("請輸入你需要的選項:").strip() if not choice: continue if choice == "5": break data = input("請輸入您的資料:").strip() if choice != "1": data = eval(data) res = msg_dic[choice](data) print(res) # data --->> [{"backend":"www.newmet1.top","record":{"server":"2.2.2.5","weight":"30","maxconn":"4000"}},{"backend":"www.newmet1.top","record":{"server":"2.2.2.4","weight":"20","maxconn":"3000"}}] # 查詢和修改功能,程式碼都涉及到檔案處理的操作,可以單獨定義一個函式,用來處理檔案的操作!!修改功能
------------------------------------ 二、修改功能--程式的解耦:-------------------------------------
import os def file_handle(backend_data,res=None,type="fetch"): if type=="fetch": with open("haproxy.conf", "r") as read_f: tag = False res = [] 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("\033[1;43m%s\033[0m" % read_line, end="") res.append(read_line) return res elif type == "change": with open("haproxy.conf", "r") as read_f, \ open("haproxy.conf_new", "w") as write_f: tag = False tag_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 tag_write: for record in res: write_f.write(record) tag_write = True os.rename("haproxy.conf", "haproxy.conf.bak") os.rename("haproxy.conf_new", "haproxy.conf") os.remove("haproxy.conf.bak") def fetch(data): # print("\033[1;43m查詢功能\033[0m") # \033[1;43m---\033[0m 給---新增顏色,43代表顏色 # print("\033[1;43m使用者資料\033[0m", data) backend_data = "backend %s" % data return file_handle(backend_data) def add(): print("新增功能") def change(data): print("修改功能") print("使用者輸入資料%s" % data) backend = data[0]["backend"] # 檔案當中的一條記錄 backend_data="backend %s" %backend old_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[0]["record"]["server"], data[0]["record"]["server"], data[0]["record"]["weight"], data[0]["record"]["maxconn"]) new_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[1]["record"]["server"], data[1]["record"]["server"], data[1]["record"]["weight"], data[1]["record"]["maxconn"]) 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_handle(backend_data,res=res,type="change") def delete(): print("刪除功能") msg = """ 1:查詢 2:新增 3:修改 4:刪除 5:退出 """ msg_dic = {"1": fetch, "2": add, "3": change, "4": delete} while True: print(msg) choice = input("請輸入你需要的選項:").strip() if not choice: continue if choice == "5": break data = input("請輸入您的資料:").strip() if choice != "1": data = eval(data) res = msg_dic[choice](data) print(res) # data --->> [{"backend":"www.newmet1.top","record":{"server":"2.2.2.5","weight":"30","maxconn":"4000"}},{"backend":"www.newmet1.top","record":{"server":"2.2.2.4","weight":"20","maxconn":"3000"}}] # 查詢和修改功能,程式碼都涉及到檔案處理的操作,可以單獨定義一個函式,用來處理檔案的操作!!修改功能:程式的解耦