def update(): try: Teams = pd.read_csv("Teams.csv") Teams = Teams['Name'] Choices =[] for row in Teams: choice=(row,row) Choices.append(choice) class up(FlaskForm): blueSide=SelectField(u'Blue Side',choices=Choices) redSide=SelectField(u'Red Side',choices=Choices) score=SelectField(u'Score',choices=[("0","Blue side won"),("1","Red side won")]) submit = SubmitField('Submit') db=DBHelper() BlueSide = None RedSide = None Score = None form =up() if form.validate_on_submit(): BlueSide = form.blueSide.data RedSide = form.redSide.data Score = form.score.data db.insertar("INSERT INTO Games VALUES('" + BlueSide + "','" + RedSide +"','" + Score+"')") flash(f'Score successfully updated in to the database!',category='success') print(db.leer()) return render_template('Update.html',name="Update results",form=form) except Exception as error: print(error) return "Error"
def setUp(self): self.db_name = 'test.db' create_db(self.db_name) self.dbh = DBHelper(self.db_name)
class TestipyDBTests(unittest.TestCase): """ Tests relating to database functionality. """ def setUp(self): self.db_name = 'test.db' create_db(self.db_name) self.dbh = DBHelper(self.db_name) def tearDown(self): self.dbh.close_con() delete_db(self.db_name) def test_open_con(self): self.dbh.open_con() if not self.dbh.con: raise Exception("Test failed, no open connection.") def test_close_con(self): self.dbh.open_con() self.dbh.close_con() try: self.dbh.con.cursor() self.dbh.c.execute('SELECT SQLITE_VERSION()') raise Exception('Connection open when expected to be closed.') except sql.Error as e: if e.__str__() == 'Cannot operate on a closed database.': pass else: print("Error: %s" % e.args[0]) raise Exception('Unexpected error.') def test_cursor(self): self.dbh.open_con() try: self.dbh.cursor() self.dbh.c.execute('SELECT SQLITE_VERSION()') if '<sqlite3.Cursor object at' in self.dbh.c.__str__(): pass else: raise Exception("'<sqlite3.Cursor object at' not found in " + self.dbh.c.__str__()) except sql.Error as e: print("Error: %s" % e.args[0]) raise Exception('Unexpected error.') def test_db_version(self): version = self.dbh.db_version() match = re.search('SQLite Version: \d.\d.\d.\d', version) if match: pass # test passes else: print(traceback.format_exc()) raise Exception('No sqlite3 version regex match.') def test_db_call_commit(self): # this is silly. commands = [ "ATTACH 'test.db' as test", 'DROP TABLE IF EXISTS test.test_table', '''CREATE TABLE IF NOT EXISTS test.test_table ( id INTEGER PRIMARY KEY AUTOINCREMENT, date text NULL, name text NULL, description text NULL, pickled_obj text NULL );''' ] try: self.dbh.open_con() self.dbh.cursor() for command in commands: self.dbh.c.execute(command) self.dbh.con.commit() except sql.Error as e: print("Error: %s" % e.args[0]) print(traceback.format_exc()) raise Exception("SQL ERROR. TEST FAILED.") finally: self.dbh.close_con()
def __init__(self, session): self.db = DBHelper('stats.db', 'plstats') super().__init__(session)
class PLHandler(BaseHandler): def __init__(self, session): self.db = DBHelper('stats.db', 'plstats') super().__init__(session) def handle_msg(self, ident_string, recipient, msg): sender_nick = self.get_nick(ident_string) msg = msg[1:] #remove leading : if sender_nick == self.session.nick: return elif(recipient == self.session.nick): self._handle_priv_msg(msg, sender_nick) elif(recipient == self.session.channel): self._handle_channel_msg(msg) def _handle_priv_msg(self, msg, sender_nick): global CMD_STAT if msg.startswith(CMD_STAT): self._display_stats(msg[len(CMD_STAT):], sender_nick) def _handle_channel_msg(self, msg): self._record_reference(msg) pass def _record_reference(self, msg): global pl_dict, DB_RECORD_TYPE for key, value in pl_dict.items(): pattern = r'(^{0}$|.*\s+{0}\s+.*|^{0}\s+.*|.*\s+{0}([.,;!].*|$))'.format(re.escape(key)) result = re.match(pattern, msg.lower()) if not result is None: self.db.add_entry(DB_RECORD_TYPE, int(time.time()), value) #print("Found Entry: {0}".format(value)) def _display_stats(self, command, sender_nick): command = command.strip() cmd_parts = command.split(" ") now_time = int(time.time()) try: if command != "" and len(cmd_parts) == 1: low_time = (now_time - int(cmd_parts[0])*3600) db_out = str(self.db.get_categories_count(low_time, now_time, 5)) self._send_msg(sender_nick, "Top mentions in the last {0} hour(s) : {1}".format(cmd_parts[0], db_out)) elif len(cmd_parts) == 2: low_time = (now_time - int(cmd_parts[0])*3600) db_out = str(self.db.get_category_count(low_time, now_time, cmd_parts[1])) self._send_msg(sender_nick, "Number of mentions of {0} in the last {1} hour(s) : {2}".format(cmd_parts[1], cmd_parts[0], db_out)) else: self._show_stats_help(sender_nick) except: pass def _get_time_tuple(self, num_hours): curr_time = int(time.time()) return ((curr_time - int(num_hours)*3600, curr_time)) def _show_stats_help(self, sender_nick): self._send_msg(sender_nick, "syntax: !stats <hours> [<language>]") def _send_msg(self, who, msg): self.session.send_data("PRIVMSG %s :%s\r\n" % (who, msg)) def get_nick(self, ident_string): temp = ident_string.split("!")[0] return temp.lstrip(':')