a util collection for python developing
Project description
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)
# 字符串转 datetime
dt = converter.str2datetime(date_str)
# 字符串转 time
converter.str2time(date_str)
# 日期字符串转时间戳,结果为秒
converter.str2timestamp(date_str)
# 日期字符串转时间戳,结果为毫秒
converter.str2timestamp(date_str, millisecond=True)
# datetime 转字符串,默认格式 %Y-%m-%d %H:%M:%S
converter.datetime2str(dt)
# datetime 转字符串,指定格式
converter.datetime2str(dt, '%Y-%m-%d')
日志功能
对原生的 logging 进行了封装,使用起来更简单
from mountains import logging
from mountains.logging import StreamHandler, FileHandler, RotatingFileHandler, TimedRotatingFileHandler
# 配置日志,输出到控制台、保存到文件、日志级别、输出格式等,文件默认保存到 log.txt
logging.init_log(StreamHandler(format=logging.FORMAT_SIMPLE), FileHandler(format=logging.FORMAT_VERBOSE, level=logging.DEBUG))
# RotatingFileHandler 按文件大小分割日志文件
logging.init_log(StreamHandler(format=logging.FORMAT_SIMPLE), RotatingFileHandler(format=logging.FORMAT_VERBOSE, level=logging.DEBUG))
# TimedRotatingFileHandler 按时间分割日志文件
logging.init_log(StreamHandler(format=logging.FORMAT_SIMPLE), TimedRotatingFileHandler(format=logging.FORMAT_VERBOSE, level=logging.DEBUG))
# 使用方法与原生的 logging 一样
logger = logging.getLogger(__name__)
logger.debug('hello')
Excel 读写
from mountains.file.excel import read_excel, write_excel, edit_excel
# 读 Excel 文件
data = read_excel('filename.xlsx')
# 写新的 Excel
excel_data = [
{
'col1': '123',
'col2': '456'
},
{
'col1': '123',
'col2': '456'
},
]
headers = ['col1', 'col2']
write_excel(headers, excel_data, 'filename.xlsx')
# 编辑 Excel,打开已有的 Excel,往里面填充数据
edit_data = {
'I2': '123'
}
edit_excel('test.xlsx', sheet_index=0, data=edit_data, output_filename='new_test.xlsx')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mountains-0.8.8.tar.gz
(139.7 kB
view details)
Built Distribution
mountains-0.8.8-py2.py3-none-any.whl
(158.1 kB
view details)
File details
Details for the file mountains-0.8.8.tar.gz
.
File metadata
- Download URL: mountains-0.8.8.tar.gz
- Upload date:
- Size: 139.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad2400eda3e9f07b47c7cc69ade07aaa17049e867988b892384e723c7340ad33 |
|
MD5 | e53f0141315aaa274df171529d426122 |
|
BLAKE2b-256 | 07f059b404166be61b303a47bb5c3fa08d21d95606275373125129cef35f4101 |
File details
Details for the file mountains-0.8.8-py2.py3-none-any.whl
.
File metadata
- Download URL: mountains-0.8.8-py2.py3-none-any.whl
- Upload date:
- Size: 158.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85bf8a085f1609537e44dea97b2fde21841cac1830e69019e70c35eda277a714 |
|
MD5 | a7347719d62490f3c2442cde55431162 |
|
BLAKE2b-256 | 3956226c68149eac1425d13a3c1dc0beb35ecbfe381d5a3f0e0793fc5ff53175 |