# 一:编写函数,(函数执行的时间是随机的) import time import random def func(): start_time = time.time() count = 1 while count < 999: count += 1 end_time = time.time() print(end_time - start_time) func() # ---------- # random()方法返回的是随机生成的一个实数 def func(): time.sleep(random.randrange(1,3)) # 产生随机数当作休息时间 print(random.randrange(1,3)) func() # ---------- print('randrange(1,100,2):',random.randrange(1,100,2)) print('randrange(100)',random.randrange(100)) # 二:编写装饰器,为函数加上统计时间的功能 import time def timess(func): def wrapper(*args,**kwargs): start_time = time.time() res = func(*args,**kwargs) stop_time = time.time() print("运行时长%s:"%(stop_time - start_time)) return res return wrapper @timess def ffo(): time.sleep(1) ffo() # 三:编写装饰器,为函数加上认证的功能 import time def timess(func): def wrapper(*args,**kwargs): start_time = time.time() res = func(*args,**kwargs) stop_time = time.time() print("运行时长%s:"%(stop_time - start_time)) return res return wrapper def login(func): def wrapper(*args,**kwargs): user = input("username: ").strip() pwd = input("password: ").strip() if user == "rocco" and pwd == "999": print("login ok") res = func(*args,**kwargs) return res else: print("login no") return wrapper @timess @login def ffo(): time.sleep(1) ffo() # 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码 # 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式 dab = "userfile.txt" status = {"user":None,"status":False} def func(type="file"): def func1(func): def wrapper(*args,**kwargs): if status["user"] and status["status"]: return func(*args,**kwargs) if type == "file": with open(dab, 'r', encoding='utf-8') as f: dicts = eval(f.read()) name = input("输入用户名: ").strip() passwd = input("输入密码: ").strip() if name in dicts["username"] and passwd == dicts["password"]: status["user"] = name status["status"] = True res = func(*args,**kwargs) return res else: print("用户或密码错误") elif type == "sql": pass else: pass return wrapper return func1 @func() def index(): print("index") @func(type='file') def home(name): print(f"{name}") index() home("rocco") # 五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录 import time import random tus = {"user":None,"login":None,"time_time":0.03} def timerr(func): def wrapper(*args,**kwargs): start = time.time() res = func(*args,**kwargs) stop = time.time() print("Run time%s" %(stop-start)) return res return wrapper def aut(func): def wrapper(*args,**kwargs): if tus["user"]: time_time = time.time() - tus["login"] if time_time < tus["time_time"]: return func(*args,**kwargs) name = input("name>").strip() pwd = input("password").strip() if name == "rocco" and pwd == "r123": tus["user"] = name tus["login"] = time.time() res = func(*args,**kwargs) return res return wrapper @timerr @aut def inde(): time.sleep(random.randrange(3)) print("aut to inde") @timerr @aut def home(name): time.sleep(random.randrange(3)) print("%s to home " %name) inde() home("rocco") # 六:编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果 from urllib.request import urlopen def index(url): def get(): return urlopen(url).read() return get b = index("http://www.cn.bing.com") print(b().decode('utf-8')) # 七:为题目五编写装饰器,实现缓存网页内容的功能: # 具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中 # 扩展功能:用户可以选择缓存介质/缓存引擎,针对不同的url,缓存到不同的文件中 # 八:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作 dictes = {} def make_name(name): def name_dic(func): dictes[name] = func return name_dic @make_name("Rocco") def name1(): print("Rocco") @make_name("Sirliu") def name2(): print("Sirliu") @make_name("chendan") def name3(): print("Chendan") print(dictes) # 九 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到日志文件中,日志文件路径可以指定 # 注意:时间格式的获取 import time time.strftime('%Y-%m-%d %X') import time import os def logg(logfile): def doc(func): if not os.path.exists(logfile): # exists稍后将更多文件添加到这里 with open(logfile,'w'):pass def wrapper(*args,**kwargs): res = func(*args,**kwargs) with open(logfile,'a',encoding='utf-8') as f: f.write(f"{time.strftime('%Y-%m-%d %X'),func.__name__}\n") return res return wrapper return doc @logg(logfile="filelog.log") def ind(): print("index>>>") ind()
最新评论
# 这只是一个创建远程登录并授权的语句、仅作为记录 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Fit2cloud!' WITH GRANT OPTION;
当MGR集群初始化结束后,需要开启MGR集群自启动(需要有一台节点是自动开启引导) loose-group_replication_start_on_boot = ON #设置节点是否在启动时自动启动 MGR 集群 loose-group_replication_bootstrap_group = ON #设置节点是否作为初始引导节点启动集群
密码:blog.sirliu.com
本内容密码:blog.sirliu.com 最新整理的文章在这里喔:https://blog.sirliu.com/2018/11/shell_lian_xi_ti.html