随笔分类 - Python
摘要:data = '[{"foo": "bar", "foo": "baz", "b": 99}]' json.loads(data, object_hook=print) json.loads(data, object_pairs_hook=print)
阅读全文
摘要:import asyncio import sys class AsyncContextManager: async def __aenter__(self): return self async def __aexit__(self, exc_type, exc_val, exc_tb): pri
阅读全文
摘要:connection import psycopg2 from psycopg2 import Error, connection, cursor conn: connection | None = None c1: cursor | None = None try: conn = psycopg2
阅读全文
摘要:Pool 使用pool正确方法: 从pool取connection,使用完close(), 底层CMySQLConnection会return到deque, 此时从pool中得到的PooledMySQLConnection底层_cnx变为None
阅读全文
摘要:contra-variance from typing import Callable class Food: ... class Meat(Food): ... class Animal: def eat(self, food: Food): ... class Dog(Animal): def
阅读全文
摘要:keyword must be strings
阅读全文
摘要:def get_public_ip(): from urllib.request import urlopen import re as r d = urlopen('http://checkip.dyndns.com').read() return r.compile(r'(\d+\.\d+\.\
阅读全文
摘要:'import *' only allowed at module level When Python compiles code it needs a determined number of local variables, The number of variables that import
阅读全文
摘要:method str.format_map(self, mapping), 直接传mapping, 可overwrite __missing__
阅读全文
摘要:decimal_to_binary def decimal_to_binary(v: float, precison: int = 6) -> str: if v == 0: return '0b0' binary = '' integral = int(v) # integral part man
阅读全文
摘要:正常情况fromisoformat都能处理 astimezone(self,tz=None) convert to aware datetime use replace(miscrosecond=0)
阅读全文
摘要:raise from try: try: 1 / 0 except ZeroDivisionError as e: raise Exception("with_traceback") from e except Exception as e: print(e, e.__cause__, type(e
阅读全文
摘要:s1 = '[{(b)}][[]]' s2 = '[{(b}][[]]' def brackets_match(s: str) -> bool: opening_brackets = '([{' closing_brackets = ')]}' nexus = {')': '(', ']': '['
阅读全文
摘要:A stack is a linear data structure that stores items in a last-in-first-out LIFO or first-in-last-out FILO manner, In stack, a new element is added at
阅读全文
摘要:判断一个整数是否为完全立方数 cubic number: import math cubical = int(input('number: ')) def is_cubical(cubical: int): n = math.ceil(pow(cubical.__abs__(), 1 / 3)) #
阅读全文
摘要:import typing class Node: def __init__(self, value): self.value = value self.prev = None self.next = None def __repr__(self): return f'Node({self.valu
阅读全文
摘要:import os, sys from stat import * from typing import Callable def visit_file(file): print('visiting', file) def walktree(top, callback: Callable): pri
阅读全文
摘要:def _convert_mode(mode: int): if not 0 <= mode <= 0o777: raise RuntimeError res = '' for v in range(0, 9): if mode >> v & 1: match v % 3: case 0: res
阅读全文
摘要:https://phrase.com/blog/posts/beginners-guide-to-locale-in-python/
阅读全文