day21

while True:
print('level1')
choice=input('level>>:').strip()
if choice=='quit':break
while True:
print('level2')
choice=input('level2>>').strip()
if choice=='quit':break
while True:
print('level3')
choice=input('level3>>').strip()
if choice=='quit':break

tag=True
while tag:
print('level1')
choice=input('level>>:').strip()
if choice=='quit':break
if choice=='quit_all':break
while tag:
print('level2')
choice=input('level2>>').strip()
if choice=='quit':break
if choice=='quit_all':break
while tag:
print('level3')
choice=input('level3>>').strip()
if choice=='quit':break
if choice=='quit_all':break

import os
def file_handle(backend_data,res=None,type='fetch'):
if type=='fetch':
with open('haporoxy.conf','r') as read_f:
tag=False
ret=[]
for read_line in read_f:
if read_line.strip()==backend_data: #去除空格和换行
tag = True
continue
if read_line.startswith('backend'):
tag = False
if tag == True:
print(read_line, end='')
ret.append(read_line)
return ret
elif type=='change':
with open('haporoxy.conf', 'r') as read_f, \
open('haporoxy.conf_new', 'w') as write_f:
tag = False
n = 0
for read_line in read_f:
if read_line.strip() == backend_data:
# write_f.write(read_line)
tag = True
continue
if tag and read_line.startswith('backend'):
tag = False
if not tag:

write_f.write(read_line)
else:
if n == 0:
for record in res:
write_f.write(record)
n += 1
os.rename('haporoxy.conf', 'haporoxy.conf.bak')
os.rename('haporoxy.conf_new', 'haporoxy.conf')
os.remove('haporoxy.conf.bak')

def fetch(data):
print('这是查询功能')
print('用户输入的数据是%s' %data)
backend_data='backend %s' %data #bacckend www.oldboy1.org


# with open('haporoxy.conf','r') as read_f:
# tag=False
# ret=[]
# for read_line in read_f:
# if read_line.strip()==backend_data: #去除空格和换行
# tag = True
# continue
# if read_line.startswith('backend'):
# tag = False
# if tag == True:
# print(read_line, end='')
# ret.append(read_line)
# return ret
res=file_handle(backend_data)
return res
def add():
pass
def change(data):
#data=[{'backend':'www.oldboy1.org','record':{'serve':'2.2.2.4','weight':20,'maxconn':3000}},
# {'backend':'www.oldboy1.org','record':{'serve':'2.2.2.5','weight':30,'maxconn':4000}}]
backend=data[0]['backend'] #www.oldboy1.org
backend_data='backend %s' %backend #backend www.oldboy1.org
print('这是修改功能')
print('用户输入的数据是%s' %data)
# data[0] #文件当中的一条记录
# data=eval(data)
old_serve_record='%sserve %s %s weight %s maxconn %s\n' %(' '*8,data[0]['record']['serve'],
data[0]['record']['serve'],
data[0]['record']['weight'],
data[0]['record']['maxconn'])


new_serve_record = '%sserve %s %s weight %s maxconn %s\n' % (' ' * 8, data[1]['record']['serve'],
data[1]['record']['serve'],
data[1]['record']['weight'],
data[1]['record']['maxconn'])
print('用户想要修改的记录',old_serve_record) #serve 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
res=fetch(backend)
print('来自change函数',res)
if not res:
return "你要修改的记录不存在1"

 

print(old_serve_record)
if old_serve_record not in res:
return "你要修改的记录不存在2"
index=res.index(old_serve_record)
res[index]=new_serve_record
print('新的列表',res)
res.insert(0,'%s\n' %backend_data)

