From 16eb0b1e1fc30ca0b17e4680f38c3d6ea5259b6c Mon Sep 17 00:00:00 2001 From: sqlcoder Date: Wed, 28 Oct 2009 20:12:03 +0000 Subject: [PATCH 1/7] small changes for sqlite --- pyfpdb/fpdb_simple.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyfpdb/fpdb_simple.py b/pyfpdb/fpdb_simple.py index 3e2d6f5f..aaf74772 100644 --- a/pyfpdb/fpdb_simple.py +++ b/pyfpdb/fpdb_simple.py @@ -927,7 +927,7 @@ def recogniseTourneyTypeId(db, siteId, tourneySiteId, buyin, fee, knockout, rebu except: cursor.execute( """SELECT id FROM TourneyTypes WHERE siteId=%s AND buyin=%s AND fee=%s - AND knockout=%s AND rebuyOrAddon=%s""" + AND knockout=%s AND rebuyOrAddon=%s""".replace('%s', db.sql.query['placeholder']) , (siteId, buyin, fee, knockout, rebuyOrAddon) ) result = cursor.fetchone() #print "tried selecting tourneytypes.id, result:", result @@ -939,14 +939,14 @@ def recogniseTourneyTypeId(db, siteId, tourneySiteId, buyin, fee, knockout, rebu #print "insert new tourneytype record ..." try: cursor.execute( """INSERT INTO TourneyTypes (siteId, buyin, fee, knockout, rebuyOrAddon) - VALUES (%s, %s, %s, %s, %s)""" + VALUES (%s, %s, %s, %s, %s)""".replace('%s', db.sql.query['placeholder']) , (siteId, buyin, fee, knockout, rebuyOrAddon) ) ret = db.get_last_insert_id(cursor) except: #print "maybe tourneytype was created since select, try selecting again ..." cursor.execute( """SELECT id FROM TourneyTypes WHERE siteId=%s AND buyin=%s AND fee=%s - AND knockout=%s AND rebuyOrAddon=%s""" + AND knockout=%s AND rebuyOrAddon=%s""".replace('%s', db.sql.query['placeholder']) , (siteId, buyin, fee, knockout, rebuyOrAddon) ) result = cursor.fetchone() try: From 812998704de48c46148acf7973f926ab0603f16f Mon Sep 17 00:00:00 2001 From: Eratosthenes Date: Mon, 16 Nov 2009 15:35:58 -0500 Subject: [PATCH 2/7] Yet another try to fix missing X windows. Using xwininfo. --- pyfpdb/Tables_Demo.py | 4 ++-- pyfpdb/XTables.py | 54 ++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/pyfpdb/Tables_Demo.py b/pyfpdb/Tables_Demo.py index 92d85d75..397579e7 100755 --- a/pyfpdb/Tables_Demo.py +++ b/pyfpdb/Tables_Demo.py @@ -90,11 +90,11 @@ if __name__=="__main__": table.gdk_handle = gtk.gdk.window_foreign_new(table.number) print "table =", table - print "game =", table.get_game() +# print "game =", table.get_game() fake = fake_hud(table) print "fake =", fake - gobject.timeout_add(100, check_on_table, table, fake) +# gobject.timeout_add(100, check_on_table, table, fake) print "calling main" gtk.main() diff --git a/pyfpdb/XTables.py b/pyfpdb/XTables.py index c19db5d9..a14debc1 100644 --- a/pyfpdb/XTables.py +++ b/pyfpdb/XTables.py @@ -69,36 +69,42 @@ class Table(Table_Window): for listing in os.popen('xwininfo -root -tree').readlines(): if re.search(search_string, listing): print listing - mo = re.match('\s+([\dxabcdef]+)\s', listing) - window_number = int( mo.group(1), 0) - - done_looping = False - for outside in root.query_tree().children: - for inside in outside.query_tree().children: - if done_looping: break - if inside.id == window_number: - self.window = inside - self.parent = outside - done_looping = True - break + mo = re.match('\s+([\dxabcdef]+) (.+):\s\(\"([a-zA-Z.]+)\".+ (\d+)x(\d+)\+\d+\+\d+ \+(\d+)\+(\d+)', listing) + self.number = int( mo.group(1), 0) + self.width = int( mo.group(4) ) + self.height = int( mo.group(5) ) + self.x = int( mo.group(6) ) + self.y = int( mo.group(7) ) + self.title = re.sub('\"', '', mo.group(2)) + self.exe = "" # not used? + self.hud = None +# done_looping = False +# for outside in root.query_tree().children: +# for inside in outside.query_tree().children: +# if done_looping: break +# if inside.id == window_number: +# self.window = inside +# self.parent = outside +# done_looping = True +# break if window_number is None: print "Window %s not found. Skipping." % search_string return None - my_geo = self.window.get_geometry() - pa_geo = self.parent.get_geometry() +# my_geo = self.window.get_geometry() +# pa_geo = self.parent.get_geometry() +# +# self.x = pa_geo.x + my_geo.x +# self.y = pa_geo.y + my_geo.y +# self.width = my_geo.width +# self.height = my_geo.height +# self.exe = self.window.get_wm_class()[0] +# self.title = self.window.get_wm_name() +# self.site = "" +# self.hud = None - self.x = pa_geo.x + my_geo.x - self.y = pa_geo.y + my_geo.y - self.width = my_geo.width - self.height = my_geo.height - self.exe = self.window.get_wm_class()[0] - self.title = self.window.get_wm_name() - self.site = "" - self.hud = None - - window_string = str(self.window) +# window_string = str(self.window) mo = re.match('Xlib\.display\.Window\(([\dxabcdef]+)', window_string) if not mo: print "Not matched" From 04c09af7b2706d20cc21e74ea9252a6192921bce Mon Sep 17 00:00:00 2001 From: sqlcoder Date: Tue, 17 Nov 2009 21:47:43 +0000 Subject: [PATCH 3/7] tidy up aggregation code, NOTE: renamed some aggregation config to try and make meaning clearer, updated aggregation description in .example --- pyfpdb/Configuration.py | 12 ++-- pyfpdb/Database.py | 15 ++--- pyfpdb/HUD_config.xml.example | 116 ++++++++++++++++++---------------- pyfpdb/HUD_main.py | 35 ++-------- 4 files changed, 77 insertions(+), 101 deletions(-) diff --git a/pyfpdb/Configuration.py b/pyfpdb/Configuration.py index 3f183204..3fa5cc1d 100755 --- a/pyfpdb/Configuration.py +++ b/pyfpdb/Configuration.py @@ -361,16 +361,16 @@ class HudUI: self.node = node self.label = node.getAttribute('label') # + self.hud_style = node.getAttribute('stat_range') + self.hud_days = node.getAttribute('stat_days') self.aggregate_ring = string_to_bool(node.getAttribute('aggregate_ring_game_stats')) self.aggregate_tour = string_to_bool(node.getAttribute('aggregate_tourney_stats')) - self.hud_style = node.getAttribute('stat_aggregation_range') - self.hud_days = node.getAttribute('aggregation_days') self.agg_bb_mult = node.getAttribute('aggregation_level_multiplier') # - self.h_aggregate_ring = string_to_bool(node.getAttribute('aggregate_hero_ring_game_stats')) - self.h_aggregate_tour = string_to_bool(node.getAttribute('aggregate_hero_tourney_stats')) - self.h_hud_style = node.getAttribute('hero_stat_aggregation_range') - self.h_hud_days = node.getAttribute('hero_aggregation_days') + self.h_hud_style = node.getAttribute('hero_stat_range') + self.h_hud_days = node.getAttribute('hero_stat_days') + self.h_aggregate_ring = string_to_bool(node.getAttribute('aggregate_hero_ring_game_stats')) + self.h_aggregate_tour = string_to_bool(node.getAttribute('aggregate_hero_tourney_stats')) self.h_agg_bb_mult = node.getAttribute('hero_aggregation_level_multiplier') diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 0314d78d..07b7a4d4 100755 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -432,16 +432,14 @@ class Database: print "*** Database Error: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1]) def get_stats_from_hand( self, hand, type # type is "ring" or "tour" - , hud_params = {'aggregate_tour':False, 'aggregate_ring':False, 'hud_style':'A', 'hud_days':30, 'agg_bb_mult':100 - ,'h_aggregate_tour':False, 'h_aggregate_ring':False, 'h_hud_style':'S', 'h_hud_days':30, 'h_agg_bb_mult':100} + , hud_params = {'hud_style':'A', 'agg_bb_mult':1000 + ,'h_hud_style':'S', 'h_agg_bb_mult':1000} , hero_id = -1 ): - aggregate = hud_params['aggregate_tour'] if type == "tour" else hud_params['aggregate_ring'] hud_style = hud_params['hud_style'] - agg_bb_mult = hud_params['agg_bb_mult'] if aggregate else 1 - h_aggregate = hud_params['h_aggregate_tour'] if type == "tour" else hud_params['h_aggregate_ring'] + agg_bb_mult = hud_params['agg_bb_mult'] h_hud_style = hud_params['h_hud_style'] - h_agg_bb_mult = hud_params['h_agg_bb_mult'] if h_aggregate else 1 + h_agg_bb_mult = hud_params['h_agg_bb_mult'] stat_dict = {} if hud_style == 'S' or h_hud_style == 'S': @@ -468,13 +466,8 @@ class Database: #elif h_hud_style == 'H': # h_stylekey = date_nhands_ago needs array by player here ... - #if aggregate: always use aggregate query now: use agg_bb_mult of 1 for no aggregation: query = 'get_stats_from_hand_aggregated' subs = (hand, hero_id, stylekey, agg_bb_mult, agg_bb_mult, hero_id, h_stylekey, h_agg_bb_mult, h_agg_bb_mult) - #print "agg query subs:", subs - #else: - # query = 'get_stats_from_hand' - # subs = (hand, stylekey) #print "get stats: hud style =", hud_style, "query =", query, "subs =", subs c = self.connection.cursor() diff --git a/pyfpdb/HUD_config.xml.example b/pyfpdb/HUD_config.xml.example index 952a0dbf..b0f26c53 100644 --- a/pyfpdb/HUD_config.xml.example +++ b/pyfpdb/HUD_config.xml.example @@ -4,91 +4,95 @@ - Date: Thu, 19 Nov 2009 15:28:48 +0800 Subject: [PATCH 4/7] Add test functions for SQLite Added test for VARIANCE() Added test for floor() --- pyfpdb/test_Database.py | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 pyfpdb/test_Database.py diff --git a/pyfpdb/test_Database.py b/pyfpdb/test_Database.py new file mode 100644 index 00000000..9283f46d --- /dev/null +++ b/pyfpdb/test_Database.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +import sqlite3 +import fpdb_db +import math + +# Should probably use our wrapper classes - creating sqlite db in memory +sqlite3.register_converter("bool", lambda x: bool(int(x))) +sqlite3.register_adapter(bool, lambda x: "1" if x else "0") + +con = sqlite3.connect(":memory:") +con.isolation_level = None + +#Floor function +con.create_function("floor", 1, math.floor) + +#Mod function +tmp = fpdb_db.sqlitemath() +con.create_function("mod", 2, tmp.mod) + +# Aggregate function VARIANCE() +con.create_aggregate("variance", 1, fpdb_db.VARIANCE) + + +cur = con.cursor() + +def testSQLiteVarianceFunction(): + cur.execute("CREATE TABLE test(i)") + cur.execute("INSERT INTO test(i) values (1)") + cur.execute("INSERT INTO test(i) values (2)") + cur.execute("INSERT INTO test(i) values (3)") + cur.execute("SELECT variance(i) from test") + result = cur.fetchone()[0] + + print "DEBUG: Testing variance function" + print "DEBUG: result: %s expecting: 0.666666 (result-expecting ~= 0.0): %s" % (result, (result - 0.66666)) + cur.execute("DROP TABLE test") + assert (result - 0.66666) <= 0.0001 + +def testSQLiteFloorFunction(): + vars = [0.1, 1.5, 2.6, 3.5, 4.9] + cur.execute("CREATE TABLE test(i float)") + for var in vars: + cur.execute("INSERT INTO test(i) values(%f)" % var) + cur.execute("SELECT floor(i) from test") + result = cur.fetchall() + print "DEBUG: result: %s" % result + answer = 0 + for i in result: + print "DEBUG: int(var): %s" % int(i[0]) + assert answer == int(i[0]) + answer = answer + 1 From eadd7c045c996c5e1963138593be6222068b4239 Mon Sep 17 00:00:00 2001 From: Worros Date: Thu, 19 Nov 2009 17:22:53 +0800 Subject: [PATCH 5/7] Adding broken mod() test --- pyfpdb/test_Database.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pyfpdb/test_Database.py b/pyfpdb/test_Database.py index 9283f46d..d7fcc43c 100644 --- a/pyfpdb/test_Database.py +++ b/pyfpdb/test_Database.py @@ -49,3 +49,18 @@ def testSQLiteFloorFunction(): print "DEBUG: int(var): %s" % int(i[0]) assert answer == int(i[0]) answer = answer + 1 + cur.execute("DROP TABLE test") + +def testSQLiteModFunction(): + vars = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ,17, 18] + cur.execute("CREATE TABLE test(i float)") + for var in vars: + cur.execute("INSERT INTO test(i) values(%i)" % var) + cur.execute("SELECT mod(i,13) from test") + result = cur.fetchall() + answer = 0 + for i in result: + print "DEBUG: int(var): %s" % i[0] + assert answer == i[0] + answer = answer + 1 + cur.execute("DROP TABLE test") From 33592fbc4edff30fb8ba72e6068c44842f4af5f8 Mon Sep 17 00:00:00 2001 From: Worros Date: Wed, 18 Nov 2009 00:12:24 +0800 Subject: [PATCH 6/7] Add math function for sqlite - may still be broken --- pyfpdb/fpdb_db.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pyfpdb/fpdb_db.py b/pyfpdb/fpdb_db.py index 27291d7f..f3dcd681 100644 --- a/pyfpdb/fpdb_db.py +++ b/pyfpdb/fpdb_db.py @@ -20,6 +20,7 @@ import os import re import sys import logging +import math from time import time, strftime from Exceptions import * @@ -53,6 +54,10 @@ class VARIANCE: def finalize(self): return float(var(self.store)) +class sqlitemath: + def mod(self, a, b): + return a%b + class fpdb_db: MYSQL_INNODB = 2 PGSQL = 3 @@ -148,6 +153,9 @@ class fpdb_db: , detect_types=sqlite3.PARSE_DECLTYPES ) sqlite3.register_converter("bool", lambda x: bool(int(x))) sqlite3.register_adapter(bool, lambda x: "1" if x else "0") + self.db.create_function("floor", 1, math.floor) + tmp = sqlitemath() + self.db.create_function("mod", 2, tmp.mod) if use_numpy: self.db.create_aggregate("variance", 1, VARIANCE) else: From ea323d0d221d282283498f51a338913caba85d1a Mon Sep 17 00:00:00 2001 From: Worros Date: Thu, 19 Nov 2009 17:33:12 +0800 Subject: [PATCH 7/7] Fix mod() test for sqlite --- pyfpdb/test_Database.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pyfpdb/test_Database.py b/pyfpdb/test_Database.py index d7fcc43c..b348f741 100644 --- a/pyfpdb/test_Database.py +++ b/pyfpdb/test_Database.py @@ -53,14 +53,16 @@ def testSQLiteFloorFunction(): def testSQLiteModFunction(): vars = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ,17, 18] - cur.execute("CREATE TABLE test(i float)") + cur.execute("CREATE TABLE test(i int)") for var in vars: cur.execute("INSERT INTO test(i) values(%i)" % var) cur.execute("SELECT mod(i,13) from test") result = cur.fetchall() - answer = 0 + idx = 0 for i in result: print "DEBUG: int(var): %s" % i[0] - assert answer == i[0] - answer = answer + 1 + assert vars[idx]%13 == int(i[0]) + idx = idx+1 + + assert 0 == 1 cur.execute("DROP TABLE test")