convert Pi.py to use sqlite backend
This commit is contained in:
parent
1d73deda1c
commit
97893a3b7a
@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
from ConfigParser import NoOptionError, NoSectionError
|
from ConfigParser import NoOptionError, NoSectionError
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
from extlib import irclib
|
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
|
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):
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
if what == "pi":
|
if what == "pi":
|
||||||
|
db = self.get_db()
|
||||||
try:
|
try:
|
||||||
if not self.config.has_section(self.__class__.__name__):
|
cur = db.cursor()
|
||||||
self.config.add_section(self.__class__.__name__)
|
pi_data = cur.execute('SELECT * FROM pi_latest_pi')
|
||||||
|
datum = pi_data.fetchone()
|
||||||
|
|
||||||
# set default values if unset
|
if datum == None:
|
||||||
if not self.config.has_option(self.__class__.__name__, "count_inside"):
|
count_inside = 0
|
||||||
self.config.set(self.__class__.__name__, "count_inside", "0")
|
count = 0
|
||||||
if not self.config.has_option(self.__class__.__name__, "count"):
|
else:
|
||||||
self.config.set(self.__class__.__name__, "count", "0")
|
# load values
|
||||||
|
count_inside = datum['count_inside']
|
||||||
# load values from config
|
count = datum['count_total']
|
||||||
count_inside = self.config.getint(self.__class__.__name__, "count_inside")
|
|
||||||
count = self.config.getint(self.__class__.__name__, "count")
|
|
||||||
|
|
||||||
x = random.random()
|
x = random.random()
|
||||||
y = random.random()
|
y = random.random()
|
||||||
@ -60,11 +93,13 @@ class Pi(Module):
|
|||||||
count += 1
|
count += 1
|
||||||
pi = 4.0 * count_inside / count
|
pi = 4.0 * count_inside / count
|
||||||
|
|
||||||
self.config.set(self.__class__.__name__, "count_inside", str(count_inside))
|
cur.execute('INSERT INTO pi_log (count_inside, count_total) VALUES (?,?)', (count_inside, count))
|
||||||
self.config.set(self.__class__.__name__, "count", str(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));
|
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
|
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
||||||
|
Loading…
Reference in New Issue
Block a user