pymap.bytes

Defines useful types and utilities for working with bytestrings.

pymap.bytes.MaybeBytes: TypeAlias = bytes | typing.SupportsBytes

An object that can be converted to a bytestring.

class pymap.bytes.MaybeBytesT

A type variable bound to MaybeBytes.

alias of TypeVar(‘MaybeBytesT’, bound=bytes | SupportsBytes)

pymap.bytes.has_bytes(value)[source]

Checks if the value is bytes or implements the __bytes__ method to be converted to bytes.

Parameters:

value (object) – The value to check.

Return type:

TypeGuard[bytes | SupportsBytes]

class pymap.bytes.WriteStream(*args, **kwargs)[source]

Typing protocol indicating the object implements the write() method.

abstract write(data)[source]

Defines an abstract method where data is written to a stream or buffer.

Parameters:

data (bytes) – The data to write.

Return type:

Any

class pymap.bytes.Writeable[source]

Base class for types that can be written to a stream.

final tobytes()[source]

Convert the writeable object back into a bytestring using the write() method.

Return type:

bytes

classmethod empty()[source]

Return a Writeable for an empty string.

Return type:

Writeable

classmethod wrap(data)[source]

Wrap the bytes in a Writeable.

Parameters:

data (bytes | SupportsBytes) – The object to wrap.

Return type:

Writeable

classmethod concat(data)[source]

Wrap the iterable in a Writeable that will write each item.

Parameters:

data (Iterable[bytes | SupportsBytes]) – The iterable to wrap.

Return type:

Writeable

write(writer)[source]

Write the object to the stream, with one or more calls to write().

Parameters:

writer (WriteStream) – The output stream.

Return type:

None

class pymap.bytes.BytesFormat(how)[source]

Helper utility for performing formatting operations that produce bytestrings. While similar to the builtin formatting and join operations, this class intends to provide cleaner typing.

Parameters:

how (bytes) – The formatting string or join delimiter to use.

format(data)[source]

String interpolation into the format string.

Parameters:

data (Iterable[SupportsIndex | bytes | SupportsBytes]) – The data interpolated into the format string.

Return type:

bytes

Examples

BytesFormat(b'Hello, %b!') % b'World'
BytesFormat(b'%b, %b!') % (b'Hello', b'World')
join(*data)[source]

Iterable join on a delimiter.

Parameters:

data (Iterable[SupportsIndex | bytes | SupportsBytes]) – Iterable of items to join.

Return type:

bytes

Examples

BytesFormat(b' ').join([b'one', b'two', b'three'])