[Tutor] New programmer, need some help getting started on my first project
Kent Johnson
kent37 at tds.net
Thu May 18 01:13:24 CEST 2006
Chris Delgado wrote:
> Here is a sample hand history.
>
> Failure To Launch 8161071-72989 Holdem No Limit $0.50/$1
> [May 17 03:26:33] : Hand Start.
> [May 17 03:26:33] : Seat 1 : bnb3 has $92.50
> [May 17 03:26:33] : Seat 2 : pineaa has $15.25
> [May 17 03:26:33] : Seat 3 : prowlerslim has $107.50
> [May 17 03:26:33] : Seat 4 : Marcelluz has $174.74
> [May 17 03:26:33] : Seat 5 : texredfsh has $35.25
> [May 17 03:26:33] : Seat 6 : aloo has $98.37
> [May 17 03:26:33] : aloo is the dealer.
> [May 17 03:26:34] : bnb3 posted small blind.
> [May 17 03:26:34] : pineaa posted big blind.
> [May 17 03:26:34] : Game
...etc
>
> Im trying to work this out as we speak but not getting too far at the moment. I'll keep plugging
> along
OK, just looking at this, I'm guessing that you might want some kind of
data to represent the players, maybe something to represent the pot,
maybe something to represent the cards on the table. It really depends
on what kind of output you want to get from this. Can you post an
example of the desired output?
I can see sort of an event-driven parser where each line is an input
event. You could have a list of pairs of regular expressions and
functions. The code would run down the list of regexes, if one matches,
call the function passing it the match object. The function parses the
specific line and calls an event handler in an output object.
By separating the parser from the output handler, you can write a new
parser for a different input format and (in theory) use the same output
handler.
It's easier to show than to explain. Here is a simple example to parse
the data above:
import re
class Handler(object):
''' This class receives parsed events. It creates any
needed data structures and writes the desired output format.
This class is independent of the source format.
This version just prints the events; the real Handler will be
more interesting.
'''
def start(self):
print 'Game started'
def assign_seat(self, num, name, amt):
print '%s in seat %s has $%.2f' % (name, num, amt)
def set_dealer(self, dealer):
print 'dealer is %s' % dealer
def set_big_blind(self, player):
print 'big blind is %s' % player
def set_small_blind(self, player):
print 'small blind is %s' % player
class Parser(object):
''' This class parses the source data. It interprets the data
and generates callback events to the output handler.
This class doesn't know anything about the output format. '''
def __init__(self, handler):
self.handler = handler
self.regexes = [
(r'Hand Start', self.start),
(r'Seat (\d+) : (\w+) has \$([\d.]+)', self.assign_seat),
(r'(\w+) is the dealer', self.set_dealer),
(r'(\w+) posted small blind', self.set_small_blind),
(r'(\w+) posted big blind', self.set_big_blind),
]
def parse(self, lines):
for line in lines:
for regex, func in self.regexes:
match = re.search(regex, line)
if match:
func(match)
def start(self, match):
self.handler.start()
def assign_seat(self, match):
num, name, dollars = match.group(1, 2, 3)
num = int(num)
dollars = float(dollars)
self.handler.assign_seat(num, name, dollars)
def set_dealer(self, match):
self.handler.set_dealer(match.group(1))
def set_small_blind(self, match):
self.handler.set_small_blind(match.group(1))
def set_big_blind(self, match):
self.handler.set_big_blind(match.group(1))
# Some test data
data = '''Failure To Launch 8161071-72989 Holdem No Limit $0.50/$1
[May 17 03:26:33] : Hand Start.
[May 17 03:26:33] : Seat 1 : bnb3 has $92.50
[May 17 03:26:33] : Seat 2 : pineaa has $15.25
[May 17 03:26:33] : Seat 3 : prowlerslim has $107.50
[May 17 03:26:33] : Seat 4 : Marcelluz has $174.74
[May 17 03:26:33] : Seat 5 : texredfsh has $35.25
[May 17 03:26:33] : Seat 6 : aloo has $98.37
[May 17 03:26:33] : aloo is the dealer.
[May 17 03:26:34] : bnb3 posted small blind.
[May 17 03:26:34] : pineaa posted big blind.
[May 17 03:26:34] : Game
'''.splitlines()
# Make a handler
handler = Handler()
# Make a parser and feed it the data
Parser(handler).parse(data)
Kent
More information about the Tutor
mailing list