flask orm 对象序列化
12345678def orm_serialize(item) -> dict: if item: try: return {c.name: getattr(item, c.name) for c in item.__table__.columns} except: return {} else: return {}
es6完全深拷贝一个对象
123export const deepClone = (obj: object) => { return JSON.parse(JSON.stringify(obj));}
编辑距离算法
1234567891011121314151617181920212223242526272829303132333435const levenshteinDistance = (str1: string, str2: string) => { const len1 = str1.length; const len2 = str2.length; const matrix: Array<number[]> = []; // 初始化第一行 for (let i = 0; i <= len1; i++) { matrix[i] = [i]; } // 初始化第一列 for (let j = 0; j <= len2; j++) { matrix[0][j] = j; } // 填充矩阵 for (let i = 1; i <= len1; i++) { for (let j = 1; j ...
es6过滤对象的某个属性
使用 Object.entries() 方法将对象转换成一个键值对的数组,然后使用 reduce() 方法遍历这个数组并过滤掉不需要的属性。最后,使用一个空对象作为累加器(acc),将过滤后的属性存储到这个对象中。
123456789const fields = { name: "xxx", age: 18, other: null}const filteredFields = Object.entries(fields).reduce((acc, [key, value]) => { if (key && value) { acc[key] = value; } return acc;}, {});console.log(filteredFields)
flask-sqlalchemy orm 框架序列化
123# 如果对象是ORM对象,则将其转换为字典并返回if isinstance(obj.__class__, DeclarativeMeta): return {c.name: getattr(obj, c.name) for c in obj.__table__.columns}
celery flower api 接口认证
12345678auth_user = 'admin'auth_passwd = '123456'usrPass = "%s:%s" % (auth_user, auth_passwd)b64Val = base64.b64encode(usrPass.encode('utf-8'))headers = { "Authorization": "Basic %s" % b64Val.decode(),}print(headers)
git回滚
版本回退1git reset --hard <目标版本号>
强制推送1git push -f
统计80端口连接数
1netstat -nat|grep -i "80"|wc -l
Python 中动态调用函数或类的方法
使用 importlib123456789101112131415161718# module.pyclass A: def foo(self): print('this is foo.') @staticmethod def static_method(): print('this is static.')def bar(): print('bar……')def baz(): print('==baz==')
这种方式其实是__import__() 方式的扩展。Python官方文档推荐程序式地导入模块时应该使用 import_module() 而不是__import__。这里我们继续使用上面定义好的module.py模块。
1234567891011# main.pyimport importlibmodule_name = 'module'module_obj = importlib.import_module(module ...
SQLAlchemy commit 后会自动重新查询数据库问题
1expire_on_commit=False