Skip to content

Commit 9898cdf

Browse files
committed
1.练习
1 parent 44ebfac commit 9898cdf

File tree

7 files changed

+301
-0
lines changed

7 files changed

+301
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import functools
2+
3+
#01.偏函数Python的functools模块提供了很多有用的功能,其中一个就是偏函数(Partial function)。要注意,这里的偏函数和数学意义上的偏函数不一样。
4+
# 在介绍函数参数的时候,我们讲到,通过设定参数的默认值,可以降低函数调用的难度。而偏函数也可以做到这一点
5+
6+
7+
# print(int('12345'))
8+
#
9+
# print(int('12345',base=16))
10+
# print(int('7456',16))
11+
#
12+
# print(int('12345',8))
13+
# print(int('12345',8))
14+
15+
16+
def int2(x, base=2):
17+
return int(x, base)
18+
19+
# print(int2('1000000'))
20+
21+
22+
# 02.functools.partial就是帮助我们创建一个偏函数的,不需要我们自己定义int2(),可以直接使用下面的代码创建一个新的函数int2:
23+
# 简单总结functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单。
24+
25+
26+
int3= functools.partial(int, base=2)
27+
print(int3('1010101'))
28+
29+
30+
max2 = functools.partial(max, 10)
31+
print(max2(5, 6, 7))
32+
# 以上和下面类似相当于
33+
args = (10, 5, 6, 7)
34+
print(max(*args))
35+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import functools
2+
3+
4+
# 01.函数也是对象
5+
# def fun1():
6+
# return 1
7+
#
8+
# f1=fun1()
9+
# print(f1)
10+
# print(fun1.__name__)
11+
12+
# 02.如要实现函数前后打印日志的功能,则需要装饰器
13+
14+
def fun2_log(fun):
15+
def wrapper(*args,**kw):
16+
print('call %s():'%fun.__name__)
17+
return fun(*args,**kw)
18+
return wrapper
19+
20+
def fun2_log1(text):
21+
def decorator(fun):
22+
def wrapper(*args,**kw):
23+
print('%s %s():'%(text,fun.__name__))
24+
return fun(*args,**kw)
25+
return wrapper
26+
return decorator
27+
28+
def fun2_log2(text):
29+
def decorator(fun):
30+
@functools.wraps(fun)#主要为解决函数名称问题
31+
def wrapper(*args,**kw):
32+
print('%s %s():'%(text,fun.__name__))
33+
return fun(*args,**kw)
34+
return wrapper
35+
return decorator
36+
37+
38+
@fun2_log
39+
def fun3_name(name):
40+
print('hello!',name)
41+
42+
# fun3_name('lucy')
43+
44+
45+
@fun2_log1('excute')
46+
def fun3_name1(name):
47+
print('hello!',name)
48+
49+
fun3_name1('lucy')
50+
51+
print(fun3_name1.__name__)
52+
53+
print('=========================')
54+
55+
@fun2_log2('excute2')
56+
def fun3_name2(name):
57+
print('hello!',name)
58+
59+
fun3_name2('lucy')
60+
61+
print(fun3_name2.__name__)
62+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 01.此类型函数,就是将函数内的函数作为一个返回值返回,懒加载
2+
3+
# def fun1(*args):
4+
# def sum():
5+
# retVal=0
6+
# for i in args:
7+
# retVal=retVal+i
8+
# return retVal
9+
# return sum
10+
#
11+
# f1=fun1(1,2,3)
12+
# f2=fun1(1,2,3)
13+
# print(f1)
14+
# print(f2)
15+
# print(f1())
16+
# print(f2())
17+
#
18+
# print(f1==f2)
19+
20+
21+
# 02.重点注意:
22+
# 1.返回的函数并非立即执行,是直到调用后,才执行
23+
# 2.此处注意,返回的3个函数,都是9
24+
25+
def fun2():
26+
fs=[]
27+
for i in range(1,4):
28+
29+
def f():
30+
return i**2
31+
32+
fs.append(f)
33+
34+
return fs
35+
36+
f1,f2,f3=fun2()
37+
38+
print(f1())
39+
print(f2())
40+
print(f3())
41+
42+
# 03.以上,如果必定需要循环引用变量,是再创建一个函数,用该函数的参数绑定循环变量当前的值,无论该循环变量后续如何更改,已绑定到函数参数的值不变:
43+
44+
def fun3():
45+
46+
def f(j):
47+
def g():
48+
return j**2
49+
50+
return g
51+
52+
fs=[]
53+
for i in range(1,4):
54+
fs.append(f(i))
55+
return fs
56+
57+
f1,f2,f3=fun3()
58+
59+
print(f1())
60+
print(f2())
61+
print(f3())
62+
63+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 01.Pillow图片处理
2+
from PIL import Image, ImageFilter
3+
import requests
4+
import psutil
5+
6+
# print('Original image size: %sx%s' % (w, h))
7+
# # 缩放到50%:
8+
# im.thumbnail((w//2, h//2))
9+
# print('Resize image to: %sx%s' % (w//4, h//4))
10+
# # 把缩放后的图像用jpeg格式保存:
11+
# im.save('D:/2.jpg', 'jpeg')
12+
13+
14+
15+
# 打开一个jpg图像文件,注意是当前路径:
16+
# im = Image.open('D:/1.jpg')
17+
# # 应用模糊滤镜:
18+
# im2 = im.filter(ImageFilter.BLUR)
19+
# im2.save('D:/1blur.jpg', 'jpeg')
20+
21+
22+
# r = requests.get('https://www.liaoxuefeng.com/wiki/1016959663602400/1183249464292448')
23+
#
24+
# print(r.status_code)
25+
# print(r.encoding)
26+
# print(r.content)
27+
28+
print(psutil.cpu_count())
29+
print(psutil.cpu_times())
30+
31+
# for x in range(10):
32+
# print(psutil.cpu_percent(interval=1, percpu=True))
33+
34+
print(psutil.virtual_memory())
35+
print(psutil.swap_memory())
36+
print(psutil.disk_partitions())
37+
38+
print( psutil.pids())
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from datetime import *
2+
3+
dtnow=datetime.now()
4+
print(dtnow)
5+
print(type(dtnow))
6+
7+
dt1=datetime(2020,4,6,14,0,22)
8+
print(dt1)
9+
10+
print(datetime.now().timestamp())
11+
12+
print('======================')
13+
14+
#01.字符串转化为datetime
15+
16+
day1 = datetime.strptime('2015-6-1 18:19:59', '%Y-%m-%d %H:%M:%S')
17+
print(day1)
18+
19+
#02.datetime转化为字符串
20+
day2=datetime.now()
21+
print(day2.strftime('%Y-%m-%d %H:%M:%S'))
22+
print(day2.strftime('%a, %b %d %H:%M'))
23+
24+
25+
26+
dtFormat='%Y-%m-%d %H:%M:%S'
27+
day3=datetime.now()
28+
29+
tmp1=day3 + timedelta(hours=10)
30+
print(tmp1.strftime(dtFormat))
31+
32+
tmp1=day3 + timedelta(days=10)
33+
print(tmp1.strftime(dtFormat))
34+
35+
tmp1=day3 + timedelta(days=365)
36+
print(tmp1.strftime(dtFormat))
37+
38+
tmp1=day3 + timedelta(days=1,hours=1)
39+
print(tmp1.strftime(dtFormat))
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
from collections import *
3+
from urllib import request,parse
4+
5+
# 解析xml
6+
from xml.parsers.expat import ParserCreate
7+
# 解析html
8+
from html.parser import HTMLParser
9+
from html.entities import name2codepoint
10+
11+
12+
13+
# 01.namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素
14+
Point =namedtuple('Point',['a','b'])
15+
p1=Point(2,3)
16+
print(p1.a)
17+
print(p1.b)
18+
19+
print(isinstance(p1,Point))
20+
print(isinstance(p1,tuple))
21+
22+
# 同理,可以用来表示一个圆形
23+
24+
Circle = namedtuple('Circle', ['x', 'y', 'r'])
25+
26+
27+
# 使用list存储数据时,按索引访问元素很快,但是插入和删除元素就很慢了,因为list是线性存储,数据量大的时候,插入和删除效率很低。
28+
# deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈:
29+
30+
31+
q = deque(['a', 'b', 'c'])
32+
33+
q.appendleft('0')
34+
print(q)
35+
36+
37+
dict1 = defaultdict(lambda: 'N/A')
38+
print(dict1['dd'])
39+
40+
41+
dict2 = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
42+
43+
44+
# ChainMap可以把一组dict串起来并组成一个逻辑上的dict。ChainMap本身也是一个dict,但是查找的时候,会按照顺序在内部的dict依次查找。
45+
# Counter是一个简单的计数器,例如,统计字符出现的个数:
46+
47+
48+
49+
50+
51+
52+
53+
with request.urlopen('https://api.douban.com/v2/book/2129650') as f:
54+
data = f.read()
55+
print('Status:', f.status, f.reason)
56+
for k, v in f.getheaders():
57+
print('%s: %s' % (k, v))
58+
print('Data:', data.decode('utf-8'))
59+
60+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from turtle import *
2+
3+
4+

0 commit comments

Comments
 (0)