PyStreamHandler is a lightweight Python package designed to facilitate reading and writing binary data from buffers using Python's built-in `struct` module and `bytearray`.
Project description
PyStreamHandler
PyStreamHandler is a lightweight Python package designed to facilitate reading and writing binary data from buffers
using Python's built-in struct module and bytearray. It provides two main classes:
- ReadStreamHandler: For parsing binary streams with automatic buffer management and error handling.
- WriteStreamHandler: For constructing binary streams with value range validation and ease of use.
Features
- Binary Parsing: Read signed and unsigned values (byte, short, int, long) with automatic buffer overflow checks.
- Data Construction: Write binary data to a mutable buffer using simple methods that ensure correct value ranges.
- Pythonic Design: Uses Python's native
bytearrayfor storage and thestructmodule for packing/unpacking binary data.
Installation
You can install PyStreamHandler via pip (if it's published):
pip install pystreamhandler
Alternatively, to install locally from your repository, clone the repository and run:
git clone https://github.com/hansenchristoffer/pystreamhandler.git
cd pystreamhandler
pip install .
Usage
Writing Data
Here's an example that demonstrates how to write various types of data to a binary buffer:
from pystreamhandler import WriteStreamHandler
# Initialize the write stream handler
writer = WriteStreamHandler()
# Write various types of data
writer.write_byte(127) # Signed byte (-128 to 127)
writer.write_unsigned_byte(255) # Unsigned byte (0 to 255)
writer.write_short(32767) # Signed short (-32768 to 32767)
writer.write_unsigned_short(65535) # Unsigned short (0 to 65535)
writer.write_int(2147483647) # Signed int (-2147483648 to 2147483647)
writer.write_unsigned_int(4294967295) # Unsigned int (0 to 4294967295)
writer.write_long(9223372036854775807) # Signed long (-9223372036854775808 to 9223372036854775807)
writer.write_unsigned_long(18446744073709551615) # Unsigned long (0 to 18446744073709551615)
# Retrieve the binary buffer
buffer = writer.get_buffer()
Reading Data
And here is how you can read data back from a binary buffer:
from pystreamhandler import ReadStreamHandler
# Assume 'buffer' is a bytearray containing binary data
reader = ReadStreamHandler(buffer)
# Read data in the same order as written
byte_value = reader.read_byte() # also, pushes cursor forward by 1 byte
short_value = reader.read_short() # also, pushes cursor forward by 2 bytes
int_value = reader.read_int() # also, pushes cursor forward by 4 bytes
long_value = reader.read_long() # also, pushes cursor forward by 8 bytes
print(f"Byte: {byte_value}, Short: {short_value}, Int: {int_value}, Long: {long_value}")
Running Tests
A test suite is included to verify functionality and error handling. To run the tests, use Python’s unittest discovery:
python -m unittest discover tests
Or if you prefer pytest, simply run:
pytest
Contributing
Contributions are welcome! If you have suggestions or improvements:
- Fork the repository.
- Create a new branch for your changes.
- Submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
For any questions, suggestions, or feedback, please contact chris.hansen.ch@outlook.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pystreamhandler-1.0.0.tar.gz.
File metadata
- Download URL: pystreamhandler-1.0.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d68dc6c90c11afb0eb72202206855a5ee72a0fd8ad3695feb87efafc47e8f35a
|
|
| MD5 |
e1882dac2bce9088f67af08cfbaa10cc
|
|
| BLAKE2b-256 |
5fa32eb5ff41b0ff9d2a628c1f0b3f78bd512d7b25111969db537e7e9fa8fa36
|
Provenance
The following attestation bundles were made for pystreamhandler-1.0.0.tar.gz:
Publisher:
python-publish.yml on HansenChristoffer/pystreamhandler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystreamhandler-1.0.0.tar.gz -
Subject digest:
d68dc6c90c11afb0eb72202206855a5ee72a0fd8ad3695feb87efafc47e8f35a - Sigstore transparency entry: 187223385
- Sigstore integration time:
-
Permalink:
HansenChristoffer/pystreamhandler@68d1cd51a4e0ff19fdb80b1b8d3c6bd24f85fdd2 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/HansenChristoffer
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@68d1cd51a4e0ff19fdb80b1b8d3c6bd24f85fdd2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pystreamhandler-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pystreamhandler-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c93e3a90b43f8a6c9e965d9a46f0df77bce2c4701ea162596e535d0a8af7743
|
|
| MD5 |
51a302f0423cd6817e8dec77cc23e20f
|
|
| BLAKE2b-256 |
344615bb32ef1f1158c4d4c53bfaebccc0ff233775de372760301143ba310e51
|
Provenance
The following attestation bundles were made for pystreamhandler-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on HansenChristoffer/pystreamhandler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystreamhandler-1.0.0-py3-none-any.whl -
Subject digest:
2c93e3a90b43f8a6c9e965d9a46f0df77bce2c4701ea162596e535d0a8af7743 - Sigstore transparency entry: 187223389
- Sigstore integration time:
-
Permalink:
HansenChristoffer/pystreamhandler@68d1cd51a4e0ff19fdb80b1b8d3c6bd24f85fdd2 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/HansenChristoffer
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@68d1cd51a4e0ff19fdb80b1b8d3c6bd24f85fdd2 -
Trigger Event:
release
-
Statement type: