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

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.1.tar.gz (14.0 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.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sansio_tools-1.0.1.tar.gz
  • Upload date:
  • Size: 14.0 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.1.tar.gz
Algorithm Hash digest
SHA256 14e2c6451c4eb1564e811858a00772894b0e4ddf05dc3e67b1c5d7f4788f9e6b
MD5 1a4fcd05651fe1fc77925083b875ea3c
BLAKE2b-256 6990d887a983b31ba3ec4d7497462401e6a1fb30dac4c04faea9b9876f066689

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sansio_tools-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8397a26c7af052a1c855b53e5bd98c7e2a2072a027f71e967c36871b3174f3e9
MD5 bb343e40db45f9884f47b2e203fa3074
BLAKE2b-256 9a71c4dc656ce86c409a55106166f9b5fe7f78321fae924e141fff34c84cf638

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 files

1.0.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page