阅读目录

一、模块和包

模块(module)的概念:

在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码会越来越长,越来越不容易维护。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

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

使用模块有哪些好处?

1、最大的好处就是大大提高了代码的可维护性。
2、编写代码不必从零开始。当一个模块编写完成,就可以被其他地方调用。我们在编写程序的时候,也经常饮用其他模块,包括Python内置的模块和来自第三方的模块。

模块一共分为三种:

1、python标准库
2、第三方模块
3、应用程序自定义模块

注意:

使用模块还可以避免函数名跟变量名冲突。相同名字的函数和变量完全可以分别存在不同的模块中,因此,我们在自己编写模块时,不必考虑名字会与其他模块冲突。但是也要注意,尽量不要与内置函数名字冲突。

二、模块的导入方法

1、import语句

import module1,[module2[,...moduleN]]

当我们使用import语句的时候,Python解释器是怎样找到对应的文件?答案就是解释器有自己的搜索路径,存在sys.path里。

import sys
print(sys.path) 运行结果如下: ['G:\\python_s3\\day21', 'G:\\python_s3', 'C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages', 'D:\\Program Files (x64)\\pycharm软件安装\\config\\PyCharm 2018.3\\helpers\\pycharm_matplotlib_backend']

因此若像我一样在当前目录下存在与要引入模块同名的文件,就会把要引入的模块没屏蔽掉。

2、from...import ...语句

from modname import name1[,name2[, ... nameN]]

这个声明不会把整个modulename模块导入到当前的命名空间中,只会讲它里面的name1或name2单个引入到执行这个声明的模块的全局符号表。 

3、from...import*语句

from modname import *

4、运行本质

1、import test
2、from test import add

无论1还是2,首先通过sys.path找到test.py,然后执行test脚本,区别是1会将test这个变量名加载到名字空间,而2只会将add这个变量名加载进来。

三、实现装饰器知识储备

装饰器=高阶函数+函数嵌套+闭包

四、高阶函数

高阶函数的定义:
1、函数接收的参数是一个函数名
2、函数的返回值是一个函数名
3、满足上述条件任意一个,都可称之为高阶函数

 

 

五、函数嵌套

def father(name):
    print('from father %s' %name)
    def son():
        print('from the son')
        def grandson():
            print('from the grandson')
        grandson()
    son()

father('朱锐')

六、闭包

1、闭包

def father(name):
    print('from father %s' %name)
    def son():
        print('from the son')
        def grandson():
            print('from the grandson')
        grandson()
    son()

father('朱锐')

'''
闭包
'''

def father(name):
    def son():
        # name='simon1'
        print('我的爸爸是%s' %name)
        def grandson():
            print('我的爷爷是%s' %name)
        grandson()
    son()
father('simon')

2、函数闭包装饰器基本实现

import time
def timmer(func):
    def wrapper():
        # print(func)
        start_time=time.time()
        func() #就是在运行test()
        stop_time=time.time()
        print('运行时间是%s' %(stop_time-start_time))
    return wrapper
@timmer #语法糖,这个是重点

def test():
    time.sleep(3)
    print('test函数运行完毕')

# res=timmer(test) #返回的是wrapper的地址
# res() #执行的是wrapper()

# test=timmer(test) #返回的是wrapper的地址
# test() #执行的是wrapper()

test()
'''
语法糖
'''
# @timmer #就相当于 test=timmer(test)

3、函数闭包加上返回值

#未加返回值
import time
def timmer(func):
    def wrapper():
        # print(func)
        start_time=time.time()
        func() #就是在运行test()
        stop_time=time.time()
        print('运行时间是%s' %(stop_time-start_time))
        return 123
    return wrapper
@timmer #语法糖

def test():
    time.sleep(3)
    print('test函数运行完毕')
    return '这是test的返回值'
res=test() #就是在运行wrapper
print(res)

运行结果如下:
C:\Python35\python3.exe G:/python_s3/day20/加上返回值.py
test函数运行完毕
运行时间是3.000171661376953
123
#加上返回值
import time
def timmer(func):
    def wrapper():
        # print(func)
        start_time=time.time()
        res=func() #就是在运行test()     ##主要修改这里1
        stop_time=time.time()
        print('运行时间是%s' %(stop_time-start_time))
        return res     ##修改这里2
    return wrapper
@timmer #语法糖

def test():
    time.sleep(3)
    print('test函数运行完毕')
    return '这是test的返回值'
res=test() #就是在运行wrapper
print(res)

运行结果如下:
C:\Python35\python3.exe G:/python_s3/day20/加上返回值.py
test函数运行完毕
运行时间是3.000171661376953
这是test的返回值

4、函数闭包加上参数

import time
def timmer(func):
    def wrapper(name,age):   #加入参数,name,age
        # print(func)
        start_time=time.time()
        res=func(name,age) ##加入参数,name,age
        stop_time=time.time()
        print('运行时间是%s' %(stop_time-start_time))
        return res
    return wrapper
@timmer #语法糖

