All pastes #2054505 Raw Edit

Untitled

public python v1 · immutable
#2054505 ·published 2011-05-06 10:01 UTC
rendered paste body
"""	1. Place in directory with .xml draw hand history	2. Run	3. Observe converted .txt hand history	4. ???	5. Profit!"""import os, xml.etree.ElementTree as ET# find hand historiesfns = []for dir, dirs, fl in os.walk('.'):	for fn in fl:		if fn[-4:] == '.xml':			fns.append(fn)for fn in fns:	donk = open(fn, 'r').read()		# fix merge xml fuckup		desc = donk.split('\n')[0] # <description> header	donk = '<container>' + ''.join(donk.split('\n')[1:]) + '</container>' # wrap up <game>s		header = ET.XML(desc)	hh = ET.XML(donk)		# let's write		out = ''	for game in hh:				out += 'Merge %s %s %s - %d players\n' % (				''.join(header.get('stakes').rpartition(' ')[-1:]),				''.join(header.get('stakes').rpartition(' ')[:-2]),				header.get('type'),				len(game.find('players')))			name = {}		for p in game.find('players'):			#name[p.get('seat')] = p.get('nickname') # unanonymous			name[p.get('seat')] = 'Seat ' + p.get('seat') # anonymous			out += 'Seat %s: %s (%s)\n' % (				p.get('seat'),				name[p.get('seat')],				p.get('balance'))				for round in game.findall('round'):			out += '[b]%s[/b]\n' % round.get('id')			for evttype in ['event', 'cards']:				for event in round.findall(evttype):					try:						out += name[event.get('player')] + ' ' + event.get('type')					except:						print 'whoops can\'t import game %s in %s' % (game.get('id'), fn) # stupid game					for chips in ('text', 'amount', 'cards'):						if event.get(chips):							def tptcards(s):								tpts = {'s':':spade:', 'h':':heart:', 'd':':diamond:', 'c':':club:', ',':' '}								for c in tpts.keys():									if(s.rpartition(c)[0:2] != ('', '')):										return tptcards(s.rpartition(c)[0]) + tpts[c] + tptcards(s.rpartition(c)[2])								return s							out += ' ' + tptcards(event.get(chips))					out += '\n'			for winner in round.findall('winner'):				out += '%s wins %s\n' % (name[winner.get('player')],  winner.get('amount'))			out += '\n\n********\n\n'		open(fn[:-4] + '.txt', 'w').write(out)