bigtools
English | 中文
bigtools 是一个 Python 工具箱,提供实用的函数、类和一批常用预设变量,方便在项目中直接调用,减少重复代码。
功能特性
- 文件读写
- JSON 与 YAML 处理
- Hash 生成
- 路径处理
- 日志与打印工具
- 请求 headers 与 Content-Type 常用变量
- 中文分词、关键词提取与文本处理
- TF-IDF 与 BM25 相似度检索
- MongoDB、Redis、Elasticsearch 等数据库工具
- 异步文件、JSON、文本处理和数据库操作
文档
完整的 API 文档请查看 api.md。
环境要求
Python >= 3.9
安装
pip install bigtools
使用
导入方式
每个函数、类、变量均支持两种导入方式,可根据实际需求选择:
# 方式一:简单直接(推荐)
from bigtools import generate_hash_value
# 方式二:包路径清晰
from bigtools.hash_tools import generate_hash_value
使用函数或类
示例:生成 Hash 值
from bigtools import generate_hash_value
# 或
# from bigtools.hash_tools import generate_hash_value
# 默认使用 md5,可选 sha1、sha256 等
hash_value = generate_hash_value('python工具箱')
print(hash_value)
示例:文件操作
from bigtools import save_file, load_file
# 保存文件
save_file('example.txt', '文件内容')
# 读取文件
content = load_file('example.txt')
print(content)
示例:JSON 操作
from bigtools import save_json_data, load_json_data
# 保存 JSON 数据
data = {'name': 'bigtools', 'version': '1.4.6'}
save_json_data(data, 'data.json')
# 读取 JSON 数据
loaded_data = load_json_data('data.json')
print(loaded_data)
使用变量
使用 headers
from bigtools import headers
# 或
# from bigtools.default_data import headers
import requests
url = 'https://example.com'
response = requests.get(url=url, headers=headers)
print(response)
使用 ContentType
以大写字母开头的名称通常是类,类中包含已赋值的变量,可用 . 来访问变量。
from bigtools import ContentType
# 或
# from bigtools.default_data import ContentType
# 发送 POST 请求
import requests
url = 'https://example.com/api'
data = {'key': 'value'}
response = requests.post(
url=url,
data=data,
headers=ContentType.app_json_headers
)
print(response)
异步使用
bigtools 提供了丰富的异步函数,适用于异步编程场景。
异步文件操作
import asyncio
from bigtools import save_file_async, load_file_async
async def main():
# 异步保存文件
await save_file_async('example.txt', '文件内容')
# 异步读取文件
content = await load_file_async('example.txt')
print(content)
asyncio.run(main())
异步 JSON 操作
import asyncio
from bigtools import save_json_data_async, load_json_data_async
async def main():
# 异步保存 JSON 数据
data = {'name': 'bigtools', 'version': '1.4.6'}
await save_json_data_async(data, 'data.json')
# 异步读取 JSON 数据
loaded_data = await load_json_data_async('data.json')
print(loaded_data)
asyncio.run(main())
异步文本处理
import asyncio
from bigtools import get_keywords_from_text_async
async def main():
text = '这是一个用于测试的文本'
keywords = await get_keywords_from_text_async(text)
print(keywords)
asyncio.run(main())
异步相似度计算
import asyncio
from bigtools import TfidfChineseRetrieverAsync, calculate_chinese_tfidf_similarity_async
async def main():
# 创建异步 TF-IDF 检索器
documents = ['文档1的内容', '文档2的内容', '文档3的内容']
retriever = await TfidfChineseRetrieverAsync.create(documents)
# 计算相似度
query = '查询内容'
similarities = await retriever.query_topk(query, top_k=2)
print(similarities)
asyncio.run(main())
异步数据库操作
import asyncio
from bigtools import async_mongo_client, AsyncElasticsearchClient
async def main():
# 异步 MongoDB 客户端
mongo = async_mongo_client(host='localhost', port=27017)
db = mongo['mydb']
collection = db['mycollection']
# 异步 Elasticsearch 客户端
es = AsyncElasticsearchClient(hosts=['localhost:9200'])
# 使用 es 进行异步操作...
asyncio.run(main())
许可证
Apache-2.0
Metadata
Release files for bigtools 1.4.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bigtools-1.4.6.tar.gz | 65.1 kB | Without provenance | Details for bigtools-1.4.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bigtools-1.4.6-py3-none-any.whl | Python 3 | none | any | Details for bigtools-1.4.6-py3-none-any.whl |
Total release size: 124.3 kB
Release files / bigtools-1.4.6.tar.gz
| Download URL | bigtools-1.4.6.tar.gz |
|---|---|
| Size | 65.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
30820df167e4a428432761de31450d99a201925c19b62a145b797ced9b68a5d9
|
|
BLAKE2b-256 checksum How to use checksums |
d221705413ca6530fa820b68a9176e2f41decf19d5dd3be29b7c22ab01417bc7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|
Release files / bigtools-1.4.6-py3-none-any.whl
| Download URL | bigtools-1.4.6-py3-none-any.whl |
|---|---|
| Size | 59.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
069d588dcc30bad56300ddf0f5fce5d7da8d26eb04c6e0381f9cdfa4f6f0a563
|
|
BLAKE2b-256 checksum How to use checksums |
85d2ae598394dab19335183b2dd52a512a906c037374ef01ff8b90d89df8c43d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|
Security for bigtools 1.4.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.