def test(name,age): #加入参数,name,age
    time.sleep(3)
    print('test函数运行完毕,名字是【%s】,年龄是【%s】' % (name,age))
    return '这是test的返回值'
res=test('simon',18) #就是在运行wrapper
print(res)

使用可变长参数代码如下:达到的效果是传参灵活

import time
def timmer(func):
    def wrapper(*args,**kwargs): #test('simon',18)  args=('simon') kwargs={'age':18}
        # print(func)
        start_time=time.time()
        res=func(*args,**kwargs) #就是在运行test()     func(*('simon'),**{'age':18})
        stop_time=time.time()
        print('运行时间是%s' %(stop_time-start_time))
        return res
    return wrapper
@timmer #语法糖

def test(name,age):
    time.sleep(3)
    print('test函数运行完毕,名字是【%s】,年龄是【%s】' % (name,age))
    return '这是test的返回值'
def test1(name,age,gender):
    time.sleep(1)
    print('test函数运行完毕,名字是【%s】,年龄是【%s】,性别是【%s】' % (name,age,gender))
res=test('simon',18) #就是在运行wrapper
print(res)

test1('simon',18,'male')

 

5、装饰器的使用

#无参装饰器
import time
def timmer(func):
    def wrapper(*args,**kwargs):
        start_time=time.time()
        res=func(*args,**kwargs)
        stop_time=time.time()
        print('run time is %s' %(stop_time-start_time))
        return res
    return wrapper

@timmer
def foo():
    time.sleep(3)
    print('from foo')
foo()
#有参装饰器
def auth(driver='file'):
    def auth2(func):
        def wrapper(*args,**kwargs):
            name=input("user: ")
            pwd=input("pwd: ")

            if driver == 'file':
                if name == 'simon' and pwd == '123':
                    print('login successful')
                    res=func(*args,**kwargs)
                    return res
            elif driver == 'ldap':
                print('ldap')
        return wrapper
    return auth2

@auth(driver='file')
def foo(name):
    print(name)

foo('simon')

 #验证功能装饰器

#验证功能装饰器
user_list=[
    {'name':'simon','passwd':'123'},
    {'name':'zhurui','passwd':'123'},
    {'name':'william','passwd':'123'},
    {'name':'zhurui1','passwd':'123'},
]
current_dic={'username':None,'login':False}


def auth_func(func):
    def wrapper(*args,**kwargs):
        if current_dic['username'] and current_dic['login']:
            res=func(*args,**kwargs)
            return res
        username=input('用户名:').strip()
        passwd=input('密码:').strip()
        for user_dic in user_list:
            if username == user_dic['name'] and passwd == user_dic['passwd']:
                current_dic['username']=username
                current_dic['login']=True
                res=func(*args,**kwargs)
                return res
        else:
            print('用户名或者密码错误')

        # if username == 'simon' and passwd == '123':
        #     user_dic['username']=username
        #     user_dic['login']=True
        #     res=func(*args,**kwargs)
        #     return res
        # else:
        #     print('用户名或密码错误')
    return wrapper

@auth_func
def index():
    print('欢迎来到某宝首页')
@auth_func
def home(name):
    print('欢迎回家%s' %name)
@auth_func
def shopping_car(name):
    print('%s购物车里有[%s,%s,%s]' %(name,'餐具','沙发','电动车'))

print('before----->',current_dic)
index()
print('after---->',current_dic)
home('simon')
# shopping_car('simon')

#带参数验证功能装饰器

#带参数验证功能装饰器
user_list=[
    {'name':'simon','passwd':'123'},
    {'name':'zhurui','passwd':'123'},
    {'name':'william','passwd':'123'},
    {'name':'zhurui1','passwd':'123'},
]
current_dic={'username':None,'login':False}

def auth(auth_type='filedb'):
    def auth_func(func):
        def wrapper(*args,**kwargs):
            print('认证类型是',auth_type)
            if auth_type == 'filedb':
                if current_dic['username'] and current_dic['login']:
                    res = func(*args, **kwargs)
                    return res
                username=input('用户名:').strip()
                passwd=input('密码:').strip()
                for user_dic in user_list:
                    if username == user_dic['name'] and passwd == user_dic['passwd']:
                        current_dic['username']=username
                        current_dic['login']=True
                        res = func(*args, **kwargs)
                        return res
                else:
                    print('用户名或者密码错误')
            elif auth_type == 'ldap':
                print('这玩意没搞过,不知道怎么玩')
                res = func(*args, **kwargs)
                return res
            else:
                print('鬼才知道你用的什么认证方式')
                res = func(*args, **kwargs)
                return res

        return wrapper
    return auth_func

@auth(auth_type='filedb') #auth_func=auth(auth_type='filedb')-->@auth_func 附加了一个auth_type  --->index=auth_func(index)
def index():
    print('欢迎来到某宝主页')

@auth(auth_type='ldap')
def home(name):
    print('欢迎回家%s' %name)
#
@auth(auth_type='sssssss')
def shopping_car(name):
    print('%s的购物车里有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃'))

# print('before-->',current_dic)
# index()
# print('after--->',current_dic)
# home('simon')
shopping_car('simon')

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