<commit_before>from __future__ import print_function

import serial
import time


_FILE_PREFIX = 'mcs'
_FILE_EXTENSION = 'log'

_SERIAL_SPEED = 115200
_SERIAL_TIMEOUT = 1

_MKS_FIELDS = 30
_MKS_EOL = '\n'
_MKS_DELIMITER = '\t'


def main():
    port = serial.Serial(self._port_name, _SERIAL_SPEED, timeout=_SERIAL_TIMEOUT)

    print('Waiting for initial packet... ', end='')

    # Wait for end of packet before reading full packets
    while port.read() != '\n':
        pass

    print('OK!')

    log_name = "{}_{}.{}".format(_FILE_PREFIX, time.strftime('%Y%m%d%H%M%S'), _FILE_EXTENSION)
    line_buffer = ''

    print("Logging to file: {}".format(log_name))

    with open(log_name, 'w') as f:
        try:
            while True:
                # Read byte
                c = port.read()

                # Check for end of line
                if len(c) > 0:
                    if c == _MKS_EOL:
                        # Process line
                        print("Packet: {}".format(line_buffer))

                        mks_fields = line_buffer.split(_MKS_DELIMITER)

                        # Validate number of fields
                        if len(mks_fields) != _MKS_FIELDS:
                            print("ERROR: Expected {} fields but got {}!".format(_MKS_FIELDS, len(mks_fields)))
                            continue

                        # Overwrite the time with system time
                        mks_fields[0] = time.strftime('%d/%m/%Y %H:%M:%S')

                        # Log line to file
                        f.write(_MKS_DELIMITER.join(mks_fields) + '\r\n')

                        # Clear buffer for next line
                        line_buffer = ''
                    else:
                        line_buffer += c
        except KeyboardInterrupt:
            print('Exiting...')
        finally:
            port.close()


if __name__ == '__main__':
    main()
<commit_msg>Minor tweak to log to a specified path<commit_after>from __future__ import print_function

import os.path
import serial
import time


_FILE_PATH = '.'
_FILE_PREFIX = 'mcs'
_FILE_EXTENSION = 'log'

_SERIAL_SPEED = 115200
_SERIAL_TIMEOUT = 1

_MKS_FIELDS = 30
_MKS_EOL = '\n'
_MKS_DELIMITER = '\t'


def main():
    port = serial.Serial(self._port_name, _SERIAL_SPEED, timeout=_SERIAL_TIMEOUT)

    print('Waiting for initial packet... ', end='')

    # Wait for end of packet before reading full packets
    while port.read() != '\n':
        pass

    print('OK!')

    log_name = os.path.abspath(os.path.join((_FILE_PATH, "{}_{}.{}".format(_FILE_PREFIX, time.strftime('%Y%m%d%H%M%S'),
                                                                           _FILE_EXTENSION))))
    
    line_buffer = ''

    print("Logging to file: {}".format(log_name))

    with open(log_name, 'w') as f:
        try:
            while True:
                # Read byte
                c = port.read()

                # Check for end of line
                if len(c) > 0:
                    if c == _MKS_EOL:
                        # Process line
                        print("Packet: {}".format(line_buffer))

                        mks_fields = line_buffer.split(_MKS_DELIMITER)

                        # Validate number of fields
                        if len(mks_fields) != _MKS_FIELDS:
                            print("ERROR: Expected {} fields but got {}!".format(_MKS_FIELDS, len(mks_fields)))
                            continue

                        # Overwrite the time with system time
                        mks_fields[0] = time.strftime('%d/%m/%Y %H:%M:%S')

                        # Log line to file
                        f.write(_MKS_DELIMITER.join(mks_fields) + '\r\n')

                        # Clear buffer for next line
                        line_buffer = ''
                    else:
                        line_buffer += c
        except KeyboardInterrupt:
            print('Exiting...')
        finally:
            port.close()


if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.10884691848906561, 'fraction_numerical': 0.0054671968190854875, 'mean_word_length': 1.451278928136419, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7337771', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1157, 'n_words': 277}