<commit_before>#!/usr/bin/env python
import MySQLdb
import logging
import sys
import time


class Database(object):
	"""
		Database class that manages the connection and any queries
	"""
	# Constructor, calls connect() on initialisation
	def __init__(self):
		self.log = logging.getLogger(type(self).__name__)
		self.log.debug("Initialising Database")
		self.connect()
	
	# Connects to the remote database server
	def connect(self):
		db_host = 'ftp.ar51.eu'
		db_port = 3306
		db_user = 'group11'
		db_pass = '<PASSWORD>'
		db_name = 'group11'
		try:
			self.log.debug("Initiated MySQL connection on %s as user %s" % (db_host, db_user))
			self.db_connect = MySQLdb.connect(host = db_host, port = db_port, user = db_user, passwd = db_pass, db = db_name)
		except MySQLdb.Error, e:
			self.log.critical("Could not initiate MySQL connection: %s" % (e))
			self.db_connect = False
			sys.exit()
	
	# Updates the state of an appliance
	def updateState(self, device, state):
		# Update devices current state
		try:
			if self.query("UPDATE DEVICES SET state = %s, date_time = %s WHERE id = %s" % (state, time.time(), device)):
				self.log.debug("Updating state of appliance %s to: %s" % (device, state))
			else:
				self.log.debug("Could not update state of appliance %s to: %s" % (device, state))
		except Exception, e:
			self.log.critical("Failed to update device %s state to %s. Error: %s" % (device, state, e))

		try:
			if self.query("INSERT INTO DEVICE_HISTORY (device_id, state, date_time, device_history) VALUES (%s, %s, %s, %s)" % (not state, time.time(), device, "Not sure what this is for yet")):
				self.log.debug("Adding to device history state: %s for device %s" % (not state, device))
			else:
				self.log.debug("Could not add to device %s history for sate: %s" % (device, not state))
		except Exception, e:
			self.log.critical("Failed to update device %s history to %s. Error: %s" % (device, not state, e))
	
	# Executes a SQL query
	def query(self, query):
		self.log.debug("Running query: %s" % (query))
		try:<commit_msg>APPLIANCE!<commit_after>#!/usr/bin/env python
import MySQLdb
import logging
import sys
import time


class Database(object):
	"""
		Database class that manages the connection and any queries
	"""
	# Constructor, calls connect() on initialisation
	def __init__(self):
		self.log = logging.getLogger(type(self).__name__)
		self.log.debug("Initialising Database")
		self.connect()
	
	# Connects to the remote database server
	def connect(self):
		db_host = 'ftp.ar51.eu'
		db_port = 3306
		db_user = 'group11'
		db_pass = '<PASSWORD>'
		db_name = 'group11'
		try:
			self.log.debug("Initiated MySQL connection on %s as user %s" % (db_host, db_user))
			self.db_connect = MySQLdb.connect(host = db_host, port = db_port, user = db_user, passwd = db_pass, db = db_name)
		except MySQLdb.Error, e:
			self.log.critical("Could not initiate MySQL connection: %s" % (e))
			self.db_connect = False
			sys.exit()
	
	# Updates the state of an appliance
	def updateState(self, appliances, state):
		# Update appliances current state
		try:
			if self.query("UPDATE DEVICES SET state = %s, date_time = %s WHERE id = %s" % (state, time.time(), appliances)):
				self.log.debug("Updating state of appliance %s to: %s" % (appliances, state))
			else:
				self.log.debug("Could not update state of appliance %s to: %s" % (appliances, state))
		except Exception, e:
			self.log.critical("Failed to update appliances %s state to %s. Error: %s" % (appliances, state, e))

		# Update appliances history
		try:
			if self.query("INSERT INTO appliances_HISTORY (appliances_id, state, date_time, appliances_history) VALUES (%s, %s, %s, %s)" % (not state, time.time(), appliances, "Not sure what this is for yet")):
				self.log.debug("Adding to appliances history state: %s for appliances %s" % (not state, appliances))
			else:
				self.log.debug("Could not add to appliances %s history for sate: %s" % (appliances, not state))
		except Exception, e:
			self.log.critical("Failed to update appliances %s history to %s. Error: %s" % (appliances, not state, e))
	
	# Executes a SQL query
	def query(self, query):
		self.log.debug("Running query: %s" % (query))
		try:
{'fraction_non_alphanumeric': 0.13240917782026768, 'fraction_numerical': 0.004780114722753346, 'mean_word_length': 4.072727272727272, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4570215', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1377, 'n_words': 466}