python基础篇第十一节课
查询功能和修改功能
import os def fetch(data): print("这是查询功能") print("用户数据是",data) backend_data = "backend %s" %data with open("test.conf","r",encoding="utf_8")as read_f: tag = False ret = [] 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("%s" %read_line,end="") ret.append(read_line) return ret def add(): pass def change(data): print("这是修改功能") print("用户输入的数据是",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("来自change的函数", 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("test.conf","r")as read_f,open("test.conf_new","w")as write_f: tag = 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.conf","test.conf_bak") os.rename("test.conf_new","test.conf") os.remove("test.conf_bak") def delete(): pass if __name__ =="__main__": msg = ''' 1:查询 2:添加 3:修改 4:删除 5:退出 ''' msg_dic = {"1":fetch,"2":add,"3":change,"4":delete,"5":quit} 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) # [{"backend":"www.oldboy1.org","record":{"server":"2.2.2.4","weight":20,"maxconn":3000}},{"backend":"www.oldboy1.org","record":{"server":"2.2.2.8","weight":30,"maxconn":4000}}]
模块
作用:1执行对应文件
2引入变量名
from 文件夹 import文件
多重包
from 包.包 import 文件
时间模块
#结构化时间 ——————当地时间 import time print(time.localtime()) #年份日期 t=time.localtime() print(t.tm_year) #年份 print(t.tm_hour) #小时 print(t.tm_mon) #月份 print(t.tm_mday) #日期 print(t.tm_wday) #一周第几天(从星期日开始算0,1,2,3,4,5,6) print(t.tm_min) #分钟 print(t.tm_sec) #秒数 print(t.tm_yday) #一年中多少天 #结构化时间——————utc print(time.gmtime()) #将结构化时间转换为时间戳 print(time.mktime(time.localtime())) #将结构化时间转换为字符串时间 print(time.strtime("%Y--%m-%d %X",time.localtime())) #年 月 日 时分秒 #将字符串时间转换为结构化时间 print(time.strftime("2018:9:27:22:42:50","%Y:%m:%d:%X")) import datetime print(datetime.datetime.now())
随机模块
# 浮点型 import random ret = random.random() print(ret) #整点型 ret =random.randint(1,5) print(ret) #输出1-5之间的任意数 ret=random.randrange(1,5) print(ret) #输出1-4之间的任意数 ret=random.choice([11,22]) print(ret) #输出11,22,33之间任意一组数 ret=random.sample([11,22,33,44,55],2) print(ret) #输出其中任意两组数 ret=random.uniform(1,4) print(ret) #输出1-4之间的浮点数 ret=[1,2,3,4,5] random.shuffle(ret) print(ret) #打乱列表中的排序 #随机验证码 def v_code(): ret =" " for i in range(5): num = random.randint(0,9) alf = chr(random.randint(65,122)) s = str(random.choice([num,alf])) ret +=s return ret print(v_code())

浙公网安备 33010602011771号