随笔分类 - Python进阶
摘要:list derivatives & dict deconstruction ebb = dict(zip('abcde', (11, 22, 11, 33, 22))) print(ebb) print([k for k, v in ebb.items() if v == 11]) filter
阅读全文
摘要:lru_cache import functools, time @functools.lru_cache(maxsize=128, typed=False) def b(x, y, z=3): time.sleep(2) return x + y print(b(5, 6)) print('~'
阅读全文
摘要:import inspect def b(x, y: int = 5, *args, z, t=55, **kwargs) -> int: return x + y signature = inspect.signature(b) print(signature) print(signature.p
阅读全文
摘要:打印Full binary tree: import math def reveal(vail: list | tuple): """ 利用Python居中打印 :param vail: :return: """ length = len(vail) height = math.ceil(math.
阅读全文
摘要:import datetime, multiprocessing, logging, time, threading from concurrent import futures FORMAT = '%(asctime)-9s [%(processName)-10s %(process)6d] %(
阅读全文
摘要:import threading, time, logging, random, datetime, multiprocessing FORMAT = '%(asctime)-15s %(process)d %(lineno)-3s [%(threadName)-11s %(thread)6d] %
阅读全文
摘要:import time, logging, typing, threading, random, datetime FORMAT = '{asctime} {levelname} {process} {processName} {threadName} {thread} {lineno}> {mes
阅读全文
摘要:import time, logging, typing, threading, random, datetime FORMAT = '{asctime} {levelname} {process} {processName} {threadName} {thread} {message}' log
阅读全文
摘要:消费速度 > 生产速度 import threading, time, logging, random FORMAT = '%(asctime)-15s [%(threadName)-11s %(thread)6d] %(message)s' logging.basicConfig(format=F
阅读全文
摘要:import threading, logging logging.basicConfig(format='%(asctime)-15s [%(threadName)s %(thread)d] %(message)s', level=logging.INFO, datefmt='%F %T') de
阅读全文
摘要:从上述报错,可知int类参数必须是 str, bytes, bytearray, int, float 五种类型 int() 可接受两个参数 base默认是10进制 无参 -> zero 第一参数为 number类型 TypeError 不能带明确的base, 即base就是默认10进制 可将flo
阅读全文
摘要:10 二进制前面 加 - 而已 由于Python number没有范围限制, 但是负数一定是补码表示, 正数前面有无限0, 负数前面有无限1 5 ==> (0)(infinite) 0101 -5 ==> (1)(infinite) 1011 以 四字节 为例, 将 -10 => 补码, 采用 &
阅读全文
摘要:下个月的同天 def same_day_of_next_month(obj: datetime.date): def last_day_of_current_month(_obj: datetime.date): if _obj.month == 12: first_day_of_next_mont
阅读全文
摘要:"," 每三位分割 '{:,}'.format(88888888888888) for align,text in zip('<^>',['left','right','right']): print('{0:{fill}{align}16}'.format(text,fill=align,alig
阅读全文
摘要:Python内置的json模块提供了非常完善的Python对象到JSON格式的转换。 json.dumps() 将Python中的对象转换为JSON中的字符串对象json.loads() 将JSON中的字符串对象转换为Python中的对象 通过一种简单的方式,用lambda方式来转换任意一个类对象为
阅读全文
摘要:class B: def __setattr__(self,key,value): pass raise NotImplementedError 类方法__setattr__覆盖父类方法后,如果为pass, raise NotImplementedError之类的,则实例不能设置任何属性 pass不
阅读全文
摘要:判断list包含关系 a = [1, 2, 3, 4, 5] b = [3, 4, 5, 3] if [v for v in b if v not in a]: print('a doesn\'t comprise b') else: print('a comprise b') 采用列表解析式 a
阅读全文
摘要:内层函数没有引用外层函数局部变量,dir()是不会显示的 def b(a): def p(): print('function p dir ->',dir()) p() print('function b dir ->',dir()) b(2) def b(a): def p(): nonlocal
阅读全文