Class
- Class为自己定义的类型;
- 通过加上括号,我们自己定义的类也可以产生该类的实例对象,每个对象拥有该类的一切特征;
- 每个实例所具有的不同的属性(实例属性)可以在初始化方法__init__(self, attribute1,attribute2,xxx) 中定义,比如同一品牌车辆的不同车架号,颜色等。self为创建的实例对象本身。
@staticmethod
的修饰, 说明这是该类的一个静态方法;- 子类的括号中要填入父类的名字,子类自动拥有父类一切属性;
- 子类中有初始化方法__init__(self,attribute1,attribute2,attribute3,xxx),一定要调用父类.__init__(self,attribute1,attribute2,xxx)
- 调用父类的方法可以用super(),这种方法调用父类初始化时不需要加self
os.system
import os
cmd = r'adb connect 172.16.250.248'
os.system(cmd)
等到os.system运行结束后才执行下一行代码
subprocess
需获取外部程序内容时用subprocess。
from subprocess import PIPE, Popen
#返回的是Popen实例对象
proc = Popen(
'fsutil volume diskfree c:',
stdin = None,
stdout = PIPE,
stterr = PIPE,
shell = True
)
#communicate 方法返回 输出到标准输出和标准错误的字节串内容
#标准输出设备和标准错误设备都是本终端设备
outinfo, errinfo = proc.communicate()
#注意返回的是byte不是str,中文windows需要用'gbk'解码, 通常用到的还有'utf8'
outinfo = outinfo.decode('utf8')
errinfo = errinfo.decode('utf8')
print(outinfo)
print('----------------------------')
print(errinfo)
outputlist = outinfo.splitlines()
free = int(outputlist[0].split(':')[1].strip())
total = int(outputlist[1].split(':')[1].strip())
if (free/total < 0.1):
print('No enough space')
else:
print('Enough space. GL&HF')
需要注意的是communicate返回的是字节串byte,不是字符串str,decode将字节串转码成字符串。
注意上述代码access denied的问题,有的用户权限不够读取c盘容量。
多线程threading
当需要等待当前线程结束后再执行后续代码,可以用thread对象的join方法。
比如后续代码需要处理子线程获取的数据,则需要thread.join()等待子线程获取完所有数据后再处理。
print('main process started')
from threading import Thread
from time import sleep
def threadFunc(arg1, arg2):
print('subprocess starts')
print(f'arguments of process are: {arg1}, {arg2}')
sleep(5)
print('subprocess end')
thread = Thread(target = threadFunc,
args = ('参数1', '参数2'))
thread.start()
thread.join()
print('main process finished')
对于共享数据,避免覆盖原始数据,使用Lock()。
bankLock = Lock()
bankLock.aquire() 第一个线程获取lock后其他的线程如果需要用到相同的数据,则需要等待bankLock.release()后才能使用。
装饰器 decorator
可以不用修改原函数来装饰一下,可以用于core不变,但是外变化的类型。
import time
#定义一个装饰器函数
def sayLocal(func):
def wrapper():
curTime = func()
return f'当地时间:{curTime}'
return wrapper
def getXXXTime():
return time.strftime('%Y_%m_%d %H:%M:%S', time.localtime())
#1种方法
#装饰getXXXTime
getXXXTime = sayLocal(getXXXTime)
#2种方法
#或者装饰getXXXTime只需要在需要加装饰器的函数前加 @+装饰器函数(@sayLocal)
print(getXXXTime())
json 序列化
目的是将存在内存中的数据对象转换成字符串,更多的应用场景是从数据库中读到的数据对象转成可供python操作的字节串。
- 如果有中文,ensure_ascii=false
import json
historyTransactions = [
{
'time' : '22222222'
'amount' : '323452'
},
]
jsonStr = json.dumps(historyTransactions, ensure_ascii=False)
print(jsonStr)
反序列化用json.loads()。
深度拷贝时可以用json序列化再反序列化,这样生成的和之前那个就不一样了。
team2 = json.loads(json.dumps(team1))
Socket 编程接口
0.0.0.0代表本机所有可用地址
127.0.0.1代表本机环回地址?
Time
import time
time.time() #-->> 1970.01.01 0点到当前时间的秒数
#通过time.time()时间差可以算出func运行的时间
from datetime import datetime
datetime.now().strftime('%Y-%m%d ** %H:%M:%S') #给定时间格式
os
os.mkdir() 可以创建目录,但上级目录没有时,则创建失败。
os.makedirs(‘path1/folder’, exist_ok = True ) 递归创建目录,如没有给定的目录,则自动创建。(True即使有已存在相同目录也不报错,直接跳过)
os.walk() 深度优先,遍历目录下所有文件
To be updated...
-----------------------------------------------------------------------------------------------------------