vanni-idmix — Python 实现
English: README_en.md
Python 参考实现,提供 IDX v1.2 二进制编码与 idmix 可插拔文本层。协议规范见仓库根目录 arithmetic.md。
架构概览
本包分三层,可独立使用:
┌─────────────────────────────────────────────────────────────┐
│ IdMix(高级封装) │
│ encode / decode:整数/短字符串 → 文本 │
│ 内部:Idx 编码 → Codec.encode │
└───────────────────────────┬─────────────────────────────────┘
│
┌──────────────────┴──────────────────┐
▼ ▼
┌─────────────────┐ ┌─────────────────────┐
│ Idx │ │ Codec(协议) │
│ 二进制编解码 │ │ 二进制 ↔ 文本 │
│ 可单独使用 │ │ 可单独使用 │
└─────────────────┘ └─────────────────────┘
| 组件 | 典型用途 |
|---|---|
| Idx | 只需紧凑自描述二进制,输出 bytes |
| Codec + encode_bytes / decode_string | 包装 Protobuf/CBOR/任意二进制为文本 |
| IdMix | access_key、短 ID:整数/字符串序列 → 混淆文本 |
安装
pip install vanni-idmix
要求:Python 3.9+
快速开始
完整流程(IdMix)
from idmix import IdMix, u16, i64, u32
m = IdMix.new()
# 整数 + 短字符串(≤63 字节)
s = m.encode(u16(5), i64(-1), u32(40), "hello")
print("encoded:", s)
values = m.decode(s)
print(values) # [5, -1, 40, 'hello']
解码返回
int/str。若需与 Go 一致的位宽编码,可使用u16()、i64()等工厂函数。
仅 IDX 二进制层
from idmix import Idx, u16, i64
idx = Idx.new()
data = idx.encode(u16(5), i64(-1))
out = idx.decode(data)
仅文本混淆层
from idmix import encode_bytes, decode_string
proto_bytes = bytes([0x08, 0x96, 0x01])
text = encode_bytes(proto_bytes)
raw = decode_string(text)
支持的输入类型
Idx.encode / IdMix.encode 接受可变参数,每个元素可为:
| Python 类型 | 说明 |
|---|---|
int |
整数(按 int64 存储) |
TypedInt / u8()…i64() |
带位宽的整数,与 Go 类型对齐 |
str |
UTF-8 文本,1~63 字节(按字节计) |
bytes / bytearray |
原始字节串,1~63 字节 |
限制:
- 单次最多 255 个对象(
with_max_objects可调) - 字符串/字节串单段最长 63 字节
- 空字符串、空
bytes不允许
模块结构
| 文件 | 职责 |
|---|---|
idx_codec.py |
Idx 二进制编解码 |
codec.py |
Codec 协议、Base64Codec、FuncCodec、包级函数 |
alphabet.py |
RadixCodec 默认进制编解码 |
number.py |
类型规范化与 TypedInt |
idmix.py |
IdMix 高级封装 |
配置示例
自定义 Idx 参数
from idmix import IdMix, Idx, with_idx, with_max_objects, with_max_variants, with_check_bits
idx = Idx.new(
with_max_objects(100),
with_max_variants(16),
with_check_bits(2),
)
m = IdMix.new(with_idx(idx))
自定义字符表
from idmix import IdMix, with_alphabet
m = IdMix.new(with_alphabet("abcd"))
使用 Base64 作为文本层
from idmix import IdMix, Base64Codec, with_codec
m = IdMix.new(with_codec(Base64Codec.new()))
自定义 Codec(异或 + Radix)
from idmix import FuncCodec, RadixCodec, encode_bytes, decode_string
inner = RadixCodec.new("abcd")
key = 0x5A
codec = FuncCodec(
encode_fn=lambda data: inner.encode(bytes(b ^ key for b in data)),
decode_fn=lambda s: bytes(b ^ key for b in inner.decode(s)),
)
text = encode_bytes(b"\xde\xad", codec)
测试
cd python
python -m unittest discover -s tests -v
跨语言向量位于 ../testdata/cross_language_vectors.json。
相关链接
Metadata
Release files for vanni-idmix 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| vanni_idmix-0.4.0.tar.gz | 14.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| vanni_idmix-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.6 kB
Release files / vanni_idmix-0.4.0.tar.gz
| Download URL | vanni_idmix-0.4.0.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7318b21e9a2118532f19e97ff64f268b017809d7b3b2798143069321dce0c71f
|
|
BLAKE2b-256 checksum How to use checksums |
fb1c6e2696516001787e3bf4f169829daebe43f39dcb5d97f82c0402a7591de9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.5
|
Release files / vanni_idmix-0.4.0-py3-none-any.whl
| Download URL | vanni_idmix-0.4.0-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f5d077edff7b71387c83008274e507a76279e260531de681d17d3d48ce631686
|
|
BLAKE2b-256 checksum How to use checksums |
e09bc7e0680ed54b50e6ddc62e772f2747f1815fe1bcdeb5ade1fd8832194727
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.5
|