SimpleDao
使用文件锁,支持linux和windowsimport osimport sysimport timeimport logginglogger = loggingclass SingleInstance: def __init__(self): py_file_path = os.path.abspath(__file__) basepath = os.path.dirname(py_file_path) self.lockfile = os.path.normpath(basepath + '/' + os.path.basename(_...
Python设置日志翻滚
2019-05-06, 访问数: 1395
可翻滚的日志: logger.py# -*- coding: utf-8 -*-import osimport loggingfrom logging.handlers import RotatingFileHandlerdef get_full_path(local_path): file_path = os.path.abspath(__file__) base_path = os.path.dirname(file_path) return os.path.join(base_path, local_path)LOG_LEVEL = logging.DEBUGLO...
Python 时间格式转换
2019-05-06, 访问数: 1077
datetime和str# str转datetimedatetime.datetime.strptime('2017-04-23 00:00:00', '%Y-%m-%d %H:%M:%S')# datetime转strdatetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S');# datetime转timestamptime.mktime(dt.timetuple()) utc时间转本地时间from_zone = tz.tzutc()to_zone = tz.tzlocal()utc = datetime.strptime(utcs, '...
Python使用gdb调试
2019-05-06, 访问数: 1384
调试运行进程gdb -p pid 增加libpython来获取更多调试信息 a)下载python2.7的源码中的Tools/gdb/libpython.pyb)引入libpython.py# 在gdb -p pid之后运行(gdb) python>import sys>sys.path.append('/path/to/libpython.py')>import libpython>end(gdb) c)之后可使用命令py-bt py-bt-full py-down py-list py-locals py-print py-up 注:python2.6的源码中提...
Python性能调试profile
2019-05-06, 访问数: 1259
工具 cProfile:基于lsprof的用C语言实现的扩展应用,运行开销比较合理,适合分析运行时间较长的程序profile:纯Python实现的性能分析模块,接口和cProfile一致。但在分析程序时增加了很大的运行开销。不过,如果想扩展profiler的功能,可以通过继承这个模块实现 基本用法python -m cProfile -s cumulative test.py 结果说明 ncalls:表示函数调用的次数tottime:表示指定函数的总的运行时间,除掉函数中调用子函数的运行时间percall:(第一个percall)等于 tottime/ncallscumtime:表示该函...
Python 序列化json 包含中文
2019-05-06, 访问数: 912
序列化json包含中文html_dict = {'key': 'value'}json_str = json.dumps(html_dict, ensure_ascii=False, encoding='utf-8')
压缩def gzip_compress(buf, compresslevel=6): out = StringIO.StringIO() f = gzip.GzipFile(fileobj=out, mode="w", compresslevel=compresslevel) f.write(buf) f.close() res = out.getvalue() return res 解压缩def gzip_decompress(buf): obj = StringIO.StringIO(buf) f = gzip.GzipFile(fil...
Python执行shell命令
2019-05-06, 访问数: 984
popendef _command_execute(command_s): """执行命令,例如command_s=['ls', '-al']""" sp = Popen(command_s, stdout=PIPE, stderr=STDOUT, close_fds=True) output_content = '' for line in iter(sp.stdout.readline, b''): output_content += line if len(output_content) > 1024 * 4: ...
安装扩展包Virtaulenvwrapper是virtualenv的扩展包,用于更方便管理虚拟环境 pip install virtualenvwrapper 设置环境在~/.bashrc中添加有一些是在 /usr/bin/virtualenvwrapper.sh source /usr/local/bin/virtualenvwrapper.sh 设置完后要运行一次source /usr/local/bin/virtualenvwrapper.sh 常用命令# 创建一个tmp的环境mkvirtualenv tmp# 切换到tmp的环境workon tmp# 退出环境deactivate...