def statistics_war(year):
    player_q = """SELECT
    player_name,
    team_id,
    pos,
    inn
    FROM statistics_fielding
    WHERE year = %s;
    """
    player_qry = player_q % (year)
    player_data = db.query(player_qry)

    entries = []
    for row in player_data:
        entry = {}
        entry['year'] = year
        player_name, team_id, position, inn = row
        entry['player_name'] = player_name

        search_name = player_name.replace("'","''")

        lookuptable = 'teams'
        team_abb = db.lookupValues("teams",("team_id","year",),(team_id,year),val="team_abb",operators=("=","="))[0]
        entry['team_abb'] = team_abb

        entry['position'] = position
        if position.lower() == 'p':
            continue
        else:

            # entry['bats'] = None
            
            entry['age'] = None

            entry['pa'] = None
            entry['inn'] = inn

            rn_val, err_val, arm_val, pb_val = helper.get_def_values(search_name, position, year)

            #1450 innings is a full season
            defense = float(inn)*(rn_val + err_val + arm_val + pb_val)/1450

            entry['defense'] = defense
            adj = float(helper.get_pos_adj(position.upper()))
            position_adj = adj*(float(inn)/1450)
            entry['position_adj'] = position_adj

            dwar = (defense+position_adj)/10

            entry['dWAR'] = dwar

            entries.append(entry)

    table = 'processed_compWAR_defensive'
    if entries != []: 
        db.insertRowDict(entries, table, replace=True, insertMany=True, rid=0)
    db.conn.commit()
def register_war(year):
    player_q = """SELECT
    player_name,
    team_abb,
    position,
    age,
    pa
    FROM register_batting_primary
    JOIN register_batting_secondary USING (year, player_name, team_abb, position, age)
    JOIN register_batting_analytical USING (year, player_name, team_abb, position, age)
    WHERE year = %s;
    """
    player_qry = player_q % (year)
    player_data = db.query(player_qry)

    entries = []
    for row in player_data:
        entry = {}
        entry['year'] = year
        player_name, team_abb, position, age, pa = row
        pa = float(pa)
        entry['player_name'] = player_name
        entry['team_abb'] = team_abb
        entry['position'] = position

        # bats = helper.get_hand(player_name)
        # entry['bats'] = bats

        if player_name[-1] not in ('*', '#'):
            s_name = player_name
        else:
            s_name = player_name[:len(player_name)-1]

        
        entry['age'] = age
        entry['pa'] = pa
        entry['inn'] = None

        if year < 2011:
            defense = 0.0
            entry['defense'] = defense

            adj = float(helper.get_pos_adj(position.upper()))
            position_adj = adj*(pa/600)
            entry['position_adj'] = position_adj

        else:
            # changes Travis d'Arnoud to Travis d''Arnoud
            search_name = s_name.replace("'","''")
            rn_val, err_val, arm_val, pb_val = helper.get_def_values(search_name, position, year)

            #600 pa is a full season
            defense = float(pa)*(rn_val + err_val + arm_val + pb_val)/600

            entry['defense'] = defense
            adj = float(helper.get_pos_adj(position.upper()))
            position_adj = adj*(float(pa)/600)
            entry['position_adj'] = position_adj

            # if player_name.lower() == 'derek jeter':
                # print rn_val, err_val, arm_val, pb_val
                # raw_input(entry)
            

        dwar = (defense+position_adj)/10.0

        entry['dWAR'] = dwar

        entries.append(entry)


    table = 'processed_compWAR_defensive'
    if entries != []: 
        db.insertRowDict(entries, table, replace=True, insertMany=True, rid=0)
    db.conn.commit()