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
开发环境设置
-
安装uv
curl -fsSL https://get.uv.dev | bash # 或者通过pip安装 pip install uv
-
可编辑安装本项目
#创建虚拟环境 uv venv uv pip install -e .
-
通过uv运行脚本main.py示例
uv run -m commons.main
-
代码格式化
uv run ruff check --fix src
#或者
make fmt
- git设置
避免中文文件名称乱码:
git config --global core.quotepath false # 让 Git 不要将非 ASCII 字符的文件名用引号括起来
Metadata
Release files for better-commons 0.1.1
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 |
|---|---|---|---|---|
| better_commons-0.1.1.tar.gz | 38.9 kB | Without provenance | Details for better_commons-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| better_commons-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for better_commons-0.1.1-py3-none-any.whl |
Total release size: 75.9 kB
Release files / better_commons-0.1.1.tar.gz
| Download URL | better_commons-0.1.1.tar.gz |
|---|---|
| Size | 38.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d0195cb5f49606026c8ab125527a0beb4e3cd9ae6eae162059b88a06f4367ba9
|
|
BLAKE2b-256 checksum How to use checksums |
374eb7ec52cf8886ccc84c49c41f355e3984fa561813cc70a2c72418143ca5b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","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.1-py3-none-any.whl
| Download URL | better_commons-0.1.1-py3-none-any.whl |
|---|---|
| Size | 37.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ca0d34ae5da633f2403051d9d1ac39a75c44b19e0d877e3a90138160a5e98027
|
|
BLAKE2b-256 checksum How to use checksums |
0013cc491ca0628196d68f860aba585dec8355100cd9f0e46ca88bc4ee652c98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","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.1
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.