2. astm.codec :: Base decoding and encoding functions¶
- astm.codec.decode(data, encoding='latin-1')¶
Common ASTM decoding function that tries to guess which kind of data it handles.
If data starts with STX character (0x02) than probably it is full ASTM message with checksum and other system characters.
If data starts with digit character (0-9) than probably it is frame of records leading by his sequence number. No checksum is expected in this case.
Otherwise it counts data as regular record structure.
Note, that data should be bytes, not unicode string even if you know his encoding.
Parameters: - data (bytes) – ASTM data object.
- encoding (str) – Data encoding.
Returns: List of ASTM records with unicode data.
Return type: list
- astm.codec.decode_component(field, encoding)¶
Decodes ASTM field component.
- astm.codec.decode_frame(frame, encoding)¶
Decodes ASTM frame: list of records followed by sequence number.
- astm.codec.decode_message(message, encoding)¶
Decodes complete ASTM message that is sent or received due communication routines. It should contains checksum that would be additionally verified.
Parameters: - message (bytes) – ASTM message.
- encoding (str) – Data encoding.
Returns: Tuple of three elements:
- int frame sequence number.
- list of records with unicode data.
- bytes checksum.
Raises: - ValueError if ASTM message is malformed.
- AssertionError if checksum verification fails.
- astm.codec.decode_record(record, encoding)¶
Decodes ASTM record message.
- astm.codec.decode_repeated_component(component, encoding)¶
Decodes ASTM field repeated component.
- astm.codec.encode(records, encoding='latin-1', size=None, seq=1)¶
Encodes list of records into single ASTM message, also called as “packed” message.
If you need to get each record as standalone message use iter_encode() instead.
If the result message is too large (greater than specified size if it’s not None), than it will be split by chunks.
Parameters: Returns: List of ASTM message chunks.
Return type: list
- astm.codec.encode_component(component, encoding)¶
Encodes ASTM record field components.
- astm.codec.encode_message(seq, records, encoding)¶
Encodes ASTM message.
Parameters: Returns: ASTM complete message with checksum and other control characters.
Return type: str
- astm.codec.encode_record(record, encoding)¶
Encodes single ASTM record.
Parameters: Returns: Encoded ASTM record.
Return type: str
- astm.codec.encode_repeated_component(components, encoding)¶
Encodes repeated components.
- astm.codec.is_chunked_message(message)¶
Checks plain message for chunked byte.
- astm.codec.iter_encode(records, encoding='latin-1', size=None, seq=1)¶
Encodes and emits each record as separate message.
If the result message is too large (greater than specified size if it’s not None), than it will be split by chunks.
Yields: ASTM message chunks. Return type: str
- astm.codec.join(chunks)¶
Merges ASTM message chunks into single message.
Parameters: chunks (iterable) – List of chunks as bytes.
- astm.codec.make_checksum(message)¶
Calculates checksum for specified message.
Parameters: message (bytes) – ASTM message. Returns: Checksum value that is actually byte sized integer in hex base Return type: bytes
- astm.codec.split(msg, size)¶
Split msg into chunks with specified size.
Chunk size value couldn’t be less then 7 since each chunk goes with at least 7 special characters: STX, frame number, ETX or ETB, checksum and message terminator.
Parameters: - msg (bytes) – ASTM message.
- size (int) – Chunk size in bytes.
Yield: bytes