![](https://img-blog.csdnimg.cn/20210913173132722.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
Python高级
py3
是大嘟嘟呀
人生如梦,易蹉跎
展开
-
Python - 调用函数时检查参数的类型是否合规
函数运行时,参数类型不对,直接引发异常,有强语言那味了原创 2024-06-20 17:54:56 · 910 阅读 · 1 评论 -
Python线程安全 - 使用生产者消费者模式加消息队列
消息队列原创 2022-09-21 15:07:32 · 2752 阅读 · 0 评论 -
Python连接MySQL - 使用连接池
mysql连接池,非常好用,传授于你原创 2022-06-28 11:26:07 · 5792 阅读 · 1 评论 -
Python -- 装饰器工厂
装饰器可真香原创 2022-06-14 15:42:45 · 29403 阅读 · 3 评论 -
Python -- 将目录下的py文件全部自动设为UTF8编码
UTF8原创 2022-03-17 11:07:21 · 10310 阅读 · 0 评论 -
Python -- 线程信号量
赶紧上号PyCharm原创 2022-02-15 15:34:31 · 2967 阅读 · 0 评论 -
Python -- 函数返回值使用缓存
缓存模块walrus原创 2022-01-19 15:19:50 · 3375 阅读 · 0 评论 -
Python3自定义包
目录结构pkg文件夹为自定义的包demo.py是测试使用pkg包包中的tool.py一个get_ua方法一个get_curr_time方法包中的__init__.py这里一定要使用from .tool import get_us,get_curr_time而不是from tool import get_us,get_curr_time测试结果...原创 2021-11-26 10:36:23 · 2322 阅读 · 0 评论 -
Python -- 反编译pyc文件
前提准备cmd中安装第三方包uncompylepip install uncompyle安装成功之后,会在Python安装目录下的Scripts目录下新增一个uncompyle6.exe以pyc文件得到py文件cmd中执行反编译命令uncompyle6 main.pyc > main.py...原创 2021-12-02 17:37:34 · 2454 阅读 · 0 评论 -
Python -- 列表去重的多种实现
代码# -*- coding: utf-8 -*-# Author : charo# Time : 2021/10/27 16:42# 1、其他语言的通用逻辑def filterList1(l): _l = [] for value in l: mark = True for _value in _l: if value == _value: mark = False原创 2021-10-27 16:46:42 · 2354 阅读 · 0 评论 -
Python -- 查询MysSQL时候输出字典类型
代码import pymysqlconnect = pymysql.connect(user="root", host="localhost", database="big_data", password="Chinais9?")cursor = connect.cursor()sql = "select * from wanfang_meta limit 10"cursor.execute(sql)infos = cursor.descriptionfileds = [info[0]原创 2021-07-21 11:53:16 · 2532 阅读 · 0 评论 -
Python -- 字符串格式化的多种方式
方式一%s + % + 元组name, age = "jack", 21print("姓名: %s, 年龄: %s" % ("charo", 21))姓名: charo, 年龄: 21方式二%s + % + 字典name, age = "thomas", 22print("姓名: %(name)s, 年龄: %(age)d" % {"name": name, "age": age})姓名: thomas, 年龄: 22方式三字符串对象的format方法,顺序.原创 2021-06-22 15:12:03 · 475966 阅读 · 0 评论 -
Mongo -- python使用update_many方法
mongo集合中可以看到name字段的值存在两个c开头的代码演示使用正则查询到集合中name字段的值是c开头的数据,再把age字段的值设为100# @Time : 2021/6/21 15:59from pymongo import MongoClientdef main(): client = MongoClient() users_coll = client["data"]["users"] query = {"name": {"$reg.原创 2021-06-21 16:33:58 · 48262 阅读 · 0 评论 -
Python -- 线程安全问题
前言当多个线程同时修改同一数据时,会导致最终的数据出现的不是正确的值所以出现了线程锁,即同一时刻只允许一个线程执行操作,这样可以确保数据的准确性示例代码# @Time : 2021/6/16 9:15import threadingimport timenum = 0 # 多个线程操作的全局变量tname = lambda: threading.currentThread().name # 获取当前线程名称def task(): print(f"线程开始..原创 2021-06-16 11:29:24 · 351051 阅读 · 2 评论 -
字符串类型的标准格式时间,转换成数字类型的时间戳
代码import times = '2021-06-05 16:57:44's_timestamp = time.mktime(time.strptime(s, "%Y-%m-%d %H:%M:%S"))print(s_timestamp, type(s_timestamp))current_timestamp = time.time()print(current_timestamp, type(current_timestamp))输出1622883464.0 <class原创 2021-06-05 17:10:27 · 2175 阅读 · 0 评论 -
Python -- 生产者消费者
生产者消费者模式初体验原创 2021-04-14 13:59:09 · 52643 阅读 · 2 评论 -
Python -- 高并发,更好用的原生库concurrent
环境Python3.2+标准库concurrent.futures模块线程池ThreadPoolExecutorfrom concurrent.futures import ThreadPoolExecutor进程池ProcessPoolExecutorfrom concurrent.futures import ProcessPoolExecutor代码示例from time import sleep, timefrom threading import curren原创 2021-03-28 13:12:40 · 18399 阅读 · 0 评论 -
MySQL -- Windows10连接MySQL
1.打开 Windows PowerShell(管理员)2.启动MySql服务输入如下指令net start mysql80mysql80是我本机的服务器名称3.连接MySql打开cmd终端,输入mysql -u root -p然后输入密码就连接到MySql了原创 2021-01-30 18:52:39 · 5358 阅读 · 0 评论 -
Python连接redis的两种方式
前言------先安装redis库------pip install redis直接连接,用redis库中的Redis或者StrictRedis连接池连接,跟直接连接操作步骤相似,只是要传入pool参数代码第一种from redis import Redis''' 一般连接,使用Redis连接'''# 这里使用Redisdb = Redis(host="localhost", port=6379, db=0, decode_responses=True)# 操作结原创 2020-07-01 17:41:15 · 4695 阅读 · 0 评论 -
增强版冒泡排序
前言假设传入的列表为[4,3,2,1,40 ,50 ,60 ],列表后面的三个元素根本不需要排序再假设传入的列表为[1,2,3,4,5,6,7],此列表本身就是有序列表冒泡排序普通写法def sort_list(list1): for i in range(len(list1) - 1): for j in range(len(list1) - 1 - i): if list1[j] > list1[j + 1]:原创 2020-06-03 14:38:47 · 3904 阅读 · 0 评论 -
python搜索文件夹所有文件及其子文件
def dir(path, n=1): if not os.path.isdir(path): print('错误:{}'.format("路径非文件夹 "), path, ) return try: global count for file in os.listdir(path): f =...原创 2019-11-16 17:52:37 · 4543 阅读 · 0 评论 -
property修饰私有属性
class X: def __init__(self): self.__money = None # 设置私有属性默认值为None @property # 使用了property,使得 对象.money 不需要加括号就能获取返回值 def money(self): return self.__money # 返回私有属性 ...原创 2019-05-08 17:11:00 · 3684 阅读 · 0 评论 -
装饰器
代码def func_out(func): print("开始执行") def fun_in(*args, **kwargs): print('hello world') result = func(*args, **kwargs) print('hello python') return result re...原创 2019-05-20 19:43:03 · 3555 阅读 · 0 评论 -
匿名函数高级用法
from functools import reduce # reduce在python3被移动到了functoolslist1 = [1, 2, 3, 4, 5, 6, 7, 8]# 匿名函数作用于list1中的元素,将每一个元素+1result1 = list(map(lambda x: x + 1, list1)) print(result1) # [2, 3, 4, ...原创 2019-05-13 16:19:25 · 3667 阅读 · 0 评论 -
namedtuple
概述创建一个类不使用class关键字,使用使用了namedtuple也可以创建代码from collections import namedtuple# 传入的"Person"即类名称,["name", "age"]即成员变量Person = namedtuple("Person", ["name", "age"])# 创建对象person = Person("Mark", "a...原创 2019-06-02 21:51:47 · 3581 阅读 · 0 评论 -
python冒泡排序讲解
分析[9, 2, 3, 5, 1, 2, 8] 列表长度为7假设是9(即索引0),则最多交换 (7-1)-0 = 6 次假设是2(即索引1),则最多交换 (7-1)-1 = 5 次推理:假设索引为index,则最多交换 (列表长度-1-index) 次代码def list_sort(array): count = 0 ...原创 2019-06-19 20:55:24 · 4477 阅读 · 0 评论 -
Python中的单例模式
什么是单例模式简单来说,单例模式就是在一个类当中只能存在一个对象。你当然可以在代码中多次创建单例类的对象,但是无论你创建多少个对象,这个对象都是同一个,即它们的地址值是一样的。先看一下非单例类class A: passif __name__ == '__main__': a1 = A() a2 = A() print(id(a1), i...原创 2019-09-02 20:20:12 · 3810 阅读 · 0 评论 -
斐波那契生成器
def fbnq(n): a, b, c = 0, 1, 0 while c < n: yield a a, b = b, a + b c += 1for value in fbnq(10): print(value, end=' ')原创 2019-05-08 16:58:46 · 4033 阅读 · 0 评论