Skip to main content

Tools for building Sans IO libraries

Project description

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"

Parser and BytesParser

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(b"")  # signal eof

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

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

sansio_tools-1.0.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

sansio_tools-1.0.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file sansio_tools-1.0.0.tar.gz.

File metadata

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

File hashes

Hashes for sansio_tools-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0b316c9500482e279dff7201f598071fd3ea6faf7f4b332f0ff58d29e9fbd47b
MD5 9056a3675a0276138387ea12a5028726
BLAKE2b-256 f128de9a551d195fd36f7340b306bd8fa5b7e2470b596a167f6990409bed309b

See more details on using hashes here.

File details

Details for the file sansio_tools-1.0.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for sansio_tools-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d34c62591515edb5eb828f721b98b9cd3cd2d4f37f8587058f3e875f92fdf912
MD5 4746762d7d3dc5dd9686ea657b06cdcd
BLAKE2b-256 c7c9259ff0cd5188b0a4fbda6b01713eca42f0488b36a6f8b7802a08579a5c57

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