diff --git a/modules/Pi.py b/modules/Pi.py index 9a64f22..c810db7 100644 --- a/modules/Pi.py +++ b/modules/Pi.py @@ -19,6 +19,7 @@ along with this program. If not, see . from ConfigParser import NoOptionError, NoSectionError import math import random +import sqlite3 from extlib import irclib @@ -34,21 +35,53 @@ class Pi(Module): Idea from #linode on OFTC. Code from http://www.eveandersson.com/pi/monte-carlo-circle """ + def db_init(self): + """ + Initialize database tables. + """ + + # init the database if pi table doesn't exist + version = self.db_module_registered(self.__class__.__name__) + if version == None: + # create tables + db = self.get_db() + try: + db.execute(''' + CREATE TABLE pi_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + count_inside INTEGER NOT NULL, + count_total INTEGER NOT NULL, + time TEXT DEFAULT CURRENT_TIMESTAMP + ) + ''') + db.execute(''' + CREATE VIEW pi_latest_pi AS + SELECT count_inside, count_total + FROM pi_log + WHERE id = last_insert_rowid() + ''') + db.execute('INSERT INTO drbotzo_modules VALUES (?,?)', (self.__class__.__name__, 1)) + db.commit() + except sqlite3.Error as e: + db.rollback() + print("sqlite error: " + str(e)) + raise + def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): if what == "pi": + db = self.get_db() try: - if not self.config.has_section(self.__class__.__name__): - self.config.add_section(self.__class__.__name__) + cur = db.cursor() + pi_data = cur.execute('SELECT * FROM pi_latest_pi') + datum = pi_data.fetchone() - # set default values if unset - if not self.config.has_option(self.__class__.__name__, "count_inside"): - self.config.set(self.__class__.__name__, "count_inside", "0") - if not self.config.has_option(self.__class__.__name__, "count"): - self.config.set(self.__class__.__name__, "count", "0") - - # load values from config - count_inside = self.config.getint(self.__class__.__name__, "count_inside") - count = self.config.getint(self.__class__.__name__, "count") + if datum == None: + count_inside = 0 + count = 0 + else: + # load values + count_inside = datum['count_inside'] + count = datum['count_total'] x = random.random() y = random.random() @@ -60,11 +93,13 @@ class Pi(Module): count += 1 pi = 4.0 * count_inside / count - self.config.set(self.__class__.__name__, "count_inside", str(count_inside)) - self.config.set(self.__class__.__name__, "count", str(count)) + cur.execute('INSERT INTO pi_log (count_inside, count_total) VALUES (?,?)', (count_inside, count)) + db.commit() + except sqlite3.Error as e: + db.rollback() + return self.reply(connection, replypath, "sqlite error: " + str(e)) - return self.reply(connection, replypath, "({0:.10f}, {1:.10f}) is {2}within the circle. pi is {5:.10f} ({3:d}/{4:d}).".format(x, y, "" if inside else "not ", count_inside, count, pi)); - except NoOptionError, NoSectionError: pass + return self.reply(connection, replypath, "({0:.10f}, {1:.10f}) is {2}within the circle. pi is {5:.10f} ({3:d}/{4:d}).".format(x, y, "" if inside else "not ", count_inside, count, pi)); # vi:tabstop=4:expandtab:autoindent # kate: indent-mode python;indent-width 4;replace-tabs on;