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

"""Simple class for encapsulating sending raw commands to an Arduino over
a serial connection. Configurable via settings.py"""

import serial
from settings import FILENAME, BAUD, SIGNATURE, TIMEOUT


class Utils:
    @classmethod
    def checksum(cls, message):
        hash = 0
        for i in xrange(len(message)):
            hash = ord(message[i]) + (hash << 6) + (hash << 16) - hash
        return str(hash & 0xFFFFFFFF)


class Arduino:
    def __init__(self):
        self.com = serial.Serial(FILENAME, BAUD, timeout=TIMEOUT)
        try:
            self.com.open()
        except:
            pass

    def send_raw_command(self, raw_command):
        checksum = Utils.checksum(raw_command)
        output = '%s %s %s\r\n' % (SIGNATURE, raw_command, checksum)
        print output
        self.com.write(output)
        
    def send_command(self, command_name, *args):
        self.send_raw_command(' '.join((command_name,) + args))

    def toggle_lights(self, number):
        self.send_command('togglelight', (number,))

    def close(self):
        self.com.close()
<commit_msg>Modified arduino.py to not error if arduino isn't available.<commit_after>#!/usr/bin/env python

"""Simple class for encapsulating sending raw commands to an Arduino over
a serial connection. Configurable via settings.py"""

import serial
from settings import FILENAME, BAUD, SIGNATURE, TIMEOUT


class Utils:
    @classmethod
    def checksum(cls, message):
        hash = 0
        for i in xrange(len(message)):
            hash = ord(message[i]) + (hash << 6) + (hash << 16) - hash
        return str(hash & 0xFFFFFFFF)


class Arduino:
    def __init__(self):
        self.open()

    def send_raw_command(self, raw_command):
        if connected:
            checksum = Utils.checksum(raw_command)
            output = '%s %s %s\r\n' % (SIGNATURE, raw_command, checksum)
            print output
            self.com.write(output)
        
    def send_command(self, command_name, *args):
        if connected:
            self.send_raw_command(' '.join((command_name,) + args))

    def toggle_lights(self, number):
        if connected:
            self.send_command('togglelight', (number,))

    def close(self):
        self.com.close()
        self.connected = False

    def open(self):
        try:
            self.com = serial.Serial(FILENAME, BAUD, timeout=TIMEOUT)
            self.com.open()
            self.connected = True
        except:
            self.connected = False
{'fraction_non_alphanumeric': 0.11084718923198733, 'fraction_numerical': 0.00395882818685669, 'mean_word_length': 2.424119241192412, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5135093', 'n_tokens_mistral': 783, 'n_tokens_neox': 744, 'n_words': 206}