'''
Created on Oct 23, 2013

@author: rcd
'''
import operator
import collections
# a useful way to refer to the data within the program,
# names were taken from the data's header row
COLUMNS = 'mp fg fga p3 p3a ft fta orb drb ast stl blk tov pf'.split()
BBallStats = collections.namedtuple('BBallStats', COLUMNS)


def loadData (formattedLines):
    """
    given lines of basketball stats, separated by tabs, in the form:
      name yr mp fg fga p3 p3a ft fta orb drb ast stl blk tov pf
    return a dictionary whose
      keys are strings, player's name
      values are lists of numbers, all stat columns in file but yr
    """
    # initially empty dictionary to return
    players = { }
    # process each line in turn
    for line in formattedLines:
        # protect ourselves from incomplete lines of data
        if len(line) > 0:
            # transform data from single string to component pieces and types
            data = line.split('\t')
            name = data[0]
            stats = [ int(s) for s in data[2:] ]
            # first time seeing player, just assign stats directly
            if name not in players:
                players[name] = stats
            # player already in dictionary, add new stats to current stats
            else:
                currentStats = players[name]
                for (k,s) in enumerate(stats):
                    currentStats[k] += s
                # OR, as asked in class:
                # players[name] = [ sum(s) for s in zip(stats,players[name]) ]

    # now that data is done, make it easier for later functions to work with
    # by converting dictionary values from a simple list to a namedtuple 
    for name in players:
        players[name] = BBallStats._make(players[name])
    return players


def printSorted (players, column):
    """
    convenience function to print the data sorted based on a given column
    """
    header = ['name'] + COLUMNS
    toPrint = [ [k] + list(v) for (k,v) in players.items() ]
    for p in sorted(toPrint, key=operator.itemgetter(header.index(column))):
        print('\t'.join([ str(x) for x in p ]))






# read data line-by-line from the file,
# throw away the header row
f = open('duke_bball_stats.txt')
formattedData = f.read().split('\n')[1:]
f.close()

# load the data into a dictionary,
#   keys are strings, player's first and last name
#   values are lists of numbers, all stat columns in file but year
players = loadData(formattedData)
printSorted(players, 'name')
print('\n')
printSorted(players, 'mp')
