Skip to main content

Tools for python

Project description

bigtools

python 工具箱,实现了一些实用的函数、类和赋值了一批常用变量,方便调用,减少工程代码量。

Documentation

完整的 API 文档请查看 api.md

Installation

pip install bigtools

Usage

导入方式

每个函数、类、变量均支持两种导入方式,可根据实际需求选择:

# 方式一:简单直接(推荐)
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.2'}
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

大写字母开头的是类(type is class),类里包含已赋值的变量,可用 . 来访问变量。

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)

Async usage

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.2'}
    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.search(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())

Project details


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.4.tar.gz (63.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bigtools-1.4.4-py3-none-any.whl (58.0 kB view details)

Uploaded Python 3

File details

Details for the file bigtools-1.4.4.tar.gz.

File metadata

  • Download URL: bigtools-1.4.4.tar.gz
  • Upload date:
  • Size: 63.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for bigtools-1.4.4.tar.gz
Algorithm Hash digest
SHA256 f5ef3e5d86f5f60b30bcabf67b10dd800fa081d6de545c884cbc205f6283b2b2
MD5 1765aba85ffb3b24653e737acec7e5d1
BLAKE2b-256 b2f7c556a1f61ce2d87c8f7c25fc587bc5c1d63b1f277547794c49adb87d8855

See more details on using hashes here.

File details

Details for the file bigtools-1.4.4-py3-none-any.whl.

File metadata

  • Download URL: bigtools-1.4.4-py3-none-any.whl
  • Upload date:
  • Size: 58.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for bigtools-1.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b69fe1045fd35f45a50b058f7c1c9215d038c366e5e3bdee83ecf87f6b1ab9c7
MD5 dac26e9aff99ad116c015f73d30a50bb
BLAKE2b-256 6aec469653b7deb80826ad9884c217c7a40733ca6daf72abc4f0a3b1b9de5048

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page