Skip to main content

A Python library for reading and writing bit fields with type annotations

Project description

BitFieldRW

PyPI version Python Support License: MIT

一个强大且易于使用的 Python 位字段读写库,支持类型注解和大端序位布局。

特性

  • 🚀 类型安全: 使用 Python 类型注解定义位字段结构
  • 📦 多种数据类型: 支持有符号/无符号整数、浮点数
  • 🔧 嵌套结构: 支持位字段结构的嵌套
  • 🔄 字节序支持: 支持大端序和小端序
  • 完整测试: 包含全面的测试用例
  • 📝 清晰 API: 直观易懂的接口设计

安装

pip install bitfieldrw

快速开始

基本用法

from bitfieldrw import bitfield, BitFieldMixin, Uint, Int, Float

@bitfield
class NetworkPacket(BitFieldMixin):
    version: Uint[4]      # 4位版本号
    header_len: Uint[4]   # 4位头长度
    type_of_service: Uint[8]  # 8位服务类型
    total_length: Uint[16]    # 16位总长度

# 创建实例
packet = NetworkPacket()
packet.version = 4
packet.header_len = 5
packet.type_of_service = 0
packet.total_length = 1500

# 转换为字节
data = packet.to_bytes()  # 默认大端序
print(f"Packed bytes: {data.hex()}")

# 从字节加载
new_packet = NetworkPacket()
new_packet.from_bytes(data)
print(f"Version: {new_packet.version}")
print(f"Total length: {new_packet.total_length}")

支持的数据类型

@bitfield
class DataTypes(BitFieldMixin):
    unsigned_int: Uint[8]     # 无符号整数 (0-255)
    signed_int: Int[8]        # 有符号整数 (-128 到 127)
    float_val: Float[32]      # 32位浮点数

data = DataTypes()
data.unsigned_int = 255
data.signed_int = -50
data.float_val = 3.14159

嵌套结构

@bitfield
class Header(BitFieldMixin):
    magic: Uint[16]
    version: Uint[8]

@bitfield
class Message(BitFieldMixin):
    header: Header           # 嵌套的位字段结构
    payload_length: Uint[16]
    flags: Uint[8]

msg = Message()
msg.header.magic = 0xABCD
msg.header.version = 1
msg.payload_length = 1024
msg.flags = 0x80

字节序支持

# 大端序 (默认)
big_endian_data = packet.to_bytes(byteorder="big")

# 小端序
little_endian_data = packet.to_bytes(byteorder="little")

# 从字节加载时指定字节序
packet.from_bytes(data, byteorder="little")

与原始整数互转

# 转换为整数
raw_value = packet.to_int()

# 从整数加载
packet.from_int(0x12345678)

API 参考

数据类型

  • Uint[n]: n 位无符号整数
  • Int[n]: n 位有符号整数(二进制补码)
  • Float[32]: 32 位 IEEE 754 浮点数

主要方法

  • to_bytes(byteorder="big"): 转换为字节串
  • from_bytes(data, byteorder="big"): 从字节串加载
  • to_int(): 转换为原始整数
  • from_int(value): 从整数加载
  • get_bit_length(): 获取总位长度
  • get_byte_length(): 获取字节长度(向上取整)

位布局

BitFieldRW 使用大端序位布局,字段按照定义顺序从高位到低位排列:

@bitfield
class Example(BitFieldMixin):
    field_a: Uint[4]  # 位 15-12
    field_b: Uint[8]  # 位 11-4
    field_c: Uint[4]  # 位 3-0

应用场景

  • 📡 网络协议: 解析和构造网络数据包
  • 💾 文件格式: 处理二进制文件格式
  • 🔧 硬件接口: 与嵌入式设备通信
  • 🎮 游戏开发: 处理紧凑的数据结构
  • 📊 数据压缩: 高效存储标志位和小整数

许可证

MIT License - 详见 LICENSE 文件。

贡献

欢迎提交 issue 和 pull request!

更新日志

0.1.0

  • 初始版本发布
  • 支持基本位字段操作
  • 支持嵌套结构
  • 完整的测试覆盖

Project details


Download files

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

Source Distribution

bitfieldrw-0.1.0.tar.gz (64.5 kB view details)

Uploaded Source

Built Distribution

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

bitfieldrw-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file bitfieldrw-0.1.0.tar.gz.

File metadata

  • Download URL: bitfieldrw-0.1.0.tar.gz
  • Upload date:
  • Size: 64.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for bitfieldrw-0.1.0.tar.gz
Algorithm Hash digest
SHA256 996b743893237dd91d006d7a55402e456c8d9d6b0176dac9fd8a79123588a61c
MD5 5f1ec2f6b426c586246d8a6744e2a205
BLAKE2b-256 66fade315ffeabee61420b2ee67b83bc4e3a0301e12c1ead0c051b64b3eb2728

See more details on using hashes here.

File details

Details for the file bitfieldrw-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bitfieldrw-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for bitfieldrw-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20d9f4aa91896b8948988dedba61fc2dae9ebae5146651533975d27ea1753198
MD5 0c401e63b68ff45bb8813f73c0fe611c
BLAKE2b-256 b314d85b6d50565da02ec3ba3e2581ad8db6be8dd2a21ede6227baca01993d30

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page