Merge branch 'master' of git://git.assembla.com/fpdboz

Conflicts:

	pyfpdb/Database.py
This commit is contained in:
Ray 2009-03-12 12:13:03 -04:00
commit 2ac4e26337
9 changed files with 227 additions and 174 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: iso-8859-15 -*- # -*- coding: utf-8 -*-
#
# Copyright 2008, Carl Gherardi # Copyright 2008, Carl Gherardi
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
@ -18,139 +19,155 @@
######################################################################## ########################################################################
import sys import sys
import Configuration import logging
from HandHistoryConverter import * from HandHistoryConverter import *
# Betfair HH format # Betfair HH format
#***** Betfair Poker Hand History for Game 466894418 *****
#NL $0.02/$0.04 Texas Hold'em - Tuesday, January 13, 18:58:55 GMT 2009
#Table Rookie 102 6-max (Real Money)
#Seat 1 is the button
#Total number of active players : 3
#Seat 1: wilco16 ( $5.12 )
#Seat 2: fredo2003 ( $6.12 )
#Seat 4: Didlidaa ( $3.61 )
#Seat 5: Heck ( $4.47 )
#Seat 6: MoDDe200009 ( $0 )
#Didlidaa posts big blind [$0.04]
#** Dealing down cards **
#Heck calls [$0.04]
#wilco16 calls [$0.04]
#Didlidaa checks
#** Dealing Flop ** [ 2c, Kd, Jh ]
#Didlidaa checks
#Heck checks
#wilco16 checks
#** Dealing Turn ** [ 5c ]
#Didlidaa checks
#Heck bets [$0.12]
#wilco16 folds
#Didlidaa calls [$0.12]
#** Dealing River ** [ Kc ]
#Didlidaa checks
#Heck bets [$0.18]
#Didlidaa calls [$0.18]
#** Showdown **
#Heck shows [ Qs, 6c ] a pair of Kings
#Didlidaa shows [ Ad, 5h ] two pair, Kings and Fives
#** Hand Conclusion **
#Didlidaa wins $0.69 from main pot with two pair, Kings and Fives
#************ Game 466894418 ends ************
class Betfair(HandHistoryConverter): class Betfair(HandHistoryConverter):
def __init__(self, config, file):
print "Initialising Betfair converter class" # Static regexes
HandHistoryConverter.__init__(self, config, file, sitename="Betfair") # Call super class init. re_GameInfo = re.compile("^(?P<LIMIT>NL|PL|) (?P<CURRENCY>\$|)?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+) (?P<GAME>(Texas Hold\'em|Omaha Hi|Razz))", re.MULTILINE)
self.sitename = "Betfair" re_SplitHands = re.compile(r'\n\n+')
self.setFileType("text", "cp1252") re_HandInfo = re.compile("\*\*\*\*\* Betfair Poker Hand History for Game (?P<HID>[0-9]+) \*\*\*\*\*\n(?P<LIMIT>NL|PL|) (?P<CURRENCY>\$|)?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+) (?P<GAMETYPE>(Texas Hold\'em|Omaha Hi|Razz)) - (?P<DATETIME>[a-zA-Z]+, [a-zA-Z]+ \d+, \d\d:\d\d:\d\d GMT \d\d\d\d)\nTable (?P<TABLE>[ a-zA-Z0-9]+) \d-max \(Real Money\)\nSeat (?P<BUTTON>[0-9]+)", re.MULTILINE)
self.rexx.setGameInfoRegex('.*Blinds \$?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+)') re_Button = re.compile(ur"^Seat (?P<BUTTON>\d+) is the button", re.MULTILINE)
self.rexx.setSplitHandRegex('\n\n+') re_PlayerInfo = re.compile("Seat (?P<SEAT>[0-9]+): (?P<PNAME>.*)\s\(\s(\$(?P<CASH>[.0-9]+)) \)")
self.rexx.setHandInfoRegex('.*#(?P<HID>[0-9]+)\n.*\nBlinds \$?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+) (?P<GAMETYPE>.*) - (?P<DATETIME>\d\d\d\d/\d\d/\d\d - \d\d:\d\d:\d\d)\nTable (?P<TABLE>[ a-zA-Z]+)\nSeat (?P<BUTTON>[0-9]+)') re_Board = re.compile(ur"\[ (?P<CARDS>.+) \]")
self.rexx.setPlayerInfoRegex('Seat (?P<SEAT>[0-9]+): (?P<PNAME>.*) \(\s+(\$ (?P<CASH>[.0-9]+) USD|new player|All-in) \)')
self.rexx.setPostSbRegex('.*\n(?P<PNAME>.*): posts small blind \[\$? (?P<SB>[.0-9]+)') def __init__(self, in_path = '-', out_path = '-', follow = False, autostart=True):
self.rexx.setPostBbRegex('.*\n(?P<PNAME>.*): posts big blind \[\$? (?P<BB>[.0-9]+)') """\
self.rexx.setPostBothRegex('.*\n(?P<PNAME>.*): posts small \& big blinds \[\$? (?P<SBBB>[.0-9]+)') in_path (default '-' = sys.stdin)
self.rexx.setHeroCardsRegex('.*\nDealt\sto\s(?P<PNAME>.*)\s\[ (?P<CARDS>.*) \]') out_path (default '-' = sys.stdout)
self.rexx.setActionStepRegex('.*\n(?P<PNAME>.*)(?P<ATYPE>: bets| checks| raises| calls| folds)(\s\[\$ (?P<BET>[.\d]+) (USD|EUR)\])?') follow : whether to tail -f the input"""
self.rexx.setShowdownActionRegex('.*\n(?P<PNAME>.*) shows \[ (?P<CARDS>.*) \]') HandHistoryConverter.__init__(self, in_path, out_path, sitename="Betfair", follow=follow) # Call super class init.
self.rexx.setCollectPotRegex('.*\n(?P<PNAME>.*) wins \$ (?P<POT>[.\d]+) (USD|EUR)(.*?\[ (?P<CARDS>.*?) \])?') logging.info("Initialising Betfair converter class")
#self.rexx.setCollectPotRegex('.*\n(?P<PNAME>.*) wins \$ (?P<POT>[.\d]+) USD(.*\[ (?P<CARDS>) \S\S, \S\S, \S\S, \S\S, \S\S \])?') self.filetype = "text"
self.rexx.sits_out_re = re.compile('(?P<PNAME>.*) sits out') self.codepage = "cp1252"
self.rexx.compileRegexes() if autostart:
self.start()
def compilePlayerRegexs(self, hand):
players = set([player[1] for player in hand.players])
if not players <= self.compiledPlayers: # x <= y means 'x is subset of y'
# we need to recompile the player regexs.
self.compiledPlayers = players
player_re = "(?P<PNAME>" + "|".join(map(re.escape, players)) + ")"
logging.debug("player_re: " + player_re)
self.re_PostSB = re.compile("^%s posts small blind \[\$?(?P<SB>[.0-9]+)" % player_re, re.MULTILINE)
self.re_PostBB = re.compile("^%s posts big blind \[\$?(?P<BB>[.0-9]+)" % player_re, re.MULTILINE)
self.re_Antes = re.compile("^%s antes asdf sadf sadf" % player_re, re.MULTILINE)
self.re_BringIn = re.compile("^%s antes asdf sadf sadf" % player_re, re.MULTILINE)
self.re_PostBoth = re.compile("^%s posts small \& big blinds \[\$?(?P<SBBB>[.0-9]+)" % player_re, re.MULTILINE)
self.re_HeroCards = re.compile("^Dealt to %s \[ (?P<CARDS>.*) \]" % player_re, re.MULTILINE)
self.re_Action = re.compile("^%s (?P<ATYPE>bets|checks|raises to|raises|calls|folds)(\s\[\$(?P<BET>[.\d]+)\])?" % player_re, re.MULTILINE)
self.re_ShowdownAction = re.compile("^%s shows \[ (?P<CARDS>.*) \]" % player_re, re.MULTILINE)
self.re_CollectPot = re.compile("^%s wins \$(?P<POT>[.\d]+) (.*?\[ (?P<CARDS>.*?) \])?" % player_re, re.MULTILINE)
self.re_SitsOut = re.compile("^%s sits out" % player_re, re.MULTILINE)
self.re_ShownCards = re.compile(r"%s (?P<SEAT>[0-9]+) (?P<CARDS>adsfasdf)" % player_re, re.MULTILINE)
def readSupportedGames(self): def readSupportedGames(self):
pass return [["ring", "hold", "nl"]
]
def determineGameType(self): def determineGameType(self, handText):
# Cheating with this regex, only support nlhe at the moment info = {'type':'ring'}
gametype = ["ring", "hold", "nl"]
m = self.rexx.game_info_re.search(self.obs) m = self.re_GameInfo.search(handText)
gametype = gametype + [m.group('SB')] if not m:
gametype = gametype + [m.group('BB')] logging.info('GameInfo regex did not match')
return None
return gametype mg = m.groupdict()
# translations from captured groups to our info strings
limits = { 'NL':'nl', 'PL':'pl', 'Limit':'fl' }
games = { # base, category
"Texas Hold'em" : ('hold','holdem'),
'Omaha Hi' : ('hold','omahahi'),
'Razz' : ('stud','razz'),
'7 Card Stud' : ('stud','studhi')
}
currencies = { u'':'EUR', '$':'USD', '':'T$' }
if 'LIMIT' in mg:
info['limitType'] = limits[mg['LIMIT']]
if 'GAME' in mg:
(info['base'], info['category']) = games[mg['GAME']]
if 'SB' in mg:
info['sb'] = mg['SB']
if 'BB' in mg:
info['bb'] = mg['BB']
if 'CURRENCY' in mg:
info['currency'] = currencies[mg['CURRENCY']]
# NB: SB, BB must be interpreted as blinds or bets depending on limit type.
return info
def readHandInfo(self, hand): def readHandInfo(self, hand):
m = self.rexx.hand_info_re.search(hand.string) m = self.re_HandInfo.search(hand.handText)
if(m == None):
logging.info("Didn't match re_HandInfo")
logging.info(hand.handText)
return None
logging.debug("HID %s, Table %s" % (m.group('HID'), m.group('TABLE')))
hand.handid = m.group('HID') hand.handid = m.group('HID')
hand.tablename = m.group('TABLE') hand.tablename = m.group('TABLE')
# These work, but the info is already in the Hand class - should be used for tourneys though. hand.starttime = time.strptime(m.group('DATETIME'), "%A, %B %d, %H:%M:%S GMT %Y")
# m.group('SB') #hand.buttonpos = int(m.group('BUTTON'))
# m.group('BB')
# m.group('GAMETYPE')
# Betfair HH files in GMT
# Stars format (Nov 10 2008): 2008/11/07 12:38:49 CET [2008/11/07 7:38:49 ET]
# or : 2008/11/07 12:38:49 ET
# Not getting it in my HH files yet, so using
# 2008/11/10 3:58:52 ET
#TODO: Do conversion from GMT to ET
#TODO: Need some date functions to convert to different timezones (Date::Manip for perl rocked for this)
hand.starttime = time.strptime(m.group('DATETIME'), "%Y/%m/%d - %H:%M:%S")
hand.buttonpos = int(m.group('BUTTON'))
def readPlayerStacks(self, hand): def readPlayerStacks(self, hand):
m = self.rexx.player_info_re.finditer(hand.string) m = self.re_PlayerInfo.finditer(hand.handText)
players = []
for a in m: for a in m:
hand.addPlayer(int(a.group('SEAT')), a.group('PNAME'), a.group('CASH')) hand.addPlayer(int(a.group('SEAT')), a.group('PNAME'), a.group('CASH'))
def markStreets(self, hand): #Shouldn't really dip into the Hand object, but i've no idea how to tell the length of iter m
# PREFLOP = ** Dealing down cards ** if len(hand.players) < 2:
# This re fails if, say, river is missing; then we don't get the ** that starts the river. logging.info("readPlayerStacks: Less than 2 players found in a hand")
#m = re.search('(\*\* Dealing down cards \*\*\n)(?P<PREFLOP>.*?\n\*\*)?( Dealing Flop \*\* \[ (?P<FLOP1>\S\S), (?P<FLOP2>\S\S), (?P<FLOP3>\S\S) \])?(?P<FLOP>.*?\*\*)?( Dealing Turn \*\* \[ (?P<TURN1>\S\S) \])?(?P<TURN>.*?\*\*)?( Dealing River \*\* \[ (?P<RIVER1>\S\S) \])?(?P<RIVER>.*)', hand.string,re.DOTALL)
def markStreets(self, hand):
m = re.search(r"\*\* Dealing down cards \*\*(?P<PREFLOP>.+(?=\*\* Dealing Flop \*\*)|.+)" m = re.search(r"\*\* Dealing down cards \*\*(?P<PREFLOP>.+(?=\*\* Dealing Flop \*\*)|.+)"
r"(\*\* Dealing Flop \*\*(?P<FLOP> \[ \S\S, \S\S, \S\S \].+(?=\*\* Dealing Turn \*\*)|.+))?" r"(\*\* Dealing Flop \*\*(?P<FLOP> \[ \S\S, \S\S, \S\S \].+(?=\*\* Dealing Turn \*\*)|.+))?"
r"(\*\* Dealing Turn \*\*(?P<TURN> \[ \S\S \].+(?=\*\* Dealing River \*\*)|.+))?" r"(\*\* Dealing Turn \*\*(?P<TURN> \[ \S\S \].+(?=\*\* Dealing River \*\*)|.+))?"
r"(\*\* Dealing River \*\*(?P<RIVER> \[ \S\S \].+))?", hand.string,re.DOTALL) r"(\*\* Dealing River \*\*(?P<RIVER> \[ \S\S \].+))?", hand.handText,re.DOTALL)
hand.addStreets(m) hand.addStreets(m)
def readCommunityCards(self, hand, street): # street has been matched by markStreets, so exists in this hand def readCommunityCards(self, hand, street): # street has been matched by markStreets, so exists in this hand
self.rexx.board_re = re.compile(r"\[ (?P<CARDS>.+) \]")
print hand.streets.group(street)
if street in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP) if street in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP)
m = self.rexx.board_re.search(hand.streets.group(street)) m = self.re_Board.search(hand.streets[street])
hand.setCommunityCards(street, m.group('CARDS').split(', ')) hand.setCommunityCards(street, m.group('CARDS').split(', '))
def readBlinds(self, hand): def readBlinds(self, hand):
try: try:
m = self.rexx.small_blind_re.search(hand.string) m = self.re_PostSB.search(hand.handText)
hand.addBlind(m.group('PNAME'), 'small blind', m.group('SB')) hand.addBlind(m.group('PNAME'), 'small blind', m.group('SB'))
except: # no small blind except: # no small blind
hand.addBlind(None, None, None) hand.addBlind(None, None, None)
for a in self.rexx.big_blind_re.finditer(hand.string): for a in self.re_PostBB.finditer(hand.handText):
hand.addBlind(a.group('PNAME'), 'big blind', a.group('BB')) hand.addBlind(a.group('PNAME'), 'big blind', a.group('BB'))
for a in self.rexx.both_blinds_re.finditer(hand.string): for a in self.re_PostBoth.finditer(hand.handText):
hand.addBlind(a.group('PNAME'), 'small & big blinds', a.group('SBBB')) hand.addBlind(a.group('PNAME'), 'small & big blinds', a.group('SBBB'))
def readAntes(self, hand):
logging.debug("reading antes")
for player in m:
logging.debug("hand.addAnte(%s,%s)" %(player.group('PNAME'), player.group('ANTE')))
hand.addAnte(player.group('PNAME'), player.group('ANTE'))
def readBringIn(self, hand):
m = self.re_BringIn.search(hand.handText,re.DOTALL)
if m:
logging.debug("Player bringing in: %s for %s" %(m.group('PNAME'), m.group('BRINGIN')))
hand.addBringIn(m.group('PNAME'), m.group('BRINGIN'))
else:
logging.warning("No bringin found")
def readButton(self, hand):
hand.buttonpos = int(self.re_Button.search(hand.handText).group('BUTTON'))
def readHeroCards(self, hand): def readHeroCards(self, hand):
m = self.rexx.hero_cards_re.search(hand.string) m = self.re_HeroCards.search(hand.handText)
if(m == None): if(m == None):
#Not involved in hand #Not involved in hand
hand.involved = False hand.involved = False
@ -159,51 +176,66 @@ class Betfair(HandHistoryConverter):
# "2c, qh" -> set(["2c","qc"]) # "2c, qh" -> set(["2c","qc"])
# Also works with Omaha hands. # Also works with Omaha hands.
cards = m.group('CARDS') cards = m.group('CARDS')
cards = set(cards.split(', ')) cards = [c.strip() for c in cards.split(',')]
hand.addHoleCards(cards, m.group('PNAME')) hand.addHoleCards(cards, m.group('PNAME'))
def readStudPlayerCards(self, hand, street):
# balh blah blah
pass
def readAction(self, hand, street): def readAction(self, hand, street):
m = self.rexx.action_re.finditer(hand.streets.group(street)) m = self.re_Action.finditer(hand.streets[street])
for action in m: for action in m:
if action.group('ATYPE') == ' raises': if action.group('ATYPE') == 'raises to':
hand.addCallandRaise( street, action.group('PNAME'), action.group('BET') ) hand.addRaiseTo( street, action.group('PNAME'), action.group('BET') )
elif action.group('ATYPE') == ' calls': # elif action.group('ATYPE') == ' completes it to':
# hand.addComplete( street, action.group('PNAME'), action.group('BET') )
elif action.group('ATYPE') == 'calls':
hand.addCall( street, action.group('PNAME'), action.group('BET') ) hand.addCall( street, action.group('PNAME'), action.group('BET') )
elif action.group('ATYPE') == ': bets': elif action.group('ATYPE') == 'bets':
hand.addBet( street, action.group('PNAME'), action.group('BET') ) hand.addBet( street, action.group('PNAME'), action.group('BET') )
elif action.group('ATYPE') == ' folds': elif action.group('ATYPE') == 'folds':
hand.addFold( street, action.group('PNAME')) hand.addFold( street, action.group('PNAME'))
elif action.group('ATYPE') == ' checks': elif action.group('ATYPE') == 'checks':
hand.addCheck( street, action.group('PNAME')) hand.addCheck( street, action.group('PNAME'))
else: else:
print "DEBUG: unimplemented readAction: %s %s" %(action.group('PNAME'),action.group('ATYPE'),) print "DEBUG: unimplemented readAction: '%s' '%s'" %(action.group('PNAME'),action.group('ATYPE'),)
def readShowdownActions(self, hand): def readShowdownActions(self, hand):
for shows in self.rexx.showdown_action_re.finditer(hand.string): for shows in self.re_ShowdownAction.finditer(hand.handText):
cards = shows.group('CARDS') cards = shows.group('CARDS')
cards = set(cards.split(', ')) cards = cards.split(', ')
hand.addShownCards(cards, shows.group('PNAME')) hand.addShownCards(cards, shows.group('PNAME'))
def readCollectPot(self,hand): def readCollectPot(self,hand):
for m in self.rexx.collect_pot_re.finditer(hand.string): for m in self.re_CollectPot.finditer(hand.handText):
hand.addCollectPot(player=m.group('PNAME'),pot=m.group('POT')) hand.addCollectPot(player=m.group('PNAME'),pot=m.group('POT'))
def readShownCards(self,hand): def readShownCards(self,hand):
for m in self.rexx.collect_pot_re.finditer(hand.string): for m in self.re_ShownCards.finditer(hand.handText):
if m.group('CARDS') is not None: if m.group('CARDS') is not None:
cards = m.group('CARDS') cards = m.group('CARDS')
cards = set(cards.split(', ')) cards = cards.split(', ')
hand.addShownCards(cards=None, player=m.group('PNAME'), holeandboard=cards) hand.addShownCards(cards=None, player=m.group('PNAME'), holeandboard=cards)
if __name__ == "__main__": if __name__ == "__main__":
c = Configuration.Config() parser = OptionParser()
if len(sys.argv) == 1: parser.add_option("-i", "--input", dest="ipath", help="parse input hand history", default="regression-test-files/betfair/befair.02.04.txt")
testfile = "regression-test-files/betfair/befair.02.04.txt" parser.add_option("-o", "--output", dest="opath", help="output translation to", default="-")
else: parser.add_option("-f", "--follow", dest="follow", help="follow (tail -f) the input", action="store_true", default=False)
testfile = sys.argv[1] parser.add_option("-q", "--quiet",
e = Betfair(c, testfile) action="store_const", const=logging.CRITICAL, dest="verbosity", default=logging.INFO)
e.processFile() parser.add_option("-v", "--verbose",
print str(e) action="store_const", const=logging.INFO, dest="verbosity")
parser.add_option("--vv",
action="store_const", const=logging.DEBUG, dest="verbosity")
(options, args) = parser.parse_args()
LOG_FILENAME = './logging.out'
logging.basicConfig(filename=LOG_FILENAME,level=options.verbosity)
e = Betfair(in_path = options.ipath, out_path = options.opath, follow = options.follow)

View File

@ -158,7 +158,7 @@ class Database:
if cv not in d or d[cv] == None: if cv not in d or d[cv] == None:
break break
elif d[cv] == 0: elif d[cv] == 0:
cards = "%sxx" % cards cards += "xx"
else: else:
cs = "card%dSuit" % i cs = "card%dSuit" % i
cards = "%s%s%s" % (cards, ranks[d[cv]], d[cs]) cards = "%s%s%s" % (cards, ranks[d[cv]], d[cs])

View File

@ -50,6 +50,7 @@ debugging: if False, pass on partially supported game types. If true, have a go
self.debugging = debugging self.debugging = debugging
if autostart: if autostart:
self.start() self.start()
# otherwise you need to call start yourself.
def compilePlayerRegexs(self, hand): def compilePlayerRegexs(self, hand):
players = set([player[1] for player in hand.players]) players = set([player[1] for player in hand.players])
@ -133,7 +134,7 @@ or None if we fail to get the info """
if not self.debugging and info['base']=='stud': if not self.debugging and info['base']=='stud':
logging.warning("Not processing Everleaf Stud hand") logging.warning("Not processing Everleaf Stud hand")
return None #return None
return info return info

View File

@ -75,7 +75,6 @@ follow : whether to tail -f the input"""
] ]
def determineGameType(self, handText): def determineGameType(self, handText):
# Cheating with this regex, only support nlhe at the moment
# Full Tilt Poker Game #10777181585: Table Deerfly (deep 6) - $0.01/$0.02 - Pot Limit Omaha Hi - 2:24:44 ET - 2009/02/22 # Full Tilt Poker Game #10777181585: Table Deerfly (deep 6) - $0.01/$0.02 - Pot Limit Omaha Hi - 2:24:44 ET - 2009/02/22
# Full Tilt Poker Game #10773265574: Table Butte (6 max) - $0.01/$0.02 - Pot Limit Hold'em - 21:33:46 ET - 2009/02/21 # Full Tilt Poker Game #10773265574: Table Butte (6 max) - $0.01/$0.02 - Pot Limit Hold'em - 21:33:46 ET - 2009/02/21
# Full Tilt Poker Game #9403951181: Table CR - tay - $0.05/$0.10 - No Limit Hold'em - 9:40:20 ET - 2008/12/09 # Full Tilt Poker Game #9403951181: Table CR - tay - $0.05/$0.10 - No Limit Hold'em - 9:40:20 ET - 2008/12/09

View File

@ -102,10 +102,6 @@ If a player has None chips he won't be added."""
else: else:
logging.error("markstreets didn't match") logging.error("markstreets didn't match")
#def addHoleCards -- to Holdem subclass
def checkPlayerExists(self,player): def checkPlayerExists(self,player):
if player not in [p[1] for p in self.players]: if player not in [p[1] for p in self.players]:
print "checkPlayerExists", player, "fail" print "checkPlayerExists", player, "fail"

View File

@ -18,7 +18,6 @@
import Hand import Hand
import re import re
import sys import sys
import threading
import traceback import traceback
import logging import logging
from optparse import OptionParser from optparse import OptionParser
@ -72,10 +71,9 @@ letter2names = {
import gettext import gettext
gettext.install('myapplication') gettext.install('myapplication')
class HandHistoryConverter(threading.Thread): class HandHistoryConverter():
READ_CHUNK_SIZE = 1000 # bytes to read at a time from file READ_CHUNK_SIZE = 1000 # bytes to read at a time from file
def __init__(self, in_path = '-', out_path = '-', sitename = None, follow=False): def __init__(self, in_path = '-', out_path = '-', sitename = None, follow=False):
threading.Thread.__init__(self)
logging.info("HandHistory init called") logging.info("HandHistory init called")
# default filetype and codepage. Subclasses should set these properly. # default filetype and codepage. Subclasses should set these properly.
@ -90,7 +88,7 @@ class HandHistoryConverter(threading.Thread):
else: else:
# TODO: out_path should be sanity checked before opening. Perhaps in fpdb_import? # TODO: out_path should be sanity checked before opening. Perhaps in fpdb_import?
# I'm not sure what we're looking for, although we don't want out_path==in_path!='-' # I'm not sure what we're looking for, although we don't want out_path==in_path!='-'
self.out_fh = open(self.out_path, 'a') #TODO: append may be overly conservative. self.out_fh = open(self.out_path, 'w') # doomswitch is now on :|
self.sitename = sitename self.sitename = sitename
self.follow = follow self.follow = follow
self.compiledPlayers = set() self.compiledPlayers = set()
@ -110,7 +108,7 @@ class HandHistoryConverter(threading.Thread):
#tmp = tmp + "\tsb/bb: '%s/%s'\n" % (self.gametype[3], self.gametype[4]) #tmp = tmp + "\tsb/bb: '%s/%s'\n" % (self.gametype[3], self.gametype[4])
return tmp return tmp
def run(self): def start(self):
"""process a hand at a time from the input specified by in_path. """process a hand at a time from the input specified by in_path.
If in follow mode, wait for more data to turn up. If in follow mode, wait for more data to turn up.
Otherwise, finish at eof...""" Otherwise, finish at eof..."""
@ -118,6 +116,7 @@ Otherwise, finish at eof..."""
if not self.sanityCheck(): if not self.sanityCheck():
print "Cowardly refusing to continue after failed sanity check" print "Cowardly refusing to continue after failed sanity check"
return return
if self.follow: if self.follow:
numHands = 0 numHands = 0
for handText in self.tailHands(): for handText in self.tailHands():
@ -131,13 +130,16 @@ Otherwise, finish at eof..."""
numHands= len(handsList) numHands= len(handsList)
endtime = time.time() endtime = time.time()
print "Processed %d hands in %.3f seconds" % (numHands, endtime - starttime) print "Processed %d hands in %.3f seconds" % (numHands, endtime - starttime)
if self.out_fh != sys.stdout:
self.out_fh.close()
def tailHands(self): def tailHands(self):
"""Generator of handTexts from a tailed file: """Generator of handTexts from a tailed file:
Tail the in_path file and yield handTexts separated by re_SplitHands""" Tail the in_path file and yield handTexts separated by re_SplitHands"""
if in_path == '-': raise StopIteration if self.in_path == '-': raise StopIteration
interval = 1.0 # seconds to sleep between reads for new data interval = 1.0 # seconds to sleep between reads for new data
fd = open(filename,'r') fd = codecs.open(self.in_path,'r', self.codepage)
data = '' data = ''
while 1: while 1:
where = fd.tell() where = fd.tell()
@ -145,15 +147,15 @@ Tail the in_path file and yield handTexts separated by re_SplitHands"""
if not newdata: if not newdata:
fd_results = os.fstat(fd.fileno()) fd_results = os.fstat(fd.fileno())
try: try:
st_results = os.stat(filename) st_results = os.stat(self.in_path)
except OSError: except OSError:
st_results = fd_results st_results = fd_results
if st_results[1] == fd_results[1]: if st_results[1] == fd_results[1]:
time.sleep(interval) time.sleep(interval)
fd.seek(where) fd.seek(where)
else: else:
print "%s changed inode numbers from %d to %d" % (filename, fd_results[1], st_results[1]) print "%s changed inode numbers from %d to %d" % (self.in_path, fd_results[1], st_results[1])
fd = open(filename, 'r') fd = codecs.open(self.in_path, 'r', self.codepage)
fd.seek(where) fd.seek(where)
else: else:
# yield hands # yield hands
@ -202,17 +204,24 @@ Tail the in_path file and yield handTexts separated by re_SplitHands"""
def processHand(self, handText): def processHand(self, handText):
gametype = self.determineGameType(handText) gametype = self.determineGameType(handText)
logging.debug("gametype %s" % gametype) logging.debug("gametype %s" % gametype)
if gametype is None:
return
# See if gametype is supported.
type = gametype['type']
base = gametype['base']
limit = gametype['limitType']
l = [type] + [base] + [limit]
hand = None hand = None
if gametype['base'] == 'hold': if l in self.readSupportedGames():
logging.debug("hand = Hand.HoldemOmahaHand(self, self.sitename, gametype, handtext)") hand = None
hand = Hand.HoldemOmahaHand(self, self.sitename, gametype, handText) if gametype['base'] == 'hold':
elif gametype['base'] == 'stud': logging.debug("hand = Hand.HoldemOmahaHand(self, self.sitename, gametype, handtext)")
hand = Hand.StudHand(self, self.sitename, gametype, handText) hand = Hand.HoldemOmahaHand(self, self.sitename, gametype, handText)
elif gametype['base'] == 'draw': elif gametype['base'] == 'stud':
hand = Hand.DrawHand(self, self.sitename, gametype, handText) hand = Hand.StudHand(self, self.sitename, gametype, handText)
elif gametype['base'] == 'draw':
hand = Hand.DrawHand(self, self.sitename, gametype, handText)
else:
logging.info("Unsupported game type: %s" % gametype)
if hand: if hand:
hand.writeHand(self.out_fh) hand.writeHand(self.out_fh)
@ -221,6 +230,7 @@ Tail the in_path file and yield handTexts separated by re_SplitHands"""
# TODO: pity we don't know the HID at this stage. Log the entire hand? # TODO: pity we don't know the HID at this stage. Log the entire hand?
# From the log we can deduce that it is the hand after the one before :) # From the log we can deduce that it is the hand after the one before :)
# These functions are parse actions that may be overridden by the inheriting class # These functions are parse actions that may be overridden by the inheriting class
# This function should return a list of lists looking like: # This function should return a list of lists looking like:
# return [["ring", "hold", "nl"], ["tour", "hold", "nl"]] # return [["ring", "hold", "nl"], ["tour", "hold", "nl"]]

View File

@ -200,7 +200,7 @@ class Importer:
self.updated[file] = time() self.updated[file] = time()
# If modified in the last minute run an immediate import. # If modified in the last minute run an immediate import.
# This codepath only runs first time the file is found. # This codepath only runs first time the file is found.
if (time() - stat_info.st_mtime) < 60: if os.path.isdir(file) or (time() - stat_info.st_mtime) < 60:
# TODO attach a HHC thread to the file # TODO attach a HHC thread to the file
# TODO import the output of the HHC thread -- this needs to wait for the HHC to block? # TODO import the output of the HHC thread -- this needs to wait for the HHC to block?
self.import_file_dict(file, self.filelist[file][0], self.filelist[file][1]) self.import_file_dict(file, self.filelist[file][0], self.filelist[file][1])
@ -229,32 +229,26 @@ class Importer:
# Load filter, process file, pass returned filename to import_fpdb_file # Load filter, process file, pass returned filename to import_fpdb_file
# TODO: Shouldn't we be able to use some sort of lambda or something to just call a Python object by whatever name we specify? then we don't have to hardcode them, # TODO: Shouldn't we be able to use some sort of lambda or something to just call a Python object by whatever name we specify? then we don't have to hardcode them,
print "converting %s" % file
hhbase = self.config.get_import_parameters().get("hhArchiveBase")
hhbase = os.path.expanduser(hhbase)
hhdir = os.path.join(hhbase,site)
try:
out_path = os.path.join(hhdir, file.split(os.path.sep)[-2]+"-"+os.path.basename(file))
except:
out_path = os.path.join(hhdir, "x"+strftime("%d-%m-%y")+os.path.basename(file))
# someone can just create their own python module for it # someone can just create their own python module for it
if filter in ("EverleafToFpdb","Everleaf"): if filter in ("EverleafToFpdb","Everleaf"):
print "converting ", file
hhbase = self.config.get_import_parameters().get("hhArchiveBase")
hhbase = os.path.expanduser(hhbase)
hhdir = os.path.join(hhbase,site)
try:
out_path = os.path.join(hhdir, file.split(os.path.sep)[-2]+"-"+os.path.basename(file))
except:
out_path = os.path.join(hhdir, "x"+strftime("%d-%m-%y")+os.path.basename(file))
#out_fh = open(ofile, 'w') # TODO: seek to previous place in input and append output
conv = EverleafToFpdb.Everleaf(in_path = file, out_path = out_path) conv = EverleafToFpdb.Everleaf(in_path = file, out_path = out_path)
conv.join()
elif filter == "FulltiltToFpdb": elif filter == "FulltiltToFpdb":
print "converting ", file conv = FulltiltToFpdb.FullTilt(in_path = file, out_path = out_path)
conv = FulltiltToFpdb.FullTilt(in_fh = file, out_fh = out_fh)
else: else:
print "Unknown filter ", filter print "Unknown filter ", filter
return return
supp = conv.readSupportedGames() # Should this be done by HHC on init?
#gt = conv.determineGameType()
# TODO: Check that gt is in supp - error appropriately if not
conv.processFile()
if(conv.getStatus()): if(conv.getStatus()):
(stored, duplicates, partial, errors, ttime) = self.import_fpdb_file(conv.getProcessedFile(), site) (stored, duplicates, partial, errors, ttime) = self.import_fpdb_file(out_path, site)
else: else:
# conversion didn't work # conversion didn't work
# TODO: appropriate response? # TODO: appropriate response?
@ -280,7 +274,7 @@ class Importer:
loc = self.pos_in_file[file] loc = self.pos_in_file[file]
except: except:
pass pass
print "DEBUG: XXXXX in import_fpdb_file"
# Read input file into class and close file # Read input file into class and close file
inputFile.seek(loc) inputFile.seek(loc)
self.lines=fpdb_simple.removeTrailingEOL(inputFile.readlines()) self.lines=fpdb_simple.removeTrailingEOL(inputFile.readlines())
@ -290,7 +284,7 @@ class Importer:
try: # sometimes we seem to be getting an empty self.lines, in which case, we just want to return. try: # sometimes we seem to be getting an empty self.lines, in which case, we just want to return.
firstline = self.lines[0] firstline = self.lines[0]
except: except:
# print "import_fpdb_file", file, site, self.lines, "\n" print "DEBUG: import_fpdb_file: failed on self.lines[0]: '%s' '%s' '%s' '%s' " %( file, site, self.lines, loc)
return (0,0,0,1,0) return (0,0,0,1,0)
if firstline.find("Tournament Summary")!=-1: if firstline.find("Tournament Summary")!=-1:

21
pyfpdb/test_Betfair.py Normal file
View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
import BetfairToFpdb
import py
def checkGameInfo(hhc, header, info):
assert hhc.determineGameType(header) == info
def testGameInfo():
hhc = BetfairToFpdb.Betfair(autostart=False)
pairs = (
(u"""***** Betfair Poker Hand History for Game 472386869 *****
NL $0.02/$0.04 Texas Hold'em - Sunday, January 25, 10:10:42 GMT 2009
Table Rookie 191 6-max (Real Money)
Seat 1 is the button
Total number of active players : 6""",
{'type':'ring', 'base':"hold", 'category':'holdem', 'limitType':'nl', 'sb':'0.02', 'bb':'0.04', 'currency':'USD'}),
)
for (header, info) in pairs:
yield checkGameInfo, hhc, header, info

View File

@ -3,17 +3,17 @@ import fpdb_simple
import datetime import datetime
import py import py
def checkDateParse(header, result): def checkDateParse(header, site, result):
assert fpdb_simple.parseHandStartTime(header) == result assert fpdb_simple.parseHandStartTime(header, site) == result
def testPokerStarsHHDate(): def testPokerStarsHHDate():
tuples = ( tuples = (
("PokerStars Game #21969660557: Hold'em No Limit ($0.50/$1.00) - 2008/11/12 10:00:48 CET [2008/11/12 4:00:48 ET]", ("PokerStars Game #21969660557: Hold'em No Limit ($0.50/$1.00) - 2008/11/12 10:00:48 CET [2008/11/12 4:00:48 ET]", "ps",
datetime.datetime(2008,9,7,11,23,14)),
("PokerStars Game #21969660557: Hold'em No Limit ($0.50/$1.00) - 2008/08/17 - 01:14:43 (ET)",
datetime.datetime(2008,11,12,15,00,48)), datetime.datetime(2008,11,12,15,00,48)),
("PokerStars Game #21969660557: Hold'em No Limit ($0.50/$1.00) - 2008/09/07 06:23:14 ET", ("PokerStars Game #21969660557: Hold'em No Limit ($0.50/$1.00) - 2008/08/17 - 01:14:43 (ET)", "ps",
datetime.datetime(2008,8,17,6,14,43)) datetime.datetime(2008,8,17,6,14,43)),
("PokerStars Game #21969660557: Hold'em No Limit ($0.50/$1.00) - 2008/09/07 06:23:14 ET", "ps",
datetime.datetime(2008,9,7,11,23,14))
) )
#def testFullTiltHHDate(self): #def testFullTiltHHDate(self):
@ -36,5 +36,5 @@ def testPokerStarsHHDate():
# self.failUnless(result == "French", "French (deep) parsed incorrectly. Expected 'French' got: " + str(result)) # self.failUnless(result == "French", "French (deep) parsed incorrectly. Expected 'French' got: " + str(result))
# result = ("French (deep) - $0.25/$0.50 - No Limit Hold'em - Logged In As xxxx") # result = ("French (deep) - $0.25/$0.50 - No Limit Hold'em - Logged In As xxxx")
for (header, result) in tuples: for (header, site, result) in tuples:
yield checkDateParse header, result yield checkDateParse, header, site, result