User:Fizzixx/Hero Parser.py

infilename = "heroes.txt" outfilename = "table.txt"

class HeroClass(object): def __init__(self): self.name = "" self.rating = 0 self.primary = "???" self.ranged_or_melee = "???" self.roles = []

possible_roles = ["carry", "disabler", "durable", "escape", "initiator", "jungler", "lane-support", "nuker", "pusher", "support"]

def rating_colour(rating):			# What background colour for the various difficulty ratings if rating <= 0: return "#ffffff" elif rating <= 16: return "#ddffdd" elif rating <= 22: return "#ffffdd" else: return "#ffdddd"

rorm_format_dict = {				# What formatting (bold, italics) for the different hero types "ranged" : "'''", "melee" : "''", "???" : ""	}

primary_colour_dict = {				# What font colour for the different primary attributes "agility" : "#009900", "intelligence" : "#0066FF", "strength" : "#993300", "???" : "#000000"	}

infile = open(infilename)

heroes = []

while 1: data = infile.readline if data == "":							# Will be true only if EOF break

data = data.split						# Convert the string into a list of tokens if data == []:							# Skip empty lines continue

# Make a new hero and then parse the line for it... heroes.append(HeroClass) for x in data: if x == "...": pass elif len(x) == 4 and x[0] == "(" and x[3] == ")": try: heroes[-1].rating = int(x[1] + x[2]) except ValueError: pass elif x in ["melee", "ranged"]: heroes[-1].ranged_or_melee = x		elif x in ["agility", "strength", "intelligence"]: heroes[-1].primary = x		elif x in possible_roles: heroes[-1].roles.append(x) else: if heroes[-1].name != "": heroes[-1].name += " " heroes[-1].name += x


 * 1) Commence outfile writing...

outfile = open(outfilename, "w")

outfile.write("{| class=\"wikitable sortable\"\n|-\n! Hero !! RBE !! Type !! Att !! Car !! Dis !! Dur !! Esc !! Ini !! Jun !! Lan !! Nuk !! Pus !! Sup\n|-\n")

for hero in heroes: outfile.write("| " + hero.name + "") bgcolor = rating_colour(hero.rating) if hero.rating > 0: outfile.write(" || style=\"background: %s\" | " % bgcolor + str(hero.rating)) else: outfile.write(" || style=\"background: %s\" | " % bgcolor + "??") outfile.write(" || "		+ rorm_format_dict[hero.ranged_or_melee]			# i.e. ' or  to make the text bold or italics		+ hero.ranged_or_melee.capitalize		+ rorm_format_dict[hero.ranged_or_melee]		) outfile.write(" || " + "%s " %		(primary_colour_dict[hero.primary], hero.primary[0:3].capitalize)	) for role in possible_roles: if role in hero.roles: outfile.write(" || " + role[0:3]) else: outfile.write(" || ")

outfile.write("\n|-\n") outfile.write("|}\n")