看了别人的写的代码, 学习到了几个比较舒服的代码风格
自定义Log
替换print使用自定义log()
def log(*args, **kwargs):
'''替代print'''
print('log', *args, **kwargs)
log()
写入到本地log
import time
def log(*args, **kwargs):
format = '%Y/%m/%d %H:%M:%S'
value = time.localtime(int(time.time()))
dt = time.strftime(format, value)
with open('log.txt', 'a', encoding='utf-8') as f:
print(dt, *args, file=f, **kwargs)
字典函数
函数存到字典里, 方便以后调用
func_list = {
'func1' = func1(),
'func2' = func2(),
}
func_list.get('func1')
字典传参
if __name__ == '__main__':
config = dict(
debug=True,
host='127.0.0.1',
port=8000, # 这样写参数方便注释
)
app.run(**config)
多模块导入
from flask import (
Flask, request,
render_template,
redirect,
)
JavaScript自定义日志
var log = function(){
console.log.apply(console, arguments)
}
log('代码')
网友评论