Skip to content
/ stru Public

An IDE friendly alternative to Python's struct and construct

License

Notifications You must be signed in to change notification settings

mrdor44/stru

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stru

Stru is a flexible and declarative binary structured data library, much like Python's struct module. With Stru, you declare your structs C-style, while maintaining all of Python's upside.

The key difference between Stru and Python's struct is that Python's struct is imperative, meaning the coder tells struct how to parse the data, while Stru is declarative, meaning the coder tells Stru how the data looks like.

Stru provides anything from simple struct-like constructs that can be used as one-on-one alternatives, to more complex constructs that further extend the struct module features.

For example, consider the following C-style struct:

struct message {
    uint32_t length;
    uint8_t magic;
    uint8_t* buffer; // buffer length indicated by length field
};

Packing this struct into binary form using Python's struct would look like the following code:

import struct
buffer = b'ABCD'
message = struct.pack(f'LB{len(buffer)}s', len(buffer), 0xEF, buffer)

On the other hand, packing this struct using Stru would look like the following code:

from stru import Struct, FieldType
class Message(Struct):
    length = FieldType.UnsignedInt
    magic = FieldType.BYTE(default=0xEF)
    buffer = FieldType.Buffer(length)
buffer=b'ABCD'
message = Message(length=len(buffer), buffer=buffer).pack()

So who needs to mess around with unreadable format strings and ambiguous letters? Start using Stru to enjoy the full power of struct and even more.

About

An IDE friendly alternative to Python's struct and construct

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages