随笔分类 - python
摘要:from PIL import Image, ImageDraw, ImageFont, ImageColor import random DEFAULT_FONT_PATH = 'c:\\windows\\fonts\\arial.ttf' def generate_verify_code(len, width, height,font_path=None, font_size=32, bg_c
阅读全文
摘要:def t(*arg, **kwargs): def tt(*arg, **kwargs): print(arg, kwargs) tt(*arg, **kwargs) t('a', 'b', c=1, d=2) # 形参中*args, **kwargs 的作用? #args, kwargs 支持了
阅读全文
摘要:流程图: 最近在看flask源码学习下flask工作原理,然后就尝试着画了个流程图,如上图所示,部分功能细节可能没有写上去,有什么遗漏的地方欢迎大家补充。 WSGI: 全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是
阅读全文
摘要:# 定义 class T: a = 123 #类属性 def func(self): #类方法 pass @classmethod def classfunc(cls): pass @staticmethod def staticfunc(): ...
阅读全文
摘要:import logging from logging import FileHandler, Formatter import os.path as fpath from datetime import datetime logfile = fpath.join(fpath.dirname(fpath.abspath(__file__)),datetime.now().strftime('%Y...
阅读全文
摘要:import heapq #用heapq 支持自定义对象 #heapq可以用默认list类型来创建堆结构, #用[]来创建堆结构不建议给初始值,如果非得给的话建议先调用heapq.heapify进行转化 import random a = [] for i in range(10): heapq.heappush(a, random.randint(1, 10)) ...
阅读全文
摘要:##python 中特殊方法简写方式 class Test: __call__ = lambda *args: args[1] * 2 #这里需要注意lambda的参数 会默认将实例self 加进去 __str__ = lambda self: 'that`s useful...%s' % self.__class__.__name__ t = Test() prin...
阅读全文
摘要:import sys default_path = r'C:\Users\lon\Desktop' #自定义路径 sys.path.append(default_path) #将自定义路径加到sys模块path路径中 print(sys.path) from test1 import Demo d = Demo() print(d.Hi())
阅读全文
摘要:# python 中的闭包 n = 10 #定义全局作用域变量 def fn(): #形成闭包 n = 100 #定义局部变量n def inner(): nonlocal n n += 1 #这里定义操作相同变量n无法调用上层作用中的变量,如果只读不写则可以正常访问 # python3 中新增nonlocal...
阅读全文
摘要:#python中通过实现这些特殊方法从而实现一些‘特定的功能’,这些方法不是给用户用的,#python解释器再给类初始化时会同时初始化这些方法class Parent: pass class Test: __slots__ = [] #属性控制列表 def __init__(self, **kwargs): for k, v in kwargs.item...
阅读全文
摘要:class Test: _parent1 = 'variable parent' #父类成员变量首先被初始化 print(_parent1) _son_son = 'search parent' def __init__(self): print(self) #实际对应的实例化对象这里为 Test2 的实例 print('pare...
阅读全文
摘要:import logging #创建日志文件 file = './test123.log' logger = logging.getLogger('test') #设置日志等级 logger.setLevel(logging.INFO) #添加文件输出流 filehanle = logging.FileHandler(file, mode='a') #设置日志输出格式 formatter ...
阅读全文
摘要:from optparse import OptionParser parser = OptionParser() parser.add_option('-f', '--file', dest='filename', help='test') # action 指定目的dest 存储的默认值 有时候参数不是必须的就可以设置action类型的默认值例如 true, false等 #常用的有 st...
阅读全文
摘要:def handle(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: print('sfdsd') return wrapper @handle #捕获异常,后面...
阅读全文
摘要:import asyncio, threading import aiohttp class Tasks(): def __init__(self, max_async, loop=None): self.loop = loop or asyncio.get_event_loop() self._queue = asyncio.Queue(maxs...
阅读全文
摘要:# _*_coding:utf-8_*_ # author:leo # date: # email:alplf123@163.com import queue, threading class Worker(threading.Thread): def __init__(self): threading.Thread.__init__(self) s...
阅读全文
摘要:#装饰器小技巧一信号量控制 import threading, time NUM = 10 # 定义信号量控制 def synchronized(func): func.__sem__ = threading.Semaphore(value=NUM) print(func.__sem__) def wraper(*args, **kwargs): #支持...
阅读全文
摘要:import xml.etree.ElementTree as tree import io xml = ''' 2 2008 141100 5 2011 59900 69 ...
阅读全文
摘要:def parent(): t = son() print(type(t)) result = t.send(None) # 开始迭代 print(result) result = t.send(20) print(result) result = t.send(None) #引发StopIteration def son(): n...
阅读全文
摘要:from collections import deque #deque 双端队列 线程安全 可以代替list 更高效 d = deque('abc') #初始化一个队列 for v in d: print(v.upper()) d.append(['1', '2', '3']) #追加 print(d) d.appendleft('1')#左追加 d.extend('123')#...
阅读全文

浙公网安备 33010602011771号