Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bswck/bitbin
Object-oriented parsing of binary data.
https://github.com/bswck/bitbin
Last synced: 24 days ago
JSON representation
Object-oriented parsing of binary data.
- Host: GitHub
- URL: https://github.com/bswck/bitbin
- Owner: bswck
- License: mit
- Created: 2023-01-01T23:01:05.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-02-21T23:50:27.000Z (over 1 year ago)
- Last Synced: 2023-03-05T18:16:01.845Z (over 1 year ago)
- Language: Python
- Size: 83 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bitbin
bitbin is a Python library for parsing binary data in an object-oriented manner achieved by
combining the features of [dataclasses](https://docs.python.org/3/library/dataclasses.html) and
[Construct](https://construct.readthedocs.io/en/latest/index.html)
(library specialized in declarative parsing and building of binary data).## Setup
`pip install bitbin`## Examples
bitbin may be used for stashing your data or sending it through sockets.
Consider following examples:```py
import bitbin as bb# Create a Struct dataclass that
# is equivalent to construct.Struct
class Point2D(bb.Struct):
"""Two-dimensional point in the plane."""# Declare 2 struct members of type int
# (which are by default understood similarly
# as in C++ - construct.Int32sn)
x: int
y: int# Create an Array type that consists of 2 Structs
Vector2D = bb.Array[2, Point2D]# Instantiate the 2-dimensional vector by member initializer list
# Other possible methods to do this:
# my_vector = Vector2D(Point2D(-20, 8), Point2D(10, 15))
# my_vector = Vector2D({'x': -20, 'y': 8}, {'x': 10, 'y': 15})
my_vector = Vector2D((-20, 8), (10, 15))# Transform to bytes. In this case the result is
# b'\xec\xff\xff\xff\x08\x00\x00\x00\n\x00\x00\x00\x0f\x00\x00\x00'
stash = bb.dumps(my_vector)# Recreate our vector using Struct.load()
# that bases on construct.Struct(...).parse()
loaded_vector = bb.loads(Vector2D, stash)# Check if both are equal
# Equality operator is available thanks to dataclasses
assert my_vector == loaded_vector# Create a Circle dataclass
# that uses previously created
# Point2D
class Circle(bb.Struct):
center: Point2D
radius: bb.double # construct.Float64n# Created object repr():
# Circle(center=Point2D(5, 5), radius=20.0)
my_circle = Circle((5, 5), 20)# b'\x05\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x004@'
stash = bb.dumps(my_circle)# Circle(center=Point2D(5, 5), radius=20.0)
loaded_circle = bb.loads(Circle, stash)assert my_circle == loaded_circle # True!
```## License
[MIT](https://choosealicense.com/licenses/mit/)## Contact
* [bswck](https://github.com/bswck)## Similar Projects
* [construct-typing](https://github.com/timrid/construct-typing)
* [construct-classes](https://github.com/matejcik/construct-classes)