Source

metaljoe_codekata / kata4 / part2_refactored.py

"""
http://codekata.pragprog.com/2007/01/kata_four_data_.html
"""

import re

from common import parse_file, get_item_with_smallest_difference


WHITESPACE_RE = re.compile( "\s+" )

TEAM_NAME     = 2
GOALS_FOR     = 7
GOALS_AGAINST = 9


def extract_football_record( line ):
    """ Extracts a tuple of fields if a valid football data record
        Returns None if not
    """
    columns = WHITESPACE_RE.split( line )
    if len( columns ) == 12:
        return columns
    return None


if __name__ == "__main__":
    data = parse_file( "football.dat", extract_football_record )
    
    print get_item_with_smallest_difference( data, TEAM_NAME, GOALS_FOR, GOALS_AGAINST )