# with open('haporoxy.conf', 'r') as read_f,\
# open('haporoxy.conf_new','w') as write_f:
# tag=False
# n=0
# for read_line in read_f:
# if read_line.strip()== backend_data:
# # write_f.write(read_line)
# tag=True
# continue
# if tag and read_line.startswith('backend'):
# tag=False
# if not tag:
#
# write_f.write(read_line)
# else:
# if n==0:
# for record in res:
# write_f.write(record)
# n+=1
# os.rename('haporoxy.conf','haporoxy.conf.bak')
# os.rename('haporoxy.conf_new','haporoxy.conf')
# os.remove('haporoxy.conf.bak')
file_handle(backend_data,res=res,type='change')
def delete():
pass
#print(__name__) #__name'__=__main__'
if __name__=='__main__':
# print('test')
msg='''
1:查询
2:添加
3:修改
4:删除
5:退出
'''
msg_dic={
'1':fetch,
'2':add,
'3':change,
'4':delete,
'5':exit

}
while True:
print(msg)
choice=input('请输入你的选项').strip()
if not choice:
continue
if choice=='5':
break

data=input('请输入你的数据') #输入的是字符串格式
if choice!='1':
data=eval(data)
res=msg_dic[choice](data)
print('最终结果',res)
#[{'backend':'www.oldboy1.org','record':{'serve':'2.2.2.4','weight':20,'maxconn':3000}},{'backend':'www.oldboy1.org','record':{'serve':'2.2.2.5','weight':30,'maxconn':4000}}]



1、函数
2、文件处理
3、Tag的用法
4、程序的解耦


模块: 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就会相对减少
,很多编程语言都采用这种组织代码的方式,在python中,一个py文件就称之为一个模块


最大的好处大大提高了代码的可维护性

模块一共三种
python标准库
第三方模块
应用程序自定义模块

在cal.py文件中定义
def add(x,y):
return x+y
def sub(x,y):
return x-y

在test.py文件中

import cal
print(cal.add(3,5))

import
1、执行调用的模块文件
2、引入变量名 cal

from cal import add
from cal import sub

from cal import * #不推荐

能直接使用调用模块里的函数名

执行文件和调用文件不在同一目录下:
from day21(被调用模块所在目录) import cal

调用模块默认的是执行文件的路径


包: 组织模块
多层:
from web.web1.we2.web3 import cal
from web.web1.we2.web3.cal import add

from web.web1.we2import web3 #执行web3的Init文件 唯一一种不支持的调用方式
web3.cal.add(2,6) #不行

if __name__=='__main__': #用于被调用文件的测试


#时间戳
print(time.time()) #1481534569.481654秒 从1970年开始
#结构化时间
t=time.localtime() #默认t=time.localtime(time.time()#)
print(time.localtime) #time.struct_time(tm_year=2018, tm_mon=8, tm_mday=25, tm_hour=13, tm_min=36, tm_sec=11, tm_wday=5, tm_yday=237, tm_isdst=0)

print(t.tm_year)

#结构化时间 世界标准时间
print(time.gmtime())#time.struct_time(tm_year=2018, tm_mon=8, tm_mday=25, tm_hour=5, tm_min=41, tm_sec=17, tm_wday=5, tm_yday=237, tm_isdst=0)

#将结构化时间转换成时间戳
print(time.mktime(time.localtime()))


#将结构化时间转换成字符串时间
print(time.strftime('%Y-%m-%d-%X,time.localtime()')) #年月日及时分秒

#将字符串时间转换为结构化时间
print(time.strptime('2016:12:24:17:50:36','%Y:%m:%d:%X'))

print(time.asctime()) #Sat Aug 25 14:08:21 2018 结构化时间转换成固定的字符串形式

print(time.ctime()) #Sat Aug 25 14:08:21 2018 时间戳转换成固定的字符串形式

import datetime
datetime.datetime.now()
print(datetime.datetime.now) #2018-08-25 14:16:53.474752


import random

ret=random.random() #0到1的浮点数
ret=random.randint(1,3) #
ret=random.randrange(1,3)
ret=random.randchoice([11,22,33])
ret=random.randsample([11,22,33,44],2) #选两个
ret=random.uniform(1,3) #浮点数
print(ret)

item=[1,3,5,7,9]
random.shuddle(item) #打乱
print(item)


def v_code():

ret=''
for i in range(5):
num=random.randint(0,9)
alf=chr(random.randint(65,122))
ret.append=str(random.choice([num,alf]))
return ret



 2018-08-27

 

posted @ 2018-08-27 20:48  漫语慢蜗牛  阅读(172)  评论(0编辑  收藏  举报