Python

python的GIL

 GIL是python的全局解释器锁,同一进程中假如有多个线程运行,一个线程在运行python程序的时候会霸占python解释器(加了一把锁即GIL),使该进程内的其他线程无法运行,等该线程运行完后其他线程才能运行。如果线程运行过程中遇到耗时操作,则解释器锁解开,使其他线程运行。所以在多线程中,线程的运行仍是...
代码星球·2020-08-09

python实现列表去重的方法

 >>>l=[1,2,3,4,2,2,2]>>>list(set(l))[1,2,3,4]>>> ...

python 警惕 IEEE 754标准

  双精度浮点数格式,即IEEE754标准>>>0.1+0.20.30000000000000004>>>(0.1+0.2)==0.3False>>> ...
代码星球·2020-08-09

python 获取subprocess进程执行后返回值

 test.py#coding=utf-8importsubprocesscompilePopen=subprocess.Popen('gcchaha',shell=True,stderr=subprocess.PIPE)compilePopen.wait()print('thestatuscodeis:',...

python 字符串与16进制 转化

 defstr_to_hex(s):returnr"/x"+r'/x'.join([hex(ord(c)).replace('0x','')forcins])defhex_to_str(s):return''.join([chr(i)foriin[int(b,16)forbins.split(r'/x')[1...

实现Python与STM32通信

 断断续续学了几周Stm32后,突然想实现上位机和下位机的通信,恰好自己学过一点python,便想通过python实现通信.在网上看见python库pyserial可以实现此功能,便去官网找了一下,附上官网pyserial档链接:https://pyserial.readthedocs.io/en/lates...
代码星球·2020-08-09

python 简单的串口收发数据

 #-*-coding:utf-8-*-importserial#打开串口serialPort="COM3"#串口baudRate=9600#波特率ser=serial.Serial(serialPort,baudRate,timeout=0.5)print("参数设置:串口={{}},波特率={{}}".f...

python 自定义异常

 definput_password():#1.提示用户输入密码pwd=input("请输入密码:")#2.判断密码长度,如果长度>=8,返回用户输入的密码iflen(pwd)>=8:returnpwd#3.密码长度不够,需要抛出异常#1>创建异常对象-使用异常的错误信息字符串作为参数ex=...
代码星球·2020-08-09

python 获取文件目录位置

 test.pyimportosprint('***获取当前目录***')print(os.getcwd())print(os.path.abspath(os.path.dirname(__file__)))print('***获取上级目录***')print(os.path.abspath(os.path....

python 简单的自定义异常类模版

 classCustomError(Exception):def__init__(self,ErrorInfo):super().__init__(self)#初始化父类self.errorinfo=ErrorInfodef__str__(self):returnself.errorinfoif__name_...

python获取当前文件路径以及父文件路径

 #当前文件的路径pwd=os.getcwd()#当前文件的父路径father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")#当前文件的前两级目录grader_father=os.path.abspath(os.path.dirname(...

python tar 打包

 importosimporttarfiledefmake_targz_one_by_one(output_filename,source_dir):tar=tarfile.open(output_filename,"w:gz")forroot,dir,filesinos.walk(source_dir):f...
代码星球·2020-08-09

python requests 上传文件

 token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiZWM2ZWFjZjFjM2UzYzEyOWU3ODA4YjgwNzkxNGIzZjMiLCJleHAiOjE1NTU0OTE3MTIsImlhdCI6MTU1NDg4NjkxMn0.L_Xle5...

python 排序 由大到小

 importfunctoolsclassSolution:#@param{integer[]}nums#@return{string}deflargestNumber(self,nums):defcomparator(x,y):#inputsarestringrepresentationsofnon-neg...
代码星球·2020-08-09

python 定时器

 2s启动一个定时器:importthreadingimporttimedefhello(name):print"hello%s"%nameglobaltimertimer=threading.Timer(2.0,hello,["Hawk"])timer.start()if__name__=="__main_...
代码星球·2020-08-09