mountains
在开发Python的过程中经常会有一些常用的方法和工具类,因此将这些代码集成在一起,在开发新东西的时候就能直接调用,加速开发。
安装
pip install mountains
功能
- Python 2-3 兼容,大部分代码都尽可能做了兼容
- 日期转换,各种日期、字符串、时间戳直接的转换
- SSHClient
- Tornado 的异步请求
- Random HTTP User Agent
- 文件、Excel、json 读写
- ...
日期转换
datetime、time、时间戳、日期字符串之间的转换
import time
from datetime import datetime
from mountains.datetime import converter
date_str = '2016-10-30 12:30:30'
dt = datetime(year=2016, month=10, day=30, hour=12, minute=30, second=30)
t = dt.timetuple()
ts = int(time.mktime(t))
ts_ms = int(time.mktime(t) * 1000)
dt = converter.str2datetime(date_str)
converter.str2time(date_str)
converter.str2timestamp(date_str)
converter.str2timestamp(date_str, millisecond=True)
converter.datetime2str(dt)
converter.datetime2str(dt, '%Y-%m-%d')
日志功能
对原生的 logging 进行了封装,使用起来更简单
from mountains import logging
from mountains.logging import StreamHandler, FileHandler, RotatingFileHandler, TimedRotatingFileHandler
logging.init_log(StreamHandler(format=logging.FORMAT_SIMPLE), FileHandler(format=logging.FORMAT_VERBOSE, level=logging.DEBUG))
logging.init_log(StreamHandler(format=logging.FORMAT_SIMPLE), RotatingFileHandler(format=logging.FORMAT_VERBOSE, level=logging.DEBUG))
logging.init_log(StreamHandler(format=logging.FORMAT_SIMPLE), TimedRotatingFileHandler(format=logging.FORMAT_VERBOSE, level=logging.DEBUG))
logger = logging.getLogger(__name__)
logger.debug('hello')
Excel 读写
from mountains.file.excel import read_excel, write_excel, edit_excel
data = read_excel('filename.xlsx')
excel_data = [
{
'col1': '123',
'col2': '456'
},
{
'col1': '123',
'col2': '456'
},
]
headers = ['col1', 'col2']
write_excel(headers, excel_data, 'filename.xlsx')
edit_data = {
'I2': '123'
}
edit_excel('test.xlsx', sheet_index=0, data=edit_data, output_filename='new_test.xlsx')