枚举法
枚举法
DBS:数据库系统
结构化语言:SQL
数据库管理系统:DBMS
数据库管理员:DBA
SQLite 动态类型
(1)NULL
(2)INTEGER
(3)REAL (decimal, float等,亲源类型)
(4)TEXT
(5)BLOB
SELECT * FROM LinkMan //从LinkMan表中选出所有行所有列字段
INSERT INTO LinkMan (Name,Mobile,BirthDate,IsValid) values ('Tom','15800000000','1990-09-01',1);
UPDATE LinkMan SET BirthDate = '1988-09-01' WHERE rowid = 1
//或
UPDATE LinkMan SET BirthDate = '1988-09-01' WHERE Name = 'Tom'
DELETE FROM LinkMan WHERE OID = 5
SQLite 操作
内置模块 sqlite3
(1)connect('数据文件.db')连接对象
【1】commit() //提交操作
【2】close()
(2)cursor游标 cursor = conn.cursor()
【1】执行语句:execute('SQL语句,[参数]')
import sqlite3
【2】fetchall() 获取所有结果到列表
【3】fetchone() 获取第一条结果到列表
【4】fetchmany(记录数) 获取制定条数结果到列表
(3)参数化查询(不要直接拼接字符串)
【1】目的:避免SQL注入
【2】?:参数传递tuple
【3】:参数名,参数传递字典表
conn = sqlite3.connert(''db\addressbook.db)
c = conn.cursor()
sql = "select * from LinkMan"
result = c.execute(sql)
for row in result:
print(row)
//添加操作
sql = "insert into LinkMan values ('Marry,'18933333333','1985-03-09',1)"
c.excute(sql)
conn.commit()
conn.close()
//修改操作
sql = "UPDATA LinkMan SET Mobile = '18900000000' WHERE Name = 'Peter'"
c.excute(sql)
conn.commit()
conn.close()
//删除操作
sql = "DELETE FROM LinkMan WHERE rowid = 5"
c.excute(sql)
conn.commit()
conn.close()
//fetchall()
sql = "SELECT OID,* FROM LinkMan"
c.execute(sql)
lst = c.fetchall()
for row in lst:
print(row)
//参数化查询(传单个参数)
name = ('Tom',)
sql = "select * from LinkMan where Name = ?"
c.execute(sql,name)
c.fetchall()
//参数化查询(传多个参数)
sql = "insert into LinkMan values (:name,:mobile,:birtthdate,:isvalid)"
c.excute(sql,{"name":John,"mobile":"18900000000","birthdate":"1989-09-03","isvalid":1})
import csv
def csv_write():
headers = ["编号","课程","讲师"]
rows = [
(1,'Python','Eason'),
(2,'C#,'Eason'),
(1,'.NET','Eason')
]
with open('mu_course.csv','w',encoding = 'utf8',newline = '' ) as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(rows)
def csv_writer_dict()
headers = ['ID','Title','Org','Url']
rows = [
{'ID':1, 'Title': 'Python', 'Org': 'Youpinketang','Url':'uke.cc'}
]
with open ('my_course2.csv','w', encoding = 'utf8') as f:
writer = csv.DictWriter(f,headers)
writer.writeheader()
writer.writerows(rows)
if __name__ =='__main__':
csv_writer()
json
import json
def jason_basic()
data = {
"ID":1
"课程":"优品课堂"
"单价":"98.00"
}
json_str = json.dumps(data)
json_data = json.loads(json_str)
def json_writer_filr():
data = {
"ID":1
"课程":"优品课堂"
"单价":"98.00"
}
with open('data.json','w',encoding='utf8') as f:
json.dump(data,f) //不是写在字符串,而是直接写在文档,所以dump后不用s
def json_read_filr():
with open('data.json','r',encoding = 'utf8') as f:
data = json.load(f)
print(data)
def json_type_diff():
print(json.dumps(False))
if __name__=='__main__':
excel
import xlrd
def xl_read()
book = xlrd.open_workbook('product.xls')
for sheet in book.sheets():
print(sheet.name)
def xl_read_dada():
book = xlrd.open_workbook('product.xls')
sheet = book.sheet_by_name("Product")
print('工作簿':{}.format(sheet.name))
print('数据行数':{}.format(sheet.rows))
print('产品数据')
for i in range(sheet.rows):
print(sheet.row_values(i)) //获取索引指定的数据行
csv
import csv
from collections import namedtuple
def csv_read()
with open('product.csv',encoding = 'utf8') as f:
reader = csv.reader(f)
headers = next(reader)
print(headers)
for row in reader:
print('编号{}\t产品{}\t单价{}'.format(row[0],row[1],row[3]))
def csv_read_by_namedtuple():
with open('product.csv', encoding = 'utf8') as f:
reader = csv.reader(f)
headers = next(reader)
Row = namedtuple('Row', headers)
for r in reader:
row = Row (*r)
print(row)
for row in reader:
print(row)
def csv_read_by_dict():
with open('product.csv',encoding = 'utf8') as f:
reader = csv.DictReader(f)
if __name__ =='__main__':
csv_read()
threading模块
(1).Thread线程类
【1】构造:
--1.Thread(target = 目标函数, args = (参数,))
--2.自定义Thread派生类,重写run方法逻辑
【2】.start() 启动线程
【3】.join() 要求主线程等待
【4】.current_thread() 获取当前线程
【5】.name 线程名称
import time
import threading
//方法1
def worker(n):
print('{threading.current_thread().name}函数执行开始于:{}'.format(time.ctime()))
time.sleep(n)
print('{threading.current_thread().name}函数执行结束于:{}'.format(time.ctime()))
def main():
print(f'[住函数执行开始于:{time.ctime()}]')
threads = []
t1 = threading.Thread(target = worker, args = (4,))
threads.append(t1)
t2 = threading.Thread(target = worker, args = (2,))
threads.append(t2)
for t in threads:
t.start()
for t in threads:
t.join() //告诉主线程等待当前线程执行完毕
print(f'[住函数执行结束于:{time.ctime()}]')
//方法2
def worker(n):
print('{threading.current_thread().name}函数执行开始于:{}'.format(time.ctime()))
time.sleep(n)
print('{threading.current_thread().name}函数执行结束于:{}'.format(time.ctime()))
class MyThread(threading.Thread):
def __init__(selffunc,args):
threading.Thread.__init__(self)
self.func = func
self.args = args
def run(self):
self.func(*self.args)
def main():
print(f'[住函数执行开始于:{time.ctime()}]')
threads = []
t1 = MyThread(worker, (4,))
threads.append(t1)
t1 = MyThread(worker, (2,))
threads.append(t2)
for t in threads:
t.start()
for t in threads:
t.join() //告诉主线程等待当前线程执行完毕
print(f'[住函数执行结束于:{time.ctime()}]')
若添加装饰器顺序为:
@div_decorator
@p_decorator
则先加<p>标签,再加<div>标签
装饰器的参数
def tags(tag):
def tag_decorator(func):
def wrapper(*args, **kwargs):
return f'<{tag}>{func(*args,**kwargs)}</{tag}>'
return wrapper
return tag_decorator
@tags('p')
def get_upper_text(text):
return text.upper()
类定义装饰器
1、脚本里的普通函数上
class P:
def __init(self,func):
self.func = func
def __call__(self, *args, **kwargs):
return '<p>' + self.func(*args, **kwargs) + '</p>'
@P
def get_text():
return '欢迎学习'
@P
def get_upper_text (text):
return text.upper()
if __name__ == '__main__':
print(get_upper_text('abc'))
函数定义的装饰器可以用在
1、脚本里的普通函数上
2、用在另外一个类定义的方法上
编写装饰器:
函数定义装饰器
def p_decorator(func):
def wrapper(*args,**kwargs):
return '<p>' + func(*args,**kwargs) +'</p>'
return wrapper
@p_decorator
def get_text():
return '欢迎学习'
if __name__ =='__main__':
print(get_text())
//或:
def p_decorator(func):
def wrapper(*args,**kwargs):
return '<p>' + func(*args,**kwargs) +'</p>'
return wrapper
def get_text():
return '欢迎学习'
if __name__ =='__main__':
html = p_decorator(get_text)
print(html())
//修饰带参数的情况
def p_decorator(func):
def wrapper(*args,**kwargs):
return '<p>' + func(*args,**kwargs) +'</p>'
return wrapper
@p_decorator
def get_upper_text(text):
return text.upper()
if __name__ =='__main__':
html = p_decorator(get_text)
print(html())
函数基础
(3)函数嵌套及跨域返回
def greeting():
def hello():
return 'Hello'
return hello()
greeting() //返回hello()的结果
def greeting():
def hello():
return'Hello'
return hello
greeting() //返回函数对象
greeting()() //返回hello()的结果
跨越访问:
def func_1():
x = 10
def func_2():
x= 20
return x+10
return func_2()
func_1() //返回30
def func_1():
x = 10
def func_2():
nonlocal x //上一层的x
return x+10
return func_2()
func_1() //返回20
装饰器
(1)用于管理和增强函数和类行为的代码
(2)提供一种在函数或类定义中插入自动运行代码的机制
(3)特点:
【1】语法更明确
【2】更高的代码可维护性
【3】更好的一致性
函数基础
(1)函数可以赋给另外一个变量
def hello(name):
print('Hello',name)
greeting = hello
greeting('Tom') //返回Hello Tom
hello('Jerry') //返回Hello Jerry
(2)将函数作为参数传递
def add(a,b):
return a+b
def subtracy(a,b):
return a - b
def action(x,y,func): //委托
return func(x,y)
action(5,3,add)
concurrent.futures模块(抽象层)
【1】IO密集型 -- 多线程
【2】计算密集型 -- 多进程
ThreadPoolExecutor
ProcessPoolExecutor
import time
import concurrent.futures
numbers = list (range(1,11))
def count(n)
for i in range(1000000):
i+=1
return i* n
def worker(x):
result count(x)
print(f'数字:{x}的计算结果是{result}')
//顺序执行
def sequntial_execution():
start_time.clock()
for i in numbers:
worker(i)
print(f'顺序执行花费时间:{tim() - start_time}秒')
//多线程执行
def threading_execution():
start_time = time.clock()
with concurrent.futures.ThreadPoolExecutor(max_workers = 5) as excutor:
for i in numbers:
executor.submit(worker,i)
print(f'顺序执行花费时间:{tim() - start_time}秒')
//进程池执行
def process_execution():
start_time = clock.clock()
with concurrent.futures.ProcessPoolExecutor(max_workers = 5) as excutor:
for i in numbers:
executor.submit(worker,i)
print(f'顺序执行花费时间:{tim() - start_time}秒')
if __name__ == '__main__':
sequential_excution()
threading_excution()
process_execution()
IO操作首选多线程
计算操作用别的首先实现并发较好:multiprocessing模块
充分运用多核,多CPU的计算能力,适用于计算密集型任务
import time
import multiprocessing
def func(n):
print(f'{multiprocessing.current_process().name}执行开始于:{time.ctime()}')
time.sleep(n)
print(f'{multiprocessing.current_process().name}执行开始于:{time.ctime()}')
def main ():
print(f'主函数运行于:{time.ctime()}')
processes = []
p1 = multiprocessing.Process(target = func, args = (4,))
processes.append(p1)
p2 = multiprocessing.Process(target = func, args = (2,))
processes.append(p2)
for p in processes:
p.start()
for p in procsses:
p.join()
print(f'主函数结束于:{time.ctime()}')
if __name__ =='__main__':
main()
队列
【1】queue模块
(1)Queue 先进先出队列 FIFO ---1.Queue(maxsize = 0) 构造实例
--2.put(block = True, timeout = None)放入数据项
--3.get(block = True, timeout = None) 获取数据项
--4.task_done() 声明当前队列任务处理完毕
--.4.join() 队列所有项处理完毕前阻塞
(2)LifoQueue 后进先出队列 LIFO --
(3)PriortyQueue 优先队列
import threading
import queue
import time
import random
def producer(data_queue):
for i in range(5):
time.sleep(0,5)
item = random.randint(1,100):
data_queue.put(item)
print(f'{threading.current_thread().name}在队列中放入数据项:{item}')
def consumer(data_queue):
while True:
try:
item = data_queue.get(timeout = 3)
print(f'{threading.current_thread().name}从队列中移除了{item}')
except queue.Empty:
break
else:
data_queue.tast_done()
def main():
q = queue.Queue()
threads = []
p = threading.Thread(target = producer, args= (q,))
p.start()
for i in range (2):
c = threading.Thread(target = consumer, arg = (q,))
p.start()
for t in threads:
t.start()
for t in threads:
t.join()
q.join()
if __name__ == '__main__':
main()
threading
threading.Lock 同步原语 -- 锁
需要共享的数据操作时用
【1】.acquire() 获得
【2】.release() 释放
【3】支持上下文操作 with lock:
import threading
import time
import random
eggs = []
lock - threading.Lock()
def put_egg (n, lst):
lock.acqire()
for i in range (1,n+1)
time.sleep(random,randint(0,2))
lst.append(i)
lock.release()
//或:
def put_egg (n, lst):
with lock:
for i in range (1,n+1)
time.sleep(random,randint(0,2))
lst.append(i)
def main():
treads = []
for i in range (3):
t - threading.Thread (target = put_egg, args = (5, eggs))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
print(eggs)
单线程:
import time
def worker(n):
print(f'函数执行开始于:{time.ctime()}')
time.sleep(n)
print(f'函数执行结束于:{time.ctime()}')
def main():
print(f'[主函数执行开始于:{time.ctime()}]')
worker(4)
worker(2)
print(f'[主函数执行开始于:{time.ctime()}]')
多线程
内置_thread模块
import time
import _thread
def worker(n):
print(f'函数执行开始于:{time.ctime()}')
time.sleep(n)
print(f'函数执行结束于:{time.ctime()}')
def main():
print(f'[主函数执行开始于:{time.ctime()}]')
_thread.start_new_thread(worker,(4,)) //要已元组方式给值
_thread.start_new_thread(worker,(2,))
time.sleep(6)
worker(2)
print(f'[主函数执行开始于:{time.ctime()}]')
_thread:
(1)没有控制进程结束机制
(2)只有一个同步原语(锁)
(3)功能少于threading模块
.start_new_thread(function, args, **kwargs=n)
并发编程
1.概述
(1)非并发
【1】程序由单个步骤序列组成
【2】包含独立子任务的程序执行性能低
(2)并发
【1】异步,高效
【2】分解子任务、简化流程与逻辑
(3)进程 process
【1】一个程序的执行实例
【2】每个进程都有自己的地址空间、内存、数据线及辅助数据
(4)线程 thread
【1】同一进程内,可被并行激活的控制流
【2】空想相同上下文(空间地址、数据结构)
【3】特点:便于信息共享和通信
【4】线程访问顺序差异会导致结果不一致(条件 race condition)
(5)python全局解释器锁
【1】Global Interpreter Lock
【2】Python代码有虚拟机(解释器主循环)控制
【3】主循环同时只能有一个控制线程执行
windows+x
A
os
文件处理
1. .mkdir('目录名') //创建目录
2. .rmdir('目录名') //删除目录
3. .rename('旧名','新名') 改名
4. .remove('文件名') //删除文件
open('info.txt'.'w',encoding = 'utf8').write('Hello')
os.listdir()
os.rename('info.txt','detail.txt')
os.remove('detail.txt')
os.chdir('..')
可移植工具
os.sep
//显示分隔符,输出'\\'
os.pathsep
//显示路径分隔符,输出'; '
os.curdir
//当前目录符号'.'
os.pardir
//上级目录符号'..'
路径模块 .path
import os
os.path.isdir(r'd:\iso')
//是否一个目录
os.path.isfile(r'd:\iso')
//是否一个文件
os.path.exists(r'd:\iso')
//是否存在目录
os.path.getsize(r'd:\add.py')
//判断文件大小(单位为字节)
os.path.split(r'c:\data\temp\data.txt')
//拆分切割路径,拆分为:路径名+文件名
name = r'c:\data\temp\data.txt'
os.path.dirname(name)
//返回:'d:\\data\\temp'
os.path.basename(name)
//返回'data.txt'
os.path.splitext(name)[1]
//返回'.txt',分割扩展名和路径
os.path.join(r'c:\temp','product.csv')
//连接操作,返回'c:\\temp||product.csv'
name.split(os.sep)
//按照双斜杠切割
p = 'D\\app\\db/files/data.csv'
os.path.normpath(p)
//标准化路径
os.path.abspath('..')
//绝对化路径:看上级绝对路径,不切换路径
shell变量
os.environ
管理工具
os.getcwd()
//输出:'F:\\RECORDED\Python\Examples\\SEC32',显示工作目录
os.listdir()
//显示当前工作目录内的内容
os.chdir('db')
//改变目录
os.listdir(r'd:')
//显示d盘根目录
os.getpid()
//显示当前目录的pid
os.getppid()
//显示父进程pid
运行shell命令
window下:
dir //显示目录
cd //切换目录
cd.. //返回上级目录
dir /b //以简洁方式显示目录下信息
type add.py //命令提示符下显示具体代码信息
shell下:
import os
.system() python脚本中运行shell命令
.popen() 运行命令并且连接输入输出流
txt = os.popen ('dir \b').read()
results = os.popen('type add.py').readlines()
sys
import sys
dir(sys)
sys.platform //当前程序运行在什么平台上
sys.version
sys.path //运行时不需要导入的路径
1.平台与版本
sys.platform
sys.version
sys.path
sys.modules
2.观察异常细节
sys.exc_info() 获取最后一次异常细节
显示出错代码位置更多的系列traceback.print_tb(sys.exc_info()[2])
import sys
try:
raise keyError
except:
print('有错误发生!')
//或
try:
raise keyError('错误的键值!')
except keyError as e:
print(e)
import traceback
try:
raise keyError
except:
print(sys.exc_info())
tracyback.print_tb(sys.exc_info()[2])
//抛出最后一次异常,可以显示最后一次异常的具体信息
3.命令行参数
sys.argv
def add():
a = 5
b = 3
return a+b
print(add())
ctrl+s保存
打印命令行参数:
import sys
def add():
a = 5
b = 3
return a + b
print(sys.argv)
print(sys.argv[0])
def add(a,b)
return a + b
a = 0
b = 0
if sys.argv[1] :
a = int(sys.argv[1])
if sys.argv[2]:
b = int(sys.argv[2])
print(add(a,b))
标准流
import sys
sys.stdin 标准输入流
sys.stdout.write('hello')
sys.stdout 标准输出流
print('请输入信息:'); sys.stdin.readline()[:]
sys.stderr 标准错误流
sys.stderr.write('错误消息')
sys.stderr.flush() //让缓存中的内容完成实际操作