复习-基础
一、review-base
其他语言吗和python的对比
c vs Python
c语言是python的底层实现,解释器就是由python编写的。
c语言开发的程序执行效率高,开发现率低(内存的管理),python开发效率更高
java vs python
同一个级别,都需要解释器老解释代码。
python简洁方便,java繁琐。
python对于机器学习等强大的类库(模块)。
解释型语言和编译型语言
解释型:边解释边执行(即时翻译)。 代表:python,php,shell 编译型:将所有代码编译成指定文件。如:.dll,然后再去执行.dll文件。 代表:c,c++,Go,c#,java
字节和位的关系
1Byte = 8bit 1Kb = 1024Byte 1Mb = 1024Kb 1Gb = 1024Mb 1Tb = 1024Gb
Python中如何实现 二进制、八进制、十进制、十六进制 之间的转换?
# 其他转十进制 a1 = "010101010101111" int(a1,base=2)# 十进制转二进制
bin(8123)
# 十进制转八进制
oct(87234)
# 十进制转十六进制
hex(3453)
练习:
如 10.3.9.12 转换规则为: 10 00001010 3 00000011 9 00001001 12 00001100 再将以上二进制拼接起来计算十进制结果:00001010 00000011 00001001 00001100 = ?
def ip_func(): ip_num = input("请输入一个ip地址:") s = "" ip_list = ip_num.split(".") for i in ip_list: i = bin(int(i)).lstrip("0b") if len(i) != 8: i = i.zfill(8) s += i return int(s,2) print(ip_func())
字节码和机器码
机器码:是汇编的结果,给操作系统直接读取使用。
字节码:字节码是一种中间状态(中间码)的二进制代码(文件)。需要直译器转译后才能成为机器码。例如:xxx.pyc文件,这个就是python中的中间文件
执行脚本头文件 #!/usr/bin/env python
Linux下运行python文件: 方式一: a.py print(123)python a.py
方式二:
a.py
#!/usr/bin/env python
print(123)赋予可执行权限 .</span>/a.py </pre>
执行脚本头文件 # --coding:utf-8 -*-
py2中:默认编码是asicc(切记!py2一定要用--coding:utf-8 -*-)
py3中:默认编码是utf-8
运算符
v1 = 1 or 2 # 1 v2 = 6 and 1 or 2 and 9 # 1 v3 = 1 or 3 # 1 v4 = 1 and 3 # 3 v5 = 0 and 2 and 1 # 0 v6 = 0 and 2 or 1 # 1 v7 = 0 and 2 or 1 or 4 # 1 v8 = 0 or False and 1 # False
三元表达式/三元运算/三目运算
v1 = 'x1' if 1==1 else 'x2'
常见数据类型
str: split、join、strip、upper
list: append、pop、insert、extend、reverse
tuple:
dict: ....
练习题:
示例1: data = [ {'id': 1, 'name': 'x1'}, {'id': 2, 'name': 'x2'}, {'id': 3, 'name': 'x3'}, {'id': 4, 'name': 'x4'}, ] data_dict = {} for item in data: data_dict[item["id"]] = item print(data_dict) while True: nid = int(input("请输入id")) if nid in data_dict: print("success") else: print("faile")
示例2: data = [ {'id': 1, 'name': 'x1'}, {'id': 2, 'name': 'x2'}, {'id': 3, 'name': 'x3'}, {'id': 4, 'name': 'x4'}, ] data_dict = {} for item in data: data_dict[item["id"]] = item data_dict[2]["name"] = "鹿晗" print(data)
示例3: data = [ {'id': 1, 'name': 'x1','pid':None}, {'id': 2, 'name': 'x2','pid':1}, {'id': 3, 'name': 'x3','pid':2}, {'id': 4, 'name': 'x4','pid':1}, ] data_dict = {} for item in data: item["children"] = [] data_dict[item["id"]] = item # print(data_dict) for item in data: pid = item["pid"] if not pid: continue data_dict[pid]["children"].append(item) print(data_dict)
练习题: v = [11, 232, 122, 13, 122, 31, 123, 111] 获取列表中第二大的数字。 def second(ln): max = 0 s = {} # 查看个数出现的次数然后与赋值给max, for i in range(len(ln)): flag = 0 for j in range(len(ln)): if ln[i] >= ln[j] and i != j: flag = flag + 1 s[i] = flag print(s) if flag > max: max = flag for i in s: if s[i] == max - 1: break print(ln[i]) second([11,232,122,13,122,31,123,111] )
函数的参数
def func(a1, a2=[]): a2.append(a1) return a2v1 = [11, 22, 33]
result1 = func(55)
print(result1) # [55]
result2 = func(44, v1)
print(result2) # [11, 22, 33, 44]
result3 = func(66)
print(result3) # [55, 66]
闭包函数
SQLAlchemy源码 class Query(object):</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> get(self, a1): </span><span style="color: #0000ff;">return</span> 1 <span style="color: #0000ff;">def</span><span style="color: #000000;"> filter(self, a1, a2): </span><span style="color: #0000ff;">return</span> 2 <span style="color: #0000ff;">class</span><span style="color: #000000;"> NewFoo(object): </span><span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span><span style="color: #000000;"> method(name): </span><span style="color: #0000ff;">def</span> inner(*args, **<span style="color: #000000;">kwargs): </span><span style="color: #0000ff;">return</span> getattr(Query, name)(*args, **<span style="color: #000000;">kwargs) </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> inner </span><span style="color: #0000ff;">for</span> name <span style="color: #0000ff;">in</span> [<span style="color: #800000;">'</span><span style="color: #800000;">get</span><span style="color: #800000;">'</span>, <span style="color: #800000;">'</span><span style="color: #800000;">filter</span><span style="color: #800000;">'</span><span style="color: #000000;">]: setattr(NewFoo, name, method(name)) obj </span>=<span style="color: #000000;"> NewFoo() </span><span style="color: #0000ff;">print</span>(obj.get(1111))</pre>
Stark组件 class UserInfoConfig(ModelStark):</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> show_gender(...): </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> is_header: </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">性别</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> obj.get_gender_display() </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> show_level(...): </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> is_header: </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">级别</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> obj.get_level_display() </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> show_status(...): </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> is_header: </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">状态</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> obj.get_status_display() list_display </span>= [<span style="color: #800000;">'</span><span style="color: #800000;">name</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">email</span><span style="color: #800000;">'</span><span style="color: #000000;">,show_gender,show_level,show_status] </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> get_display(filed_name,title): </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> show(obj,is_header): </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> is_header: </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> title tpl </span>= <span style="color: #800000;">"</span><span style="color: #800000;">get_%s_display</span><span style="color: #800000;">"</span> %<span style="color: #000000;">filed_name </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> getattr(obj,tpl)() </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> show </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> UserInfoConfig(ModelStark): list_display </span>= [<span style="color: #800000;">'</span><span style="color: #800000;">name</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">email</span><span style="color: #800000;">'</span>,get_display(<span style="color: #800000;">'</span><span style="color: #800000;">gender</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">性别</span><span style="color: #800000;">'</span>),get_display(<span style="color: #800000;">'</span><span style="color: #800000;">level</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">级别</span><span style="color: #800000;">'</span>),get_display(<span style="color: #800000;">'</span><span style="color: #800000;">status</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">状态</span><span style="color: #800000;">'</span>)]</pre>
装饰器
编写装饰器计算函数执行时间
import time from functools import wrapsdef time_this_function(func):
# 作为装饰器使用,返回函数执行需要花费的时间
@wraps(func)
def wrapper(args, **kwargs):
start = time.time()
result = func(args, **kwargs)
end = time.time()
# 函数的名字
print(func.name, end - start)
return result</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> wrapper
if name == 'main':
@time_this_function
def count_number(n):
while n>0:
time.sleep(0.1)
n+=-1
count_number(10)
#!/usr/bin/env python # -*- coding:utf-8 -*-import time
def deco_limit(s, foo= None):
"""
这是个函数装饰器,可以控制访问频率
:param s: 该参数为访问频率,每多少s一次
:param foo: 可选参数,用户自定制装饰器逻辑(比如cookie或状态验证等,执行返回值为False则不执行函数) #该参数实现了,我又删了,不好用,给你提供思路自己做
:return:
"""
def wrpper(func):
"""
该函数接受被修饰函数作为参数,返回装饰器,变量func_identify为变量标识,
存储被修饰函数的name为key,value为调用成功的时间戳。key-second存储为时间频率
:param func: 参数为被修饰函数
:return:
"""
name = func.name
func_identify ={name: 0,'foo': foo, 'second': s}
def inner(args, **kwargs):
"""
执行函数
:param args: 将接收的参数,打包
:param kwargs:
:return:
"""
useable_time = func_identify[name] + func_identify['second']
time_now = time.time()
remain_time = useable_time-time_now
# print(remain_time)
if time_now > useable_time:
func_identify[name] = time_now #设置调用时间
res = func(args,**kwargs)
else:
print('