Lancer Knightz (Guild)/Guild Records

From SpiralKnights

Revision as of 14:31, 1 January 2013 by Heruncle (Talk | contribs)

Jump to: navigation, search

This short Python script is used by the guild Lancer Knightz to summarize its guild records. It seems to be far less capable than the solution here, but also simpler. To use it, paste the code below into a text editor, save the file as "guilddata.py", and then follow the instructions at the top of the file.

# To use this software, you must have some version of Python installed, 
# such as 2.7.x. Place this program file in the same directory as your 
# Spiral Knights guild record CSV file. Then, at the command line, type 
# python guilddata.py filename.csv
# where "filename.csv" is the name of your CSV file. You'll see a 
# printout of crowns, crystal energy, and mist energy transactions. 
# (Each section of the printout is prefaced by somewhat cryptic header 
# text. The program was made to be easily extended, at the expense of 
# some aesthetics.)

import re
import operator
import sys

# Given a regular expression string, a dictionary, and a string to search,
# extracts a key-value pair from the string using the regular expression,
# and updates the dictionary (adding to the value already there, if any).
def regexpIntoDict(regexp, dict, line):
    match = re.search(regexp, line)
    if match != None:
        name = match.groups()[0]
        amount = int((match.groups()[1]).replace(",", ""))
        if name in dict:
            dict[name] = dict[name] + amount
        else:
            dict[name] = amount
        return match

if len(sys.argv) != 2:
    print "error: python guilddata.py filename.csv expected"
else:
    # Define the quantities that we're interested in, as Python regular expressions.
    regexps = []
    regexps.append(r"""([^"]+)","Added ([0-9,]+) crowns""")
    regexps.append(r"""([^"]+)","Deposited ([0-9,]+) crystal energy""")
    regexps.append(r"""([^"]+)","Withdrew ([0-9,]+) mist energy""")
    # Define a dictionary to keep track of the guild records matching each regular expression.
    dictionaries = [{} for regexp in regexps]
    # Collect data from the CSV file into the dictionaries.
    file = open(sys.argv[1])
    line = file.readline()
    while line != "":
        for i in range(len(regexps)):
            if regexpIntoDict(regexps[i], dictionaries[i], line):
                break
        line = file.readline()
    file.close()
    # Convert each dictionary into a sorted list of tuples.
    lists = [sorted(dict.items(), key=operator.itemgetter(1), reverse=True) for dict in dictionaries]
    # Print each sorted result list.
    for i in range(len(regexps)):
        print regexps[i]
        for pair in lists[i]:
            print pair[0], pair[1]
        print

Here's an example of the output:

([^"]+)","Added ([0-9,]+) crowns
Donkeyhaute 50000
Vanzanz 44300
Dhalgren 30000
Gulliverbfg 17500
Fafaprince 13000
Knightware 12500
Davidrv 11333
Lakitoo 5712
Flabiesausage 4000
Tsiddique 2500
Preussenempire 2000
Zacharylim 1000
Silentnator 100

([^"]+)","Deposited ([0-9,]+) crystal energy
Bionicfusion 600
Vanzanz 600

([^"]+)","Withdrew ([0-9,]+) mist energy
Vanzanz 600
Knightware 234
Bionicfusion 100
Fafaprince 99
Jadjabbour 80
Realm-Of-Shadows 59
Silentnator 19
Sircheesyz 9
Personal tools