Skip to main content

来自已弃坑的 Hydrogenlib,重拾起当初的决心

[!TIP] 这个项目来自 HydrogenLibObjective-Struct 模块,是一个脱离大项目的版本

同时,HydrogenLib中的 Objective-Struct 不再会更新

[!TIP] 作者的主要开发精力放在了另一些项目上,也许很长时间这个项目都不会再更新

OBStruct

一个简单、纯粹、极速的二进制解析库

亮点

  • 纯 Python
  • 基于类型注解的字段声明方式
  • 支持 struct 标准库中的所有格式,并额外支持 array 类型
  • 基于 AOT 的加速
  • 完整的操作支持(打包、解包、流式处理)

[!WARNING] 使用 array 类型时,建议不要传入 list 类型或其它 Python 序列, 可能会带来极大的性能影响

基准测试

系统环境

  • 12th Gen Intel® Core (TM) i7-12700H

在 12个字段,10000 次操作,8轮取平均值的条件下:

  • 打包:269.639ns 一次操作
  • 解包:582.185ns 一次操作

新的测试报告请参见 BENCHMARK_TEST

安装

pip install obstruct

如果使用 uv 等包管理器,则按照工具的方法安装即可

用法

首先声明一个 Struct 类型,它是一个 dataclass,也是一个 struct

import obstruct as obs
from dataclasses import dataclass


@dataclass()
class MyStruct(obs.Struct):
    a: int
    b: float
    c: obs.long
    d: obs.unsigned_short


if __name__ == '__main__':
    data = MyStruct(a=0, b=2.0, c=10000, d=14545)

    packed_data = data.pack()
    print(packed_data)

    restored = MyStruct.unpack(packed_data)
    print(restored)

    print(data == restored)

[!TIP] 建议 dataclass 装饰器中使用 slots=True 以加速属性访问和内存效率

在上述示例中,你可能发现 data != restored,这是浮点数的精度限制导致的。 因此,我们不建议使用浮点数类型,浮点数在二进制传输中也不常见。

计划

  • 展平装饰器,目前的 API 较为繁琐,需要简化为唯一的 Struct 基类
  • 继续优化性能
  • 通过存根文件强制表明类型似乎变得不再有效了
  • 支持 Tag 联合类型,对标 msgspec

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

obstruct-0.5.1.tar.gz (44.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

obstruct-0.5.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file obstruct-0.5.1.tar.gz.

File metadata

  • Download URL: obstruct-0.5.1.tar.gz
  • Upload date:
  • Size: 44.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for obstruct-0.5.1.tar.gz
Algorithm Hash digest
SHA256 21e35e2ea077acb64ec27b189712d812d174d651b70cda624ec90b24dd8bfa3c
MD5 644c7b2b71c206add054c7bdb0adae98
BLAKE2b-256 d29f4f7a85bc2c6f85a6b13717fe631f67a9fb8ab98f7fc0a87d3c92b5b273e0

See more details on using hashes here.

File details

Details for the file obstruct-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: obstruct-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for obstruct-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e1d5e55daa1ca9731d70261e094f495dcbbffa15b8514c48a6d48b6bd8ced0a4
MD5 d08cc5bc70e805a7e92aae22d550acd0
BLAKE2b-256 5c089194e5ad4886c52a398fd4da84bf730fdb1819eccf95a3e4a52f65599fe7

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.2

2 files

This release

0.5.1 This release

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.15

2 files

0.2.14

2 files

0.2.13

2 files

0.2.12

2 files

0.2.11

2 files

0.2.10

2 files

0.2.9

2 files

0.2.8

2 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 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