def geturl(url):
response = requests.get(url)
print(response.status_code)
geturl("http://www.baidu.com")
def timer(func):
def wapper(url):
start_time = time.time()
func(url)
stop_time = time.time()
so_time_is = stop_time - start_time
print("运行时间%s"%so_time_is)
return wapper
@timer
def geturl(url):
response = requests.get(url)
print(response.status_code)
python = geturl("http://www.baidu.com")
#一个low得不能再low得验证脚本,如果是显示环境中所有数据必须是由数据库或者一个静态文件提供,并且登录成功时,需要保存用户的一个状态
def auth(auth_type): #有参装饰器名称
def auth_deco(func): #定义第二层函数名称
def wrapper(*args,**kwargs): #最里层函数,主要实现认证功能
if auth_type == "file":
username = input("username>>:").strip()
password = input("username>>").strip()
if username == "leon" and password == "loveleon":
res = func(*args,**kwargs)
return res
elif auth_type == "mysql_auth":
print("mysql_auth...")
return func(*args,**kwargs)
return wrapper #第二层返回的是wrapper函数,其实就是home
return auth_deco #第一层返回的结果等于第二层函数的名称
@auth('file')
def home():
print("welcome")
home() #执行home-->wrapper
a = 10
b = 20
c = 30
def read1():
print("in the read1")
def read2():
print("in the read2")
import leonyan #Python IDE这行会爆红,但是不用管
leonyan.read1() #执行leonyan这个包中的read1函数
leonyan.read2() #执行leonyan这个包中read2函数
print(leonyan.a + leonyan.b + leonyan.c ) #输出60
总结:在Python中包的导入(import ***)会干三个事情:1:创建新的作用域;2:执行该作用域的顶级代码,比如你导入的那个包中有print执行后就会直接在屏幕中输出print的内容;3:得到一个模块名,绑定到该模块内的代码
import leonyan as ly
import pandas as pd #这是一个第三方模块,以后的博客中会写到,这是一个用于做统计的
from leonyan import read1 #引