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 toDeserializeErrorvia theerror_infoattribute.
See also
- 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:
UnicodeError – Invalid string.
- Returns:
the byte sequence.
- Return type:
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:
UnicodeError – Invalid string.
- Return type:
- 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:
DeserializeError –
UnicodeErrorraised when decoding data.- Returns:
the string.
- Return type:
Important
By default, trailing newline sequences are removed. This can be change by setting keep_end parameter to
Trueat 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:
LimitOverrunError – Reached buffer size limit.
IncrementalDeserializeError –
UnicodeErrorraised when decoding data.
- Return type:
- final create_deserializer_buffer(sizehint: int) bytearray
See
BufferedIncrementalPacketSerializer.buffered_incremental_deserialize()documentation for details.- Return type:
- 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:
LimitOverrunError – Reached buffer size limit.
IncrementalDeserializeError –
UnicodeErrorraised when decoding data.
- Return type: