sansio_tools
Useful tools for building Sans IO-based libraries.
{class}~.queue.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"
{class}~.parser.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)
return "ok!"
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
assert p.generator_return_value == "ok!"
{class}~.parser.StringParser
{class}.StringParser is the text counterpart of {class}.BinaryParser: a generator-based parser for streams of
str chunks (e.g. data received from a socket, decoded to text). Its main method,
{meth}~.StringParser.read_character_ranges(), reads a string made up of allowed characters, which you declare
with a {class}.CharacterRanges object. Reading stops at end of stream, when the maximum length is reached, or at
the first disallowed character, and the {class}.StringStopReason returned alongside the string tells you
which of those happened. Any terminating character is left in the queue so your parser can consume
or inspect it next.
Example code:
from sansio_tools.parser import CharacterRanges, StringParser, StringStopReason
# Declare which characters are allowed in a "word" (here: a-z and 0-9).
word_chars = CharacterRanges(((97, 123), (48, 58)))
results = []
def parser_generator(p: StringParser):
# Read words, one at a time, separated by any non-word character.
while True:
# Stop once the stream has ended and the buffer is empty.
if p.eof and not p.queue:
break
# Read 1 to 10 word characters. The terminating character (if any)
# is left in the queue, and the stop reason tells us why reading stopped.
text, reason = yield from p.read_character_ranges(word_chars, min_length=1, max_length=10)
results.append((text, reason))
if reason is StringStopReason.FORBIDDEN:
# A non-word character ended the word; consume it as a separator.
p.queue.popleft(1)
p = StringParser(parser_generator)
# Feed the input in chunks; words are assembled across feed boundaries.
p.feed("he")
p.feed("llo,")
assert results == [("hello", StringStopReason.FORBIDDEN)]
p.feed("wor")
p.feed("ld!")
assert results == [
("hello", StringStopReason.FORBIDDEN),
("world", StringStopReason.FORBIDDEN),
]
p.feed("abcdefg")
p.feed("hijklmnop")
assert results[-1] == ("abcdefghij", StringStopReason.MAX_LENGTH)
p.feed(None) # signal eof
assert results == [
("hello", StringStopReason.FORBIDDEN),
("world", StringStopReason.FORBIDDEN),
("abcdefghij", StringStopReason.MAX_LENGTH),
("klmnop", StringStopReason.EOF),
]
assert p.generator_exited
Notes:
- If fewer than
min_lengthmatching characters are found before the string ends, {meth}~.StringParser.read_character_ranges()raises {class}.UnexpectedCharacterError. - {meth}
~.StringParser.read_character_ranges_iter()is the streaming variant: it yields each chunk of matching characters as it becomes available (yieldingNonewhenever it needs more data) and returns aStringStopReasonat the end. Use it when you want to process a long string incrementally instead of receiving it as one joined string.
{class}~.multihash.HashFunctionRegistry
The multihash module implements the multihash and
multibase formats. A multihash is a self-describing hash:
it embeds the hash function code and the digest size as varint prefixes in front of the raw digest, so
the value can be interpreted without any out-of-band information about which algorithm produced it.
A ready-to-use {data}~.multihash.registry instance ships with a set of common hash functions (SHA-2,
SHA-3, SHAKE-256, and BLAKE2). Use {attr}~.multihash.HashFunctionRegistry.name_to_hash to look one up by
name, call it to obtain a {class}~.multihash.Hasher, and feed it data to produce a
{class}~.multihash.Digest.
Example code:
from sansio_tools.multihash import registry
# Compute a sha2-256 multihash.
hasher = registry.name_to_hash["sha2-256"]()
hasher.update(b"hello world")
digest = hasher.digest()
print(digest.to_multihash_bytes().hex())
# 1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
print(digest.to_multihash_base64url())
# uEiC5TSe5k00-CKUuUtfafav6xITv43pTgO6QiPes4u_N6Q
# Decode a multihash (bytes or multibase base64url) back into a Digest.
decoded = registry.decode(digest.to_multihash_base64url())
assert decoded.digest == digest.digest
assert decoded.function.name == "sha2-256"
The module also exposes more low-level functions:
- {func}
~.multihash.multihash_varint_encode()for the LEB128 varints used by the format, and the sans-io reader generators {func}~.multihash.read_multihash_varint(), {func}~.multihash.read_multihash_raw_header()and {func}~.multihash.read_multihash_raw()for decoding varints and raw multihashes from a {class}~.parser.BinaryParser. - {func}
~.multihash.multibase_encode_base64url()/ {func}~.multihash.multibase_decode_base64url()for multibase base64url strings (theuprefix). - {meth}
~.multihash.HashFunctionRegistry.register()to add additional hash functions at runtime. - {meth}
~.multihash.HashFunctionRegistry.read_from_binary_parser()to read a multihash from a {class}~.parser.BinaryParseras a {class}~.multihash.Digest, or {func}~.multihash.read_multihash_raw()to split it into its(function_code, digest)components without requiring a registered function.
Metadata
Release files for sansio-tools 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sansio_tools-2.0.0.tar.gz | 31.2 kB | Without provenance | Details for sansio_tools-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sansio_tools-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for sansio_tools-2.0.0-py3-none-any.whl |
Total release size: 48.0 kB
Release files / sansio_tools-2.0.0.tar.gz
| Download URL | sansio_tools-2.0.0.tar.gz |
|---|---|
| Size | 31.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73ffb8b142cc697a00e82161d74367f3615d844d644af673f6eb5e43e1d3f94f
|
|
BLAKE2b-256 checksum How to use checksums |
25134877daebbfcff1c8c3483aa4a116db395a07bce4a956582cff733c9faf57
|
| 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-2.0.0-py3-none-any.whl
| Download URL | sansio_tools-2.0.0-py3-none-any.whl |
|---|---|
| Size | 16.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d63557861e1fe7336c8696892cc8dba4f95580608a00f742a64561e71f53ed9
|
|
BLAKE2b-256 checksum How to use checksums |
a8132d20c38b76594fd6204e6cdbcd51015a7aeb8b9a919d78c4b32dcfa1dc1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Security for sansio-tools 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.