一、私有

class DB:
port = 3306 #类变量
def __init__(self):
self.host = '127.0.0.1'
self.__user = 'root' #实例变量,成员变量
self.__password = '123456'
self.db = 'xx'
# self.__help()
def sql(self,sql):
self.__help()
print('执行sql')
def __help(self):
print(self.host)
print(self.__user)
print(self.__password)
print(self.db)
print(self.port) def get_port(self): #实例方法
print(self.port) @classmethod
def help(cls):
print('这个类是用来连接数据库的,它的用法是xxx')
print('cls的内存地址',id(cls)) DB.help() 二、多线程及多线程下载网页
import threading
import time
def run():
time.sleep(5)
print('over') start_time = time.time()
for i in range(100):
t = threading.Thread(target=run)
t.start() print('运行的时候几个线程',threading.activeCount() ) # 1
#threading.activeCount就是当前有几个线程
while 1:
if threading.active_count()==1:
break print('这时候几个线程',threading.activeCount()) # 2
end_time = time.time()
print('run time =',end_time - start_time) # print('运行结束') ''' 第一种for循环主线程等待子线程运行结束
start_time = time.time()
ths = [] #100
for i in range(100):
t = threading.Thread(target=run) #实例化一个线程,
t.start()#启动这个线程
ths.append(t) for t in ths:
t.join() #循环等待每个子线程执行结束
end_time = time.time()
print('run time =',end_time - start_time)
''' # start_time = time.time()
# run()
# run()
# run()
# run()
# end_time = time.time()
# print('run time =',end_time - start_time)
import requests
import time
import threading
def download_html(url,file_name):
res = requests.get(url)
with open(file_name+'.html','wb') as fw:
fw.write(res.content)
print('【%s】下载完成'%file_name) urls={
'nnzhp':'http://www.nnzhp.cn',
'cc':'http://www.cc-na.cn',
'dsx':'http://www.imdsx.cn',
'besttest':'http://www.besttest.cn',
} start_time = time.time() for file_name,url in urls.items():
t = threading.Thread(target=download_html,args=(url,file_name))
#args=(url,) #单个参数的时候,一定要这么写
t.start()
while threading.active_count()!=1:
pass end_time = time.time()
print('run time =',end_time - start_time) #单线程
# start_time = time.time()
# for k,v in urls.items():
# download_html(url=v,file_name=k)
# end_time = time.time()
# print('run time =',end_time - start_time) 三、静态方法及守护线程
import turtle as t
import time
class Pig:
words = '老铁们,爱你们'
@classmethod
def pq(cls):
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255, 155, 192), "pink")
t.setup(840, 500)
t.speed(0.5) # 鼻子
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
t.begin_fill()
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill() t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill() t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill() # 头
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill() # 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill() t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill() # 眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill() t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill() t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill() t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill() # 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill() # 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80) # 身体
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill() # 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90) t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90) # 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20) t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20) # 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.up()
t.goto(180, 90)
t.pencolor('red')
t.write(cls.words, font=('微软雅黑', 15, 'normal'))
time.sleep(50)
@staticmethod
def about_me():
print('这个猪的类,可以画佩奇 Pig.pq() \n'
'还可以画乔治 ,pig,qz()') Pig.words = '哈哈哈哈哈,一个精致的猪猪女孩!'
Pig.about_me() 修改父类的方法
class Lw:
def about_me(self,name):
print('[%s] 会抽烟喝酒烫头'%name)
print('[%s] 会抽烟喝酒烫头'%name)
print('[%s] 会抽烟喝酒烫头'%name)
print('[%s] 会抽烟喝酒烫头'%name) class Xw(Lw):
def about_me(self,name,age):
super().about_me(name) #spuer指的就父类
print('age',age) def abc(self):
pass wxm = Xw()
wxm.about_me('王小明',18) 接口
import requests
import nnlog
class MyRequest:
log_file_name = 'MyRequest.log'#日子文件名
time_out = 10 #请求超时时间
def __init__(self,url,data=None,headers=None,file=None):
self.url = url
self.data = data
self.headers = headers
self.file = file
def post(self):
try:
req = requests.post(self.url,data=self.data,headers=self.headers,
files=self.file,timeout=self.time_out)
except Exception as e:
res = {"status":0,"err_msg":e.args} #0代表请求失败
else:
try:
res = {"status":1,"data":req.json()} #1代表返回的json
except Exception as e:
res = {"staus":2,"data":req.text} #2代表返回不是json
log_str = 'url: %s 请求方式:post data:%s ,返回数据:%s'%(self.url,self.data,res)
self.write_log(log_str)
return res def get(self):
try:
req = requests.get(self.url,params=self.data,headers=self.headers,timeout=self.time_out)
except Exception as e:
res = {"status":0,"err_msg":e.args} #0代表请求失败
else:
try:
res = {"status":1,"data":req.json()} #1代表返回的json except Exception as e:
res = {"staus":2,"data":req.text} #2代表返回不是json
log_str = 'url: %s get请求 data:%s ,返回数据:%s'%(self.url,self.data,res)
self.write_log(log_str)
return res @classmethod
def write_log(cls,content):
log = nnlog.Logger(cls.log_file_name)
log.debug(content) 四、继承
import threading
import time class MyThread(threading.Thread):
def run(self):
#这个方法必须叫run
time.sleep(5)
print('run..') for i in range(5):
t = MyThread()
t.start()
获取多线程运行函数的返回值
import requests
import threading all_res = []
def get_name(name):
r = requests.get('http://api.nnzhp.cn/api/user/stu_info',
params={'stu_name':name})
res = r.json()
all_res.append(res) for i in range(10):
t = threading.Thread(target=get_name,args=(i,))
t.start() while threading.active_count()!=1:
pass print(all_res) 守护线程
import threading
import time def hhh():
time.sleep(5)
print('hhhh') for i in range(100):
t = threading.Thread(target=hhh)
t.setDaemon(True) #设置子线程为守护线程
t.start() print('秦始皇死了') 锁
#coding=utf-8
import threading
num = 0
lock = threading.Lock() #申请一把锁
def xiaojun():
global num
# lock.acquire() #加锁
# num+=1
# lock.release() #解锁
# with lock: #和上面一样
# num += 1
num +=1
for i in range(1000):
t = threading.Thread(target=xiaojun)
t.start()
while threading.active_count()!=1:
pass
print(num)
#多个线程同时操作同一个数据的时候一定要加锁 多进程
from multiprocessing import Process
import multiprocessing
import time
import threading
def run_thread():
time.sleep(60)
print('%s在运行'%threading.current_thread()) def run():
for i in range(10):
t = threading.Thread(target=run_thread)
t.start()
while threading.active_count()!=1:
pass for i in range(10):
p = Process(target=run) #起进程
p.start()
p.pid 单元测试
#unittest
#junit
#phpunit
import unittest def calc(a,b):
return a+b class MyTest(unittest.TestCase):
def testa(self):
res = calc(1,2)
self.assertEqual(3,res,msg='预期结果和实际结果不一致')
def testb(self):
res = calc(0,1)
self.assertEqual(2,res,msg='预期结果和实际结果不一致') # unittest.main()



python基础(九)的更多相关文章

  1. Python基础(九) type元类

    python元类:type()    元类是python高阶语法. 合理的使用可以减少大量重复性的代码. 元类实际上做了以下三方面的工作: 干涉创建类的过程 修改类 返回修改之后的类 为什么使用元类? ...

  2. Python基础(九) 常用模块汇总

    3.8 json模块重点 json模块是将满足条件的数据结构转化成特殊的字符串,并且也可以反序列化还原回去. 不同语言都遵循的一种数据转化格式,即不同语言都使用的特殊字符串.(比如Python的一个列 ...

  3. python基础九之函数

    1,函数的定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数分为自定义函数和内置函数,内置函数就是python内部自带的一些函数,如:print().int()等.自定义函数 ...

  4. python 基础(九) 文件操作

    文件操作 一.函数: f = open(’文件名','打开方式'[,encoding='字符编码']) open 打开的方式 字符 说明 r 只读的方式打开 rb 以二进制的形式打开文件 只读 r+ ...

  5. 十九. Python基础(19)--异常

    十九. Python基础(19)--异常 1 ● 捕获异常 if VS异常处理: if是预防异常出现, 异常处理是处理异常出现 异常处理一般格式: try:     <............. ...

  6. 九. Python基础(9)--命名空间, 作用域

    九. Python基础(9)--命名空间, 作用域 1 ● !a 与 not a 注意, C/C++可以用if !a表示if a == 0, 但是Python中只能用if not a来表示同样的意义. ...

  7. Python基础学习笔记(九)常用数据类型转换函数

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...

  8. Python之路【第二篇】:Python基础

    参考链接:老师 BLOG : http://www.cnblogs.com/wupeiqi/articles/4906230.html 入门拾遗 一.作用域 只要变量在内存中就能被调用!但是(函数的栈 ...

  9. Python 基础语法(三)

    Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...

  10. Python之路【第二篇】:Python基础(一)

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 1 2 3 if 1==1:     name = 'wupeiqi' print  name 下面的结论对吗? ...

随机推荐

  1. Android View的滑动

    Android View的滑动 文章目录 Android View的滑动 一.实现移动 1.1 layout() 1.2 设置位置偏移量 1.3 改变布局参数 1.4 动画 1.5 ScrollTo以 ...

  2. Shell脚本 自动部署 SpringBoot 应用

    公司项目使用了SpringBoot.开发的应用需要自动上传到服务器.虽然目前对热部署还没完全掌握.先使用shell简化一下部署吧. # 上传密钥 sshLoginKey=/f/MyFile/root. ...

  3. 随机函数rand()与srand()

    一.int rand(void); 函数所在的头文件是stdlib.h: 其内部实现线性同除法,不是真正的随机数.通常rand()%x是指在x范围内取模,返回值0-x; 系统默认随机种子是1: 二.v ...

  4. 记录pycharm快捷键出错的其中一个原因

    #pycharm使用小技巧  最近在使用pycharm,所遇到的一些快捷键失效的问题.如ctrl+c,ctrl+v等:包括键入时,总是需要用“i”来实现等问题.  究其缘故,是在安装pycharm时, ...

  5. Git设置旧邮箱与现邮箱不一致问题

    设置名字和邮箱git config user.name 'lhr' git config user.email 'lhr@qq.com' 工程根目录创建email.sh文件粘贴以下代码 #!/bin/ ...

  6. ESP32搭建4.esp32官方程序下载与串口读取

    上一篇写到esp-idf的整体环境搭建,本文主要讲讲如何将examples中的例子烧写到ESP32的flash中,以及如何通过linux串口进行调试. 一. Esp32程序下载 1.Ctrl+Alt+ ...

  7. 成功使Linux服务端和Windows客户端建立socket通信

    一.准备工作 1.一台装有虚拟机的Windows7操作系统,虚拟机中装的是CentOS6.5版本的Linux 2.Windows7已经装有java环境 二.编码 使用java编写socket通信的服务 ...

  8. L1-060 心理阴影面积

    这是一幅心理阴影面积图.我们都以为自己可以匀速前进(图中蓝色直线),而拖延症晚期的我们往往执行的是最后时刻的疯狂赶工(图中的红色折线).由红.蓝线围出的面积,就是我们在做作业时的心理阴影面积. 现给出 ...

  9. Python中集合set()的使用及处理

    在Python中集合(set)与字典(dict)比较相似,都具有无序以及元素不能重复的特点 1.创建set 创建set需要一个list或者tuple或者dict作为输入集合 重复的元素在set中会被自 ...

  10. Oracle入门之对表内容的dml操作

    oracle表的管理--添加数据 使用insert语句向表中插入数据 插入数据基本语法: insert into table[(column [,column...])] values(value [ ...