String Serializer

String line packet serializer module.

class easynetwork.serializers.line.StringLineSerializer

Bases: BufferedIncrementalPacketSerializer[str, str, bytearray]

A serializer to handle ASCII-based protocols.

__init__(newline: Literal['LF', 'CR', 'CRLF'] = 'LF', *, encoding: str = 'ascii', unicode_errors: str = 'strict', limit: int = DEFAULT_SERIALIZER_LIMIT, keep_end: bool = False, debug: bool = False) None
Parameters:
  • newline (Literal['LF', 'CR', 'CRLF']) –

    Magic string indicating the newline character sequence. Possible values are:

    • "LF" (the default): Line feed character ("\n").

    • "CR": Carriage return character ("\r").

    • "CRLF": Carriage return + line feed character sequence ("\r\n").

  • encoding (str) – String encoding. Defaults to "ascii".

  • unicode_errors (str) – Controls how encoding errors are handled.

  • limit (int) – Maximum buffer size. Used in incremental serialization context.

  • keep_end (bool) – If True, returned data will include the separator at the end.

  • debug (bool) – If True, add information to DeserializeError via the error_info attribute.

final serialize(packet: str) bytes

Encodes the given string to bytes.

Roughly equivalent to:

def serialize(self, packet):
    return packet.encode()

Example

>>> s = StringLineSerializer()
>>> s.serialize("character string")
b'character string'
Parameters:

packet (str) – The string to encode.

Raises:
Returns:

the byte sequence.

Return type:

bytes

Important

The output does not contain newline.

final incremental_serialize(packet: str) Generator[bytes]

Encodes the given string to bytes and appends separator.

See AbstractIncrementalPacketSerializer.incremental_serialize() documentation for details.

Raises:
Return type:

Generator[bytes]

final deserialize(data: bytes) str

Decodes data and returns the string.

Roughly equivalent to:

def deserialize(self, data):
    return data.decode()

Example

>>> s = StringLineSerializer()
>>> s.deserialize(b"character string")
'character string'
Parameters:

packet – The data to decode.

Raises:

DeserializeErrorUnicodeError raised when decoding data.

Returns:

the string.

Return type:

str

Important

By default, trailing newline sequences are removed. This can be change by setting keep_end parameter to True at initialization.

final incremental_deserialize() Generator[None, bytes, tuple[str, bytes]]

Yields until separator is found and return the decoded string.

See AbstractIncrementalPacketSerializer.incremental_deserialize() documentation for details.

Raises:
Return type:

Generator[None, bytes, tuple[str, bytes]]

final create_deserializer_buffer(sizehint: int) bytearray

See BufferedIncrementalPacketSerializer.buffered_incremental_deserialize() documentation for details.

Return type:

bytearray

final buffered_incremental_deserialize(buffer: bytearray) Generator[int, int, tuple[str, memoryview]]

Yields until separator is found and return the decoded string.

See BufferedIncrementalPacketSerializer.buffered_incremental_deserialize() documentation for details.

Raises:
Return type:

Generator[int, int, tuple[str, memoryview]]

property separator: bytes

Byte sequence that indicates the end of the token. Read-only attribute.

property debug: bool

The debug mode flag. Read-only attribute.

property buffer_limit: int

Maximum buffer size. Read-only attribute.

property keep_end: bool

Specifies whether or not to preserve the separator during deserialization. Read-only attribute.

property encoding: str

String encoding. Read-only attribute.

property unicode_errors: str

Controls how encoding errors are handled. Read-only attribute.