Skip to main content

Python Better Commons(better-commons)

Python版本的基础库,方便开发复用.

基础功能

仿照Hadoop格式的XML配置

配置文件的格式示例:

<configurations>
    <!--  配置模板  -->
    <property>
        <name>api.server.host</name>
        <value>0.0.0.0</value>
        <description>API服务器地址</description>
    </property>

    <property>
        <name>api.server.port</name>
        <value>8080</value>
        <description>API服务端口</description>
    </property>
</configurations>

使用示例

import asyncio
from commons import Config, configure_logging

configure_logging()
config = Config("./config/conf-template.xml", "./config/conf-private.xml")

# config demo
host = config.get_option_str("api.server.host") or "127.0.0.1"

# 以下语句执行时会报错,因为`api.server2.port`不存在
# port:int = config.get_int("api.server2.port")
# 以下语句会返回None
# port = config.get_option_int("api.server2.port")

port = config.get_option_int("api.server.port")

print(host, port)

封装OpenAI格式的大模型API调用,并支持自动缓存大模型输出结果,重复内容的调用直接从缓存中获取结果。

示例代码如下:

import asyncio
from commons import ChatModel, Config, configure_logging, create_chat_client

configure_logging()
# 参考./config/conf-template.xml,设置具体的DEEPSEEK_V4_FLASH的值
config = Config("./config/conf-template.xml", "./config/conf-private.xml")
client = create_chat_client(
    ChatModel.DEEPSEEK_V4_FLASH, config=config, max_tokens=8192, cache_enabled=True
)
response = asyncio.run(
    client.chat(system_prompt="You're a good helper.", user_prompt="hello")
)
print(response)

日常开发

运行以下脚本,可以自动格式化代码,并发现类型错误。

make fine

开发环境设置

  1. 安装uv

    curl -fsSL https://get.uv.dev | bash
    # 或者通过pip安装
    pip install uv
    
  2. 可编辑安装本项目

    #创建虚拟环境
    uv venv
    uv pip install -e .
    
  3. 通过uv运行脚本main.py示例

    uv run -m commons.main
    
  4. 代码格式化

uv run ruff check --fix src
#或者
make fmt
  1. git设置

避免中文文件名称乱码:

git config --global core.quotepath false # 让 Git 不要将非 ASCII 字符的文件名用引号括起来

Metadata

Release files for better-commons 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for better-commons 0.1.2
File Size Uploaded Provenance Actions
better_commons-0.1.2.tar.gz 89.5 kB Without provenance Details for better_commons-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for better-commons 0.1.2
File Interpreter ABI Platform Actions
better_commons-0.1.2-py3-none-any.whl Python 3 none any Details for better_commons-0.1.2-py3-none-any.whl

Total release size: 138.6 kB

Release files / better_commons-0.1.2.tar.gz

Download URL better_commons-0.1.2.tar.gz
Size 89.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6dcc2f079e3bd8f8b525f724cf135a1d1f26f91ea42557528370fef71e658582
BLAKE2b-256 checksum
How to use checksums
bfbb4ce89b422dd19e8ab8ba9370507cc9af8423a1e91c62c2e873171ce6086e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / better_commons-0.1.2-py3-none-any.whl

Download URL better_commons-0.1.2-py3-none-any.whl
Size 49.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66b6b5294fc768e77bae24c23f32c6380917b0ff7a7e6fe8be4df2b2e97ceedd
BLAKE2b-256 checksum
How to use checksums
d958426cb3c17253609de04e6dabd9109f6c752374e0ff06d8ad74b11ea4bfec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for better-commons 0.1.2

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page