python学习笔记

class Student():
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def say(self):
        print(self.name,self.age)

#lambda表达式
summ=lambda x,y,z:x+y+z

#高阶函数
def funA():
    print("funA")

funB=funA

#函数名称是变量
#funA和funB只是名称不一样
#既然函数名是变量,则应该可以被当作参数传入另一个函数

def funA(n):
    return n*100

def funB(n):
    return funA(n)*3

def funC(n,f):
    return f(n)*3

def funD(n):
    return n*10

#系统高阶函数-map

def fun(x):
    return 10*x

a=[1,2,3,4]
b=map(fun,a)
#type(b) is a object可迭代
for i in b:
    print(i)

#reduce
#原意是归并,缩减
#把一个可迭代的对象归并成一个结果
#对于作为参数的函数要求:必须有两个参数,必须有返回结果
#reduce([1,2,3,4,5])==f(f(f(f(1,2),3),4),5)
#reduce需要导入functools包

def madd(x,y):
    return x+y
mlist=reduce(madd,[1,2,3,4,5])

#filter函数
#过滤函数:对一组数据进行过滤,符合条件的数据会生成一个新的列表并返回
#跟map相比较
    #相同:都对列表的每一个元素逐一进行操作
    #不同:map生成一个和原来数据相对应的新队列
        #filter不一定,只有符合条件的才会进入新的数据集合
#写法:
    #利用给定函数进行判断
    #返回值一定是一个bool值
    #返回格式:filter(f,data)
def isEven(a):
    return a%2==0

list1=[1,2,3,4,5,6,7,8,9,0]
list2=filter(isEven,list1)
#list2是一个filter object(可迭代对象)

#高阶函数-排序
#sorted()
a=[1,3,234,46,45,4]
sorted(a,key=abs,reverse=True)
#key=abs 安装绝对值排序
astr=['dd','daa','Asd','sda']
sorted(astr,key=str.lower,reverse=False)

#返回函数
    #函数可以返回具体的值
    #也可以返回一个函数作为结果
def myF(a):
    print('In myF')
    return None
a=myF(8)
print(a)
#函数作为返回值返回,被返回的函数在函数体内定义

def myF2():
    def myF3():
        print("In myF3")
        return 3
    return myF3


def myF4(*args):
    def myF5():
        rst=0
        for i in args:
            rst+=i
        return rst
    return myF5
#闭包(closure)
#当一个函数在内部定义函数,并且内部函数应用外部函数的参数或者局部变量
#当内部函数被当作返回值的时候,相关参数和变量保存在返回的函数中,这种结果叫闭包

 

#装饰器(Decrator)
#在不改动函数代码的基础上无限制扩展函数功能的一种机制,
#本质上讲,装饰器是一个返回函数的高阶函数
#装饰器的使用:使用@语法,即在每次要扩展到函数定义前使用@+函数名
def printTime(f):
    def wrapper(*args,**kwargs):
        print("Time:",time.ctime())
        return f(*args,**kwargs)
    return wrapper

#上面定义了装饰器,使用的时候需要用到@,此符号是python语法糖
@printTime
def hello():
    print("hello world")

hello()
#效果相同
printTime(hello)

#time:....
#hello world

#偏函数
    #参数固定的函数,相当于一个有特定参数的函数体
    #functools.partial的作用是,把一个函数某些函数固定,返回一个新函数

int("12356")#字符串转换为十进制数字
int("12344",base=8)#字符串转换为八进制数字

l1=['aa','bb','cc']
l2=[1,2,3]
zip1=zip(l1,l2)
for z in zip1:
    print(z)

#collections模块
#namedtuple
#deque
import collections
Circle=collections.namedtuple("Circle",['x','y','r'])
c1=Circle(0,0,1)
print(c1.x)

#deque双端队列

from collections import deque
q=deque('a','b','c')
print(q)
q.append("d")
print(q)
q.appendleft('x')
print(q)

#defaultdict
#当直接读取dict不存在的属性时,直接返回默认值

#counter 统计字符串个数
from collections import Counter
s="aaabbc"
c=Counter(s)
Counter({'a':3,'b':2,'c':1})

s=['aa','aa','bb','cc','bb']
c=Counter(s)
Counter({'aa':2,'bb':2,'cc':1})

#调试技术 
单元测试->集成测试->交测试部
.分类:
    .静态测试
    .动态测试
#pdb调试
.pdb:python调试库
#pycharm调试
.run/debug模式

#文件
长久保存信息的一种数据信息集合
-常用操作
    打开关闭
    读写内容
    查找
    #open(path,mode)
    mode:r(read),w(write),x(创建方式),a(append),b(binary)
#with语句-上下文管理技术,不需要close()语句

with open("1.txt",'r') as f:
    pass

unittest单元测试框架总结
unittest单元测试框架不仅适用于单元测试,还可以适用EWEB自动化测试用例的开发
与执行,该测试框架可组织执行测试用例,并且提供丰富的断言方法,判断测试用例
是否通过,最终生成测试结果。
1.unittest模块属性
BaseTestSuite
FunctionTestCase
SkipTest
[TestCase]
TestLoader
TestProgram
TestResult
[TestSuite]
TextTEstResult
[TextTestRunner]
.
.
.
[defaultTestLoader]
[expectedFailure]
[main]
[skip]
[skiplf]
[skipUnless]

unittest.TestCase:所有测试用例类的基本类

#多线程VS多进程

import time
import _thread as thread
def loop1():
    print("start loop 1 at :",time.ctime())
    time.sleep(4)
    print("End loop 1 at :",time.ctime())

def loop2():
    print("start loop 2 at :",time.ctime())
    time.sleep(2)
    print("End loop 2 at :",time.ctime())

def main():
    print("starting at :",time.ctime())
    
    thread.start_new_thread(loop1,())
    thread.start_new_thread(loop2,())
    
    print("All done at :",time.ctime())

if __name__=='__main__':
    main()

#######################################


import time
#import _thread as thread
import threading
def loop1():
    print("start loop 1 at :",time.ctime())
    time.sleep(4)
    print("End loop 1 at :",time.ctime())

def loop2():
    print("start loop 2 at :",time.ctime())
    time.sleep(2)
    print("End loop 2 at :",time.ctime())

def main():
    print("starting at :",time.ctime())
    
    t1=threading.Thread(target=loop1,args=())
    t1.start()

    t2=threading.Thread(target=loop2,args=())
    t2.start()

    t1.join()
    t2.join()

    
    print("All done at :",time.ctime())

if __name__=='__main__':
    
    main()
    while True:
        time.sleep(1)
threading的使用
.直接利用threading.Thread生成Thread实例
    1.t=threading.Thread(target=xxx,args=(xx,xx,..))
    2.t.start():启动多线程
    3.t.join():等候多线程执行完成
    4.守护线程-daemon

线程常用属性:
    threading.currenThread:返回当前线程变量
    threading.enumerate:返回一个包含正在运行的线程list
    threading.activeCount:返回正在运行的线程数量
    thr.setName():给线程设置名字
    thr.getName()

继承Thread类
class MyThread(threading.Thread):
    """docstring for ClassName"""
    def __init__(self, arg):
        super(MyThread, self).__init__()
        self.arg = arg
    def run(self):
        time.sleep(2)
        print("the args for this class is {0}".format(self.arg))

for i in range(5):
    t=MyThread(i)
    t.start()
    t.join()
print("Main thread is done!!!!!")

共享变量

##以下代码共享变量会出现问题
sum=0
loopSum=1000000
lock=threading.Lock()
def myAdd():
    global sum,loopSum
    for i in range(1,loopSum):
        lock.acquire()
        sum+=1
        lock.release()
def myMinu():
    global sum,loopSum
    for i in range(1,loopSum):
        lock.acquire()
        sum-=1
        lock.release()
if __name__=='__main__':
    print("Start.....(0)".format(sum))

    t1=threading.Thread(target=myAdd,args())
    t2=threading.Thread(target=myMinu,args())

    t1.start()
    t2.start()

    t1.join()
    t2.join()

    print("Done.......{0}".format(sum))

#解决方法:加锁(lock)、信号量

.锁(Lock)
    -是一个标志,表示一个线程在占有一些资源
    -使用方法
        共享资源
        加锁、释放锁
lock=threading.Lock()

def myAdd():
    global sum,loopSum
    for i in range(1,loopSum):
        lock.acquire()
        sum+=1
        lock.release()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值