Tools for python
Project description
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
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
bigtools-1.4.6.tar.gz
(65.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
bigtools-1.4.6-py3-none-any.whl
(59.2 kB
view details)
File details
Details for the file bigtools-1.4.6.tar.gz.
File metadata
- Download URL: bigtools-1.4.6.tar.gz
- Upload date:
- Size: 65.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30820df167e4a428432761de31450d99a201925c19b62a145b797ced9b68a5d9
|
|
| MD5 |
4dcce15f3b0b6004060a33cacde92b14
|
|
| BLAKE2b-256 |
d221705413ca6530fa820b68a9176e2f41decf19d5dd3be29b7c22ab01417bc7
|
File details
Details for the file bigtools-1.4.6-py3-none-any.whl.
File metadata
- Download URL: bigtools-1.4.6-py3-none-any.whl
- Upload date:
- Size: 59.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
069d588dcc30bad56300ddf0f5fce5d7da8d26eb04c6e0381f9cdfa4f6f0a563
|
|
| MD5 |
c4bbefe619418271c73f4f963e633e49
|
|
| BLAKE2b-256 |
85d2ae598394dab19335183b2dd52a512a906c037374ef01ff8b90d89df8c43d
|