Skip to main content

ctb

化学工具箱核心库。目前提供:

  • balance(equation) — 化学方程式配平(支持离子)
  • mass(name, mode=1) — 相对原子 / 分子质量计算

安装

pip install ctb

用法

配平方程式

from ctb import balance

balance("H2+O2=H2O")                 # '2H2+O2=2H2O'
balance("Fe2O3+CO=Fe+CO2")           # 'Fe2O3+3CO=2Fe+3CO2'
balance("H^1++OH^1-=H2O")            # 'H^1++OH^1-=H2O'
balance("CO3^2-+H^1+=HCO3^1-")       # 'CO3^2-+H^1+=HCO3^1-'

计算相对原子 / 分子质量

from ctb import mass

# mode=1(默认):精确值
mass("H")               # 1.008
mass("Ag")              # 107.87
mass("CO2")             # 44.009
mass("Fe2(SO4)3")       # 399.858

# mode=2:近似值(四舍五入,氯除外为 35.5)
mass("H", 2)            # 1.0
mass("CO2", 2)          # 44.0
mass("NaCl", 2)         # 58.5   ← 氯按 35.5 处理
mass("Fe2(SO4)3", 2)    # 400.0

输入格式

符号 含义
= / -> / → 反应箭头(配平用)
+ 分隔化合物
^n+ 带 n 个正电荷(如 H^1+、Fe^3+)
^n- 带 n 个负电荷(如 CO3^2-)
() / [] 支持括号,可嵌套
大小写 严格区分,如 Co(钴)≠ CO(一氧化碳)

API

balance(equation: str) -> str

返回配平后的方程式字符串(不含空格)。失败抛 ValueError。

mass(name: str, mode: int = 1) -> float

返回相对原子质量 / 相对分子质量(float,保留 4 位小数)。

  • mode=1:精确值
  • mode=2:近似值(四舍五入到整数,Cl 固定为 35.5)

失败抛 ValueError(无效化学式 / 非法 mode)或 KeyError(未知元素)。

更新日志

v0.2.0

  • 新增 mass(name, mode=1) 函数
  • 相对原子质量改用 IUPAC 精确值

v0.1.0

  • 首个版本,提供 balance(equation)

许可证

MIT © 2026 123IOPjklbnm

Metadata

Release files for ctb 0.2.0

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

Source distribution (sdist)

Source distribution for ctb 0.2.0
File Size Uploaded
ctb-0.2.0.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ctb 0.2.0
File Interpreter ABI Platform
ctb-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / ctb-0.2.0.tar.gz

Download URL ctb-0.2.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
eb5dc5274f9e83adc3eace16768743400ea561deebbbe28186d98dc940780ff2
BLAKE2b-256 checksum
How to use checksums
43a9c78a097c4dcad38daf668fb6c411c7744f4831eeea8d46f201376317d137
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / ctb-0.2.0-py3-none-any.whl

Download URL ctb-0.2.0-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4838575ac76b414dcf7d05c7ed935e3efda131c467ac916fbbfd772e637f8ee
BLAKE2b-256 checksum
How to use checksums
0c132e4163010aa76e36eea600abfc3ab877dfbb98924ed00f851ba158ceb6cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

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