Welcome to stringtools’s documentation!

stringtools (Stringtools)

Author:

Description:

This Module supports functionality around string operations.

Submodules:

Unittest:

See also the unittest documentation.

Module Documentation:

stringtools.gzip_compress(s, compresslevel=9)

Method to compress a stream of bytes.

Parameters:
  • s (str) – The bytestream (string) to be compressed
  • compresslevel (int) – An optional compressionn level (default is 9)
Returns:

The compressed bytestream

Return type:

str

Example:

import stringtools
import report


report.stdoutLoggingConfigure(log_name_lvl=[(stringtools.logger_name, 'DEBUG'), ])
print(stringtools.hexlify(stringtools.gzip_compress(15 * b'\x00' + 15 * b'\xff')))

Will result to the following output:

2020-01-28 21:45:24,558: STRINGTOOLS - DEBUG - GZIP: Finished to compress a string (compression_rate=0.867, consumed_time=0.0s).
(26): 1f 8b 08 00 64 9d 30 5e 02 ff 63 60 40 01 ff 51 01 00 2d 8a 7d de 1e 00 00 00
stringtools.gzip_extract(s)

Method to extract data from a compress stream of bytes.

Parameters:s (str) – The compressed bytestream (string) to be extracted
Returns:The extracted data
Return type:str

Example:

import stringtools
import report


report.stdoutLoggingConfigure(log_name_lvl=[(stringtools.logger_name, 'DEBUG'), ])
print(stringtools.hexlify(stringtools.gzip_extract(b'\x1f\x8b\x08\x00\xd0\x82\xe5\x5c\x02\xff\x63\x60\x40\x01\xff\x51\x01\x00\x2d\x8a\x7d\xde\x1e\x00\x00\x00')))

Will result to the following output:

2020-01-28 21:45:39,899: STRINGTOOLS - DEBUG - GZIP: Finished to extract a string (compression_rate=0.867, consumed_time=0.0s).
(30): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
stringtools.hexlify(s)

Method to hexlify a string.

Parameters:s (str) – A string including the bytes to be hexlified.
Returns:The hexlified string
Return type:str

Example:

import stringtools


print(stringtools.hexlify(b'A stringexample with non printable characters like \x12'))

Will result to the following output:

(52): 41 20 73 74 72 69 6e 67 65 78 61 6d 70 6c 65 20 77 69 74 68 20 6e 6f 6e 20 70 72 69 6e 74 61 62 6c 65 20 63 68 61 72 61 63 74 65 72 73 20 6c 69 6b 65 20 12

csp (Carriage-Return seperation protocol)

Author:

Description:

This module is a submodule of stringtools and creates an frame to transmit and receive messages via an serial interface.

Submodules:

stringtools.csp.build_frame(msg, seperator='\n')

This Method builds an “csp-frame” to be transfered via a stream.

Parameters:data (str) – A String (Bytes) to be framed
Returns:The “csp-framed” message to be sent
Return type:str

Example:

import stringtools

data = b'message'
print(stringtools.hexlify(data))
print(stringtools.hexlify(stringtools.csp.build_frame(data)))

Will result to the following output:

(7): 6d 65 73 73 61 67 65
(8): 6d 65 73 73 61 67 65 0a
class stringtools.csp.csp(seperator='n')

This class extracts messages from an “csp-stream”.

Example:

import report
import stringtools

report.stdoutLoggingConfigure(log_name_lvl=[(stringtools.logger_name, 'DEBUG'), ])
s = stringtools.csp.csp()
for byte in b'message\n':
    data = s.process(bytes([byte]))
    if len(data) > 0:
        print(data)

Will result to the following output:

2020-01-28 21:31:22,093: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (5): 5b 27 6d 27 5d
2020-01-28 21:31:22,094: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (10): 5b 27 6d 27 5d 5b 27 65 27 5d
2020-01-28 21:31:22,094: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (15): 5b 27 6d 27 5d 5b 27 65 27 5d 5b 27 73 27 5d
2020-01-28 21:31:22,095: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (20): 5b 27 6d 27 5d 5b 27 65 27 5d 5b 27 73 27 5d 5b 27 73 27 5d
2020-01-28 21:31:22,095: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (25): 5b 27 6d 27 5d 5b 27 65 27 5d 5b 27 73 27 5d 5b 27 73 27 5d 5b 27 61 27 5d
2020-01-28 21:31:22,095: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (30): 5b 27 6d 27 5d 5b 27 65 27 5d 5b 27 73 27 5d 5b 27 73 27 5d 5b 27 61 27 5d 5b 27 67 27 5d
2020-01-28 21:31:22,096: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (35): 5b 27 6d 27 5d 5b 27 65 27 5d 5b 27 73 27 5d 5b 27 73 27 5d 5b 27 61 27 5d 5b 27 67 27 5d 5b 27 65 27 5d
2020-01-28 21:31:22,096: STRINGTOOLS - DEBUG - CSP: Leaving data in buffer (to be processed next time): (41): 5b 27 6d 27 5d 5b 27 65 27 5d 5b 27 73 27 5d 5b 27 73 27 5d 5b 27 61 27 5d 5b 27 67 27 5d 5b 27 65 27 5d 5b 27 5c 6e 27 5d
process(data)

This processes a byte out of a “stp-stream”.

Parameters:data (bytes) – A byte stream
Returns:A list of the extracted message(s)
Return type:list

stp (Serial transfer protocol)

Author:

Description:

This module is a submodule of stringtools and creates an serial frame to transmit and receive messages via an serial interface.

Submodules:

stringtools.stp.DATA_CLEAR_BUFFER = '<'

The clear buffer byte (‘\x3a\x3c’ -> start of message)

stringtools.stp.DATA_STORE_SYNC_VALUE = '='

The store sync value byte (‘\x3a\x3d’ -> ‘\x3a’ inside a message)

stringtools.stp.DATA_SYNC = ':'

The data sync byte

stringtools.stp.DATA_VALID_MSG = '>'

The valid message byte (‘\x3a\x3e’ -> end of message)

stringtools.stp.STP_STATE_ESCAPE_1 = 1

Escape 1 state definition (‘\x3a\x3c’ found)

stringtools.stp.STP_STATE_ESCAPE_2 = 2

Escape 2 state definition (‘\x3a’ found inside a message)

stringtools.stp.STP_STATE_IDLE = 0

Idle state definition (default)

stringtools.stp.STP_STATE_STORE_DATA = 3

Store data state definition (start of message found; data will be stored)

stringtools.stp.build_frame(data)

This Method builds an “stp-frame” to be transfered via a stream.

Parameters:data (str) – A String (Bytes) to be framed
Returns:The “stp-framed” message to be sent
Return type:str

Example:

import stringtools

print(repr(stringtools.stp.build_frame(b':message:')))

Will result to the following output:

b':<:=message:=:>'
class stringtools.stp.stp

This class extracts messages from an “stp-stream”.

Example:

import report
import stringtools

report.stdoutLoggingConfigure(log_name_lvl=[(stringtools.logger_name, 'DEBUG'), ])
s = stringtools.stp.stp()
for byte in b':<:=message:=:>':
    data = s.process(bytes([byte]))
    if len(data) > 0:
        print(data)

Will result to the following output:

2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: data sync (3a) received => changing state STP_STATE_IDLE -> STP_STATE_ESCAPE_1
2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: start pattern (3a 3c) received => changing state STP_STATE_ESCAPE_1 -> STP_STATE_STORE_DATA
2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2
2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: store sync pattern (3a 3d) received => changing state STP_STATE_ESCAPE_2 -> STP_STATE_STORE_DATA
2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2
2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: store sync pattern (3a 3d) received => changing state STP_STATE_ESCAPE_2 -> STP_STATE_STORE_DATA
2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2
2020-01-28 21:38:27,329: STRINGTOOLS - DEBUG - STP: end pattern (3a 3e) received => storing message and changing state STP_STATE_ESCAPE_2 -> STP_STATE_IDLE
2020-01-28 21:38:27,329: STRINGTOOLS - INFO - STP: message identified - (9): 3a 6d 65 73 73 61 67 65 3a
[b':message:']
process(data)

This processes a byte out of a “stp-stream”.

Parameters:data (bytes) – A byte stream
Returns:The extracted message or None, if no message is identified yet
Return type:str

Indices and tables