Skip to main content

sansio_tools

Useful tools for building Sans IO-based libraries.

BytesQueue

Example code:

from sansio_tools.queue import BytesQueue

q = BytesQueue()

q.append(b"012345")

print(bytes(q.peekleft(2)))  # b"01"

print(bytes(q.popleft(3)))  # b"012"

# put the bytes back into the queue!
q.appendleft(b"012")

print(bytes(q.popleft(4)))  # b"0123"

BinaryParser

Example code:

from struct import Struct
from sansio_tools.parser import BinaryParser


# int8, uint32, uint64
my_struct = Struct("!bIQ")

def parser_generator(p: BinaryParser):
    b = yield from p.read_bytes(6)
    results.append(b)
    b = yield from p.read_bytes(6)
    results.append(b)

    lst = yield from p.read_struct(my_struct)
    results.append(lst)

    n = yield from p.read_int(2, "little", False)
    results.append(n)

    n = yield from p.read_variable_length_int_7bit(
        maximum_length=10,
        byteorder="big",
        continuation_bit_value=True,
        require_canonical=True,
    )
    results.append(n)

results = []
p = BinaryParser(parser_generator)
p.feed(b"hell")
p.feed(b"o world!\xff")

# receive partial results as they become available!
assert results == [b"hello ", b"world!"]
assert not p.generator_exited
results.clear()

p.feed(b"\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x12\x34")
p.feed(b"\x81\x80\x80")

assert results == [(-1, 0xff << 24, 0xff << 56), 0x3412]
assert not p.generator_exited
results.clear()

p.feed(b"\x80\x05")
p.feed(None)  # signal eof

assert results == [(1 << (7 * 4)) + 5]
assert p.generator_exited

Metadata

Release files for sansio-tools 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sansio-tools 1.0.1
File Size Uploaded
sansio_tools-1.0.1.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sansio-tools 1.0.1
File Interpreter ABI Platform
sansio_tools-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.6 kB

Release files / sansio_tools-1.0.1.tar.gz

Download URL sansio_tools-1.0.1.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
14e2c6451c4eb1564e811858a00772894b0e4ddf05dc3e67b1c5d7f4788f9e6b
BLAKE2b-256 checksum
How to use checksums
6990d887a983b31ba3ec4d7497462401e6a1fb30dac4c04faea9b9876f066689
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / sansio_tools-1.0.1-py3-none-any.whl

Download URL sansio_tools-1.0.1-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8397a26c7af052a1c855b53e5bd98c7e2a2072a027f71e967c36871b3174f3e9
BLAKE2b-256 checksum
How to use checksums
9a71c4dc656ce86c409a55106166f9b5fe7f78321fae924e141fff34c84cf638
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.1.0

2 release 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