From 8b933bbd7b6ad1ed8a792e0ee8213ddb1f483d70 Mon Sep 17 00:00:00 2001 From: eblade Date: Wed, 18 Mar 2009 21:22:04 -0400 Subject: [PATCH] fpdb_import: formatting cleanup fpdb_simple: significant performance enhancements likely in checkPositions, convertCardValuesBoard, filterCrap, float2int, isActionLine, isWinLine --- pyfpdb/fpdb_import.py | 24 +-- pyfpdb/fpdb_simple.py | 336 ++++++++++++++++-------------------------- 2 files changed, 141 insertions(+), 219 deletions(-) diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index b7212f4a..9eaa1fcd 100644 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -51,22 +51,22 @@ class Importer: def __init__(self, caller, settings, config): """Constructor""" - self.settings=settings - self.caller=caller - self.config = config - self.fdb = None - self.cursor = None - self.filelist = {} - self.dirlist = {} + self.settings = settings + self.caller = caller + self.config = config + self.fdb = None + self.cursor = None + self.filelist = {} + self.dirlist = {} self.addToDirList = {} self.removeFromFileList = {} # to remove deleted files - self.monitor = False - self.updated = {} #Time last import was run {file:mtime} - self.lines = None - self.faobs = None #File as one big string + self.monitor = False + self.updated = {} #Time last import was run {file:mtime} + self.lines = None + self.faobs = None #File as one big string self.pos_in_file = {} # dict to remember how far we have read in the file #Set defaults - self.callHud = self.config.get_import_parameters().get("callFpdbHud") + self.callHud = self.config.get_import_parameters().get("callFpdbHud") if 'minPrint' not in self.settings: #TODO: Is this value in the xml file? self.settings['minPrint'] = 30 diff --git a/pyfpdb/fpdb_simple.py b/pyfpdb/fpdb_simple.py index 2677d11b..c47b6c86 100644 --- a/pyfpdb/fpdb_simple.py +++ b/pyfpdb/fpdb_simple.py @@ -18,14 +18,17 @@ #This file contains simple functions for fpdb import datetime +import time import re -PS=1 -FTP=2 +PS = 1 +FTP = 2 + +# TODO: these constants are also used in fpdb_save_to_db and others, is there a way to do like C #define, and #include ? +MYSQL_INNODB = 2 +PGSQL = 3 +SQLITE = 4 -MYSQL_INNODB=2 -PGSQL=3 -SQLITE=4 # Data Structures for index and foreign key creation # drop_code is an int with possible values: 0 - don't drop for bulk import # 1 - drop during bulk import @@ -384,27 +387,20 @@ def calcPayin(count, buyin, fee): result.append (buyin+fee) return result #end def calcPayin - + def checkPositions(positions): - """verifies that these positions are valid""" - for i in xrange(len(positions)): - pos=positions[i] - try:#todo: use type recognition instead of error - if (len(pos)!=1): - raise FpdbError("invalid position found in checkPositions. i: "+str(i)+" position: "+pos) #dont need to str() here - except TypeError:#->not string->is int->fine - pass - - ### RHH modified to allow for "position 9" here (pos==9 is when you're a dead hand before the BB - ### eric - position 8 could be valid - if only one blind is posted, but there's still 10 people, ie a sitout is present, and the small is dead... - if not (pos == "B" or pos == "S" or (pos >= 0 and pos <= 9)): - raise FpdbError("invalid position found in checkPositions. i: "+str(i)+" position: "+str(pos)) -#end def fpdb_simple.checkPositions + """ verify positions are valid """ + for p in positions: + if not (p == "B" or p == "S" or (p >= 0 and p <= 9)): + raise FpdbError("invalid position '" + p + "' found in checkPositions") + + ### RHH modified to allow for "position 9" here (pos==9 is when you're a dead hand before the BB + ### eric - position 8 could be valid - if only one blind is posted, but there's still 10 people, ie a sitout is present, and the small is dead... #classifies each line for further processing in later code. Manipulates the passed arrays. def classifyLines(hand, category, lineTypes, lineStreets): - currentStreet="predeal" - done=False #set this to true once we reach the last relevant line (the summary, except rake, is all repeats) + currentStreet = "predeal" + done = False #set this to true once we reach the last relevant line (the summary, except rake, is all repeats) for i, line in enumerate(hand): if done: if "[" not in line or "mucked [" not in line: @@ -518,19 +514,10 @@ def convertCardValues(arr): #converts the strings in the given array to ints (changes the passed array, no returning). see table design for conversion details def convertCardValuesBoard(arr): + # TODO: this could probably be useful in many places, it should be a constant somewhere maybe? + card_map = { "2": 2, "3" : 3, "4" : 4, "5" : 5, "6" : 6, "7" : 7, "8" : 8, "9" : 9, "T" : 10, "J" : 11, "Q" : 12, "K" : 13, "A" : 14} for i in xrange(len(arr)): - if (arr[i]=="A"): - arr[i]=14 - elif (arr[i]=="K"): - arr[i]=13 - elif (arr[i]=="Q"): - arr[i]=12 - elif (arr[i]=="J"): - arr[i]=11 - elif (arr[i]=="T"): - arr[i]=10 - else: - arr[i]=int(arr[i]) + arr[i] = card_map[arr[i]] #end def convertCardValuesBoard #this creates the 2D/3D arrays. manipulates the passed arrays instead of returning. @@ -544,10 +531,7 @@ def createArrays(category, seats, card_values, card_suits, antes, winnings, rake winnings.append(0) rakes.append(0) - if (category=="holdem" or category=="omahahi" or category=="omahahilo"): - streetCount=4 - else: - streetCount=5 + streetCount = 4 if category == "holdem" or category == "omahahi" or category == "omahahilo" else 5 for i in xrange(streetCount): #build the first dimension array, for streets tmp=[] @@ -569,15 +553,15 @@ def createArrays(category, seats, card_values, card_suits, antes, winnings, rake action_amounts[i].append(tmp) tmp=[] actionNos[i].append(tmp) - if (category=="holdem" or category=="omahahi" or category=="omahahilo"): - pass - elif (category=="razz" or category=="studhi" or category=="studhilo"):#need to fill card arrays. +# if (category=="holdem" or category=="omahahi" or category=="omahahilo"): +# pass + if category=="razz" or category=="studhi" or category=="studhilo":#need to fill card arrays. for i in xrange(seats): - for j in xrange (7): + for j in xrange(7): card_values[i].append(0) card_suits[i].append("x") - else: - raise FpdbError("invalid category") +# else: +# raise FpdbError("invalid category") #end def createArrays def fill_board_cards(board_values, board_suits): @@ -590,16 +574,16 @@ def fill_board_cards(board_values, board_suits): def fillCardArrays(player_count, base, category, card_values, card_suits): """fills up the two card arrays""" if (category=="holdem"): - cardCount=2 + cardCount = 2 elif (category=="omahahi" or category=="omahahilo"): - cardCount=4 + cardCount = 4 elif base=="stud": - cardCount=7 + cardCount = 7 else: - raise fpdb_simple.FpdbError ("invalid category:", category) + raise fpdb_simple.FpdbError("invalid category:", category) - for i in xrange (player_count): - while (len(card_values[i]) 0 +# ret = any(True for searchstr in ActionLines if searchstr in line) +# ret = len( [ x for x in ActionLines if line.find(x) > -1] ) > 0 +# ret = any(searchstr in line for searchstr in ActionLines) #end def isActionLine #returns whether this is a duplicate @@ -828,56 +795,14 @@ def isRebuyOrAddon(topline): #returns whether the passed topline indicates a tournament or not def isTourney(topline): - if (topline.find("Tournament")!=-1): - return True - else: - return False + return "Tournament" in topline #end def isTourney +WinLines = ( "wins the pot", "ties for the ", "wins side pot", "wins the low main pot", "wins the high main pot", + "wins the high pot", "wins the high side pot", "wins the main pot", "wins the side pot", "collected" ) #returns boolean whether the passed line is a win line def isWinLine(line): - if (line.find("wins the pot")!=-1): - return True - elif (line.find("ties for the high pot")!=-1): - return True - elif (line.find("ties for the high main pot")!=-1): - return True - elif (line.find("ties for the high side pot")!=-1): - return True - elif (line.find("ties for the low pot")!=-1): - return True - elif (line.find("ties for the low main pot")!=-1): - return True - elif (line.find("ties for the low side pot")!=-1): - return True - elif (line.find("ties for the main pot")!=-1): #for ftp tied main pot of split pot - return True - elif (line.find("ties for the pot")!=-1): #for ftp tie - return True - elif (line.find("ties for the side pot")!=-1): #for ftp tied split pots - return True - elif (line.find("wins side pot #")!=-1): #for ftp multi split pots - return True - elif (line.find("wins the low main pot")!=-1): - return True - elif (line.find("wins the low pot")!=-1): - return True - elif (line.find("wins the low side pot")!=-1): - return True - elif (line.find("wins the high main pot")!=-1): - return True - elif (line.find("wins the high pot")!=-1): - return True - elif (line.find("wins the high side pot")!=-1): - return True - elif (line.find("wins the main pot")!=-1): - return True - elif (line.find("wins the side pot")!=-1): #for ftp split pots - return True - elif (line.find("collected")!=-1): - return True - else: - return False #not raising error here, any unknown line wouldve been detected in isActionLine already + return len( [ x for x in WinLines if x in line ] ) > 0 #end def isWinLine #returns the amount of cash/chips put into the put in the given action line @@ -892,23 +817,20 @@ def parseActionAmount(line, atype, site, isTourney): if line.endswith(" and is capped"): line=line[:-14] - - if (atype=="fold"): - amount=0 - elif (atype=="check"): - amount=0 - elif (atype=="unbet" and site=="ftp"): - pos1=line.find("$")+1 - pos2=line.find(" returned to") - amount=float2int(line[pos1:pos2]) - elif (atype=="unbet" and site=="ps"): - #print "ps unbet, line:",line - pos1=line.find("$")+1 - if pos1==0: - pos1=line.find("(")+1 - pos2=line.find(")") - amount=float2int(line[pos1:pos2]) - elif (atype=="bet" and site=="ps" and line.find(": raises $")!=-1 and line.find("to $")!=-1): + if atype == "fold" or atype == "check": + amount = 0 + elif atype == "unbet": + if site == "ftp": + pos1 = line.find("$") + 1 + pos2 = line.find(" returned to") + amount = float2int(line[pos1:pos2]) + elif site == "ps": + pos1 = line.find("$") + 1 + if pos1 == 0: + pos1 = line.find("(") + 1 + pos2 = line.find(")") + amount = float2int(line[pos1:pos2]) + elif atype == "bet" and site == "ps" and line.find(": raises $")!=-1 and line.find("to $")!=-1: pos=line.find("to $")+4 amount=float2int(line[pos:]) else: