Skip to main content

beansieve 账单整理工具

beansieve 是一个用于整理 beancount 账本的小工具。这个工具主要实现以下两个功能:

  1. 通过 --type aggregate 将账单中包含特定账户的条目放到一个新账本文件中。
  2. 通过 --type archive 将特定时间范围内的 Transaction 条目保留到一个新账本文件中。

安装

pip install beansieve

一些术语

  • type:操作类型,即 aggregate 按账户拆分账本或 archive 按时间范围归档账本;
  • source:源文件,即要操作的账本文件;
  • dest:目标文件,即新生成的账本文件的保存目录;
  • rule:使用 aggregate 时用于指定聚合规则;
  • keep:使用 archive 时用于指定保留时间范围。

Structure 输出文件的结构

该工具运行会重新组织账本文件,组织规则如下:

── account.beancount      # 账户相关指令,即 `open` 和 `close`
├── commodity.beancount   # commodity 指令
├── custom.beancount      # custom 指令(如 fava 中提供的 balance 指令)
├── document.beancount    # document 指令
├── event.beancount       # event 指令
├── history.beancount     # 如果使用 `archive`,则 `keep` 范围外的 Transaction 会被移动到此文件夹中
├── main.beancount        # 主账本文件,include 其他账本文件,如果使用 `archive` 则会包含 `keep` 范围内的 Transaction,如果使用 `aggregate` 则未匹配上的 Transaction 文件会被移动到此文件
├── note.beancount        # note 指令
├── price.beancount       # price 指令
├── <key>.beancount       # 使用 `aggregate` 时按 `rule` 拆分出来的账本
└── query.beancount       # query 指令

aggregate 的用法

使用 aggregate 将账单中包含特定账户的条目放到一个新账本文件中。 通过使用 --rule 设置账户匹配规则,支持设置多种规则,单条规则的格式为 <key>:<pattern>,其中 <key> 为文件名称,<pattern> 为一个用于匹配账户名称的正则表达式。如果有多条规则则使用 , 分割匹配规则。

例如下面的脚本会将 :

  1. posting 中账户名称匹配 Income:US:ETrade:.* 的 Transaction 移动到 Etrade.beancount 文件;
  2. posting 中账户名称匹配 Income:US:Babble:Salary 的 Transaction 移动到 Payroll.beancount 文件;
  3. 其他的 Transaction 移动到 main.beancount 文件;
  4. 其他的 Directive 会按照 Structure 规则移动到特定的文件中。
# generate sample beancount files
bean-example > example.beancount
bean-report example.beancount balances > example-balances.txt

# aggregate
rm -rf example_aggregate
python -m beansieve \
  --type aggregate \
  --source "example.beancount" \
  --dest "example_aggregate" \
  --rule "Etrade|Income:US:ETrade:.*,Payroll|Income:US:Babble:Salary"
bean-report example_aggregate/main.beancount balances > example_aggregate-balances.txt

archive 的用法

使用 archive 能够保留将特定时间范围内的 Transaction 条目保留到 main.beancount 文件中,时间范围外的 Transaction 会被移动到 archive.beancount 文件中。 其中 --keep 用于指定从当前时刻其回溯的时间范围,比如 1d 表示自当前时刻起前 1 天的 Transaction 会被保留到 main.beancount 文件中,1w 则保留前 7 天的条目,1m 保留前 30 天的条目。 例如下面的脚本会将:

  1. 从当前时刻起前 7 天的 Transaction 移动到 main.beancount 文件;
  2. 其他 Transaction 移动到 archive.beancount 文件;
  3. 其他的 Directive 会按照 Structure 规则移动到特定的文件中。
# generate sample beancount files
bean-example > example.beancount
bean-report example.beancount balances > example-balances.txt

rm -rf example_archive
python -m beansieve \
  --type archive \
  --source "example.beancount" \
  --dest "example_archive" \
  --keep 7d
bean-report example_archive/main.beancount balances > example_archive-balances.txt

其他说明

  1. 这个工具不能保证输入(--source)文件结构的完整性,因此如果有使用 include 拆分账本文件的习惯,请先确保你认同 Structure 的拆分规则;
  2. aggregate 和 archive 这两个命令不能同时使用,因为对我而言只是当有拆分 Transaction 的时候才会使用 aggregate 拆分账本,更多时候是通过 archive 来确保最近一段时间的账本文件(main.beancount)足够精简;
  3. 使用前做好备份,以防止数据丢失,在使用前后使用 bean-report 生成 balance 报表进行对比,可以确保转换无误。

Metadata

Release files for beansieve 0.1.1

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

Source distribution (sdist)

Source distribution for beansieve 0.1.1
File Size Uploaded Provenance Actions
beansieve-0.1.1.tar.gz 12.2 kB Without provenance Details for beansieve-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 28.6 kB

Release files / beansieve-0.1.1.tar.gz

Download URL beansieve-0.1.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f48bced390ee0c01bf020b9ab9c822411fdea10a5a914fd1317e87e19d8c9f3d
BLAKE2b-256 checksum
How to use checksums
63bef6f06a674771c48a2740b0055e919981f7cb7adeac390b2497a90e3053d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

Release files / beansieve-0.1.1-py3-none-any.whl

Download URL beansieve-0.1.1-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d319fb50abd5830b732a31b683c79439b7a69cfe1590f334acf689081bbcf5ca
BLAKE2b-256 checksum
How to use checksums
7331c300da24603761714676a6848fb6fe86d1246136c4cfcbbd6d6000d156d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

Security for beansieve 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.

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