<commit_before>import panoptes.utils.logger as logger
import panoptes.utils.error as error

import logging
from threading import Thread
import serial
import time


@logger.set_log_level('debug')
@logger.has_logger
class SerialData():

    """
    Main serial class
    """

    def __init__(self, port=None):

        try:
            self.ser = serial.Serial()
            self.ser.port = port
            self.ser.baudrate = 9600

            self.ser.bytesize = serial.EIGHTBITS
            self.ser.parity = serial.PARITY_NONE
            self.ser.stopbits = serial.STOPBITS_ONE
            self.ser.timeout = 0.1
            self.ser.xonxoff = 0
            self.ser.rtscts = 0
            self.ser.interCharTimeout = None

            self.logger.debug(
                'Serial connection set up to mount, sleeping for two seconds')
            time.sleep(2)

        except:
            self.ser = None
            self.logger.critical('Could not set up serial port')

        self.logger.info('SerialData created')

    def connect(self):
        """ Actually set up the Thrad and connect to serial """

        self.logger.info('Serial connect called')
        if not self.ser.isOpen():
            try:
                self.ser.open()
            except serial.serialutil.SerialException as err:
                raise error.BadSerialConnection(msg=err)

        if type(self.ser) == 'panoptes.utils.serial.SerialData':
            Thread(target=serial_receiving, args=(self.ser,)).start()

        if not self.ser.isOpen():
            raise error.BadSerialConnection

        self.logger.info('Serial connection established to mount')
        return self.ser.isOpen()

    def write(self, value):
        """
            For now just pass the value along to serial object
        """
        assert self.ser
        assert self.ser.isOpen()

        self.logger.debug('Serial write: {}'.format(value))
        return self.ser.write(value.encode())

    def read(self):
        """ Reads value """
        assert self.ser
        assert self.ser.isOpen()

        response_string = self.ser.readline().decode()
        self.logger.debug('Serial read: {}'.format(response_string))
<commit_msg>Serial class will wait for a response<commit_after>import panoptes.utils.logger as logger
import panoptes.utils.error as error

import logging
from threading import Thread
import serial
import time


@logger.set_log_level('debug')
@logger.has_logger
class SerialData():

    """
    Main serial class
    """

    def __init__(self, port=None):

        try:
            self.ser = serial.Serial()
            self.ser.port = port
            self.ser.baudrate = 9600

            self.ser.bytesize = serial.EIGHTBITS
            self.ser.parity = serial.PARITY_NONE
            self.ser.stopbits = serial.STOPBITS_ONE
            self.ser.timeout = 0.1
            self.ser.xonxoff = 0
            self.ser.rtscts = 0
            self.ser.interCharTimeout = None

            self.logger.debug(
                'Serial connection set up to mount, sleeping for two seconds')
            time.sleep(2)

        except:
            self.ser = None
            self.logger.critical('Could not set up serial port')

        self.logger.info('SerialData created')

    def connect(self):
        """ Actually set up the Thrad and connect to serial """

        self.logger.info('Serial connect called')
        if not self.ser.isOpen():
            try:
                self.ser.open()
            except serial.serialutil.SerialException as err:
                raise error.BadSerialConnection(msg=err)

        if type(self.ser) == 'panoptes.utils.serial.SerialData':
            Thread(target=serial_receiving, args=(self.ser,)).start()

        if not self.ser.isOpen():
            raise error.BadSerialConnection

        self.logger.info('Serial connection established to mount')
        return self.ser.isOpen()

    def write(self, value):
        """
            For now just pass the value along to serial object
        """
        assert self.ser
        assert self.ser.isOpen()

        self.logger.debug('Serial write: {}'.format(value))
        return self.ser.write(value.encode())

    def read(self):
        """ 
        Reads value using readline 
        If no response is given, delay and then try to read again. Fail after 10 attempts
        """
        assert self.ser
        assert self.ser.isOpen()

        retry_limit = 7
        delay = 0.5

        i = 0
        while True:
            response_string = self.ser.readline().decode()
            if response_string > '' or i > retry_limit: break
            time.sleep(delay)
            i += 1

        self.logger.debug('Serial read: {}'.format(response_string))

{'fraction_non_alphanumeric': 0.10050568900126422, 'fraction_numerical': 0.005267593763168985, 'mean_word_length': 1.9122699386503068, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '85117', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1270, 'n_words': 339}