<commit_before>#!/usr/bin/env python3

import config

import time
from datetime import datetime

import serial
from phant import Phant


def read_and_store(bean_serial, phant_endpoint):
    print('Requesting...')
    bean.write(b'\n')
    raw_data = bean.readline()
    data = raw_data.decode('utf-8').rstrip('\r\n')

    if len(data) == 0:
        print('Bean timed out.\n')
        return False

    data_split = data.split('\t')
    print('    %s' % datetime.now())
    for num, field in enumerate(config.DATA_FIELDS):
        print('    %s: %s' % (field, data_split[num]))

    print('Sending...')
    phant.log(*data_split)

    print('Done!\n')
    return True

print('Data destination: %s' % config.PUBLIC_KEY)
print('Data fields: %s' % str(config.DATA_FIELDS))
phant = Phant(config.PUBLIC_KEY, *config.DATA_FIELDS,
              private_key=config.PRIVATE_KEY)
bean = serial.Serial(config.BEAN_SERIAL_PATH,
                     timeout=config.BEAN_REQ_TIMEOUT_SECS)

last_run = None

while True:
    now = datetime.now()

    if last_run:
        since_last_run = (now - last_run).total_seconds()

    if not last_run or since_last_run >= config.INTERVAL_SECS:
        success = read_and_store(bean, phant)
        if success:<commit_msg>Catch if Bean doesn't return enough values<commit_after>#!/usr/bin/env python3

import config

import time
from datetime import datetime

import serial
from phant import Phant


def read_and_store(bean_serial, phant_endpoint):
    print('Requesting...')
    bean.write(b'\n')
    raw_data = bean.readline()
    data = raw_data.decode('utf-8').rstrip('\r\n')

    if len(data) == 0:
        print('Bean timed out.\n')
        return False

    data_split = data.split('\t')
    if len(data_split) < len(config.DATA_FIELDS):
        print('Expected %s fields, but only received %s from Bean.' %
              (len(config.DATA_FIELDS), len(data_split)))
        return False

    print('    %s' % datetime.now())
    for num, field in enumerate(config.DATA_FIELDS):
        print('    %s: %s' % (field, data_split[num]))

    print('Sending...')
    phant.log(*data_split)

    print('Done!\n')
    return True

print('Data destination: %s' % config.PUBLIC_KEY)
print('Data fields: %s' % str(config.DATA_FIELDS))
phant = Phant(config.PUBLIC_KEY, *config.DATA_FIELDS,
              private_key=config.PRIVATE_KEY)
bean = serial.Serial(config.BEAN_SERIAL_PATH,
                     timeout=config.BEAN_REQ_TIMEOUT_SECS)

last_run = None

while True:
    now = datetime.now()

    if last_run:
        since_last_run = (now - last_run).total_seconds()

    if not last_run or since_last_run >= config.INTERVAL_SECS:
        success = read_and_store(bean, phant)
        if success:
{'fraction_non_alphanumeric': 0.12973171628077912, 'fraction_numerical': 0.002205071664829107, 'mean_word_length': 3.0445765230312034, 'pattern_counts': {'":': 0, '<': 4, '<?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': '3155407', 'n_tokens_mistral': 944, 'n_tokens_neox': 915, 'n_words': 201}