'''
Created on Oct 5, 2011

@author: rodger
'''
# must include these imports to use the file chooser
import os
import tkFileDialog

    
def choose_file_to_open():
    """
    prompt for existing file, return the file (open for reading)
    """
    file = tkFileDialog.askopenfile(title="choose a file", initialdir=os.getcwd())
    return file

def columnNumber(data, word):
    '''
    data is a list, return the position that word appears 
    in the list, Look for word as a substring.
    return -1 if not found
    '''
    for (i,v) in enumerate(data):
        #TODO:
        
        
        
        
    return -1

def processFile(file):
    '''
    process the lines in a .csv file, separator is ','
    return the file as a list of lists
    '''
    all = []
    for xline in file:
        line = xline.strip()
        newline = line.split(',')
        all.append(newline)
    return all
        

def getColumn(data, startpos):
    '''
    data is a list of lists, startpos is a (row, column) indicating 
       the position of a word in data
    return the rest of the column below startpos
    '''
    columnlist = []
    for num in range(startpos[0]+1,len(data)):
        #TODO
        columnlist.append(                                       )
    return columnlist

def getColumnRow(data, word):
    '''
    data is a list of lists, find first occurrence of word
    and return the row and column where word appears.
    return as a tuple (row,column)
    '''
    for (row,itemlist) in enumerate(data):            
        column = columnNumber(itemlist, word)
        #TODO:
        


    return 

def convertToNums(data):
    '''
    data is the complete column, and row is the first row with
    a valid data item. The data items are strings such as '4.3'
    but some of them are empty '' and must be ignored.
    return a list of floats of all the strings that are 
    valid numbers
    '''
    # TODO: return list comprehension
    return 

def getStatesWithRate(alldata, startpos, num):
    '''
    alldata is all of the data in a list of lists of strings
    startpos is the (row,column) position of the category,
    row+1 would be where the numbers start. 
    return a list of states (from column 0) that have num in them
    in column 'column'
    num is a float
    '''
    states = []
    for row in range(startpos[0]+1,len(alldata)):
        #TODO:



    return states
       
def checkPrintTwoColumns(alldata,col1, col2):
    '''
    print two of the columns in alldata (a list of lists).
    print the two columns one row at a time on a line
    '''
    print "Printing two columns"     
    for num in range(0,len(alldata)):
        print alldata[num][col1] + " " + alldata[num][col2]
         
      
    
def printStats(data,type):   
    '''
    type is a string defining what type of data this is
    '''
    print type +  " stats"
    print str(max(data)) + " is highest number"
    print str(min(data)) +  " is lowest number"
    
alldata = processFile(choose_file_to_open())
print alldata 
startpos = (row, column) = getColumnRow(alldata,"Murder")
oneColumn = getColumn(alldata, startpos)
oneColumnFloats = convertToNums(oneColumn)
print alldata
print startpos
maximum = max(oneColumnFloats)
minimum = min(oneColumnFloats)
print "Maximum: ",
print getStatesWithRate(alldata, startpos, maximum)
print "Minimum: ",
print getStatesWithRate(alldata, startpos, minimum)
checkPrintTwoColumns(alldata, 0, startpos[1])
