<s> """<STR_LIT>""" <EOL> import lxml . etree as etree <EOL> import datetime <EOL> import mlbgame . data <EOL> def scoreboard ( year , month , day , home = None , away = None ) : <EOL> """<STR_LIT>""" <EOL> data = mlbgame . data . get_scoreboard ( year , month , day ) <EOL> parsed = etree . parse ( data ) <EOL> root = parsed . getroot ( ) <EOL> games = { } <EOL> for game in root : <EOL> if game . tag == "<STR_LIT>" or game . tag == "<STR_LIT>" : <EOL> teams = game . findall ( '<STR_LIT>' ) <EOL> home_name = teams [ <NUM_LIT:0> ] . attrib [ '<STR_LIT:name>' ] <EOL> away_name = teams [ <NUM_LIT:1> ] . attrib [ '<STR_LIT:name>' ] <EOL> if ( home_name == home and home != None ) or ( away_name == away and away != None ) or ( away == None and home == None ) : <EOL> game_type = "<STR_LIT>" <EOL> game_data = game . find ( '<STR_LIT>' ) <EOL> game_id = game_data . attrib [ '<STR_LIT:id>' ] <EOL> game_league = game_data . attrib [ '<STR_LIT>' ] <EOL> game_status = game_data . attrib [ '<STR_LIT:status>' ] <EOL> game_start_time = game_data . attrib [ '<STR_LIT>' ] <EOL> home_team_data = teams [ <NUM_LIT:0> ] . find ( '<STR_LIT>' ) <EOL> home_team = { '<STR_LIT:name>' : home_name , '<STR_LIT>' : int ( home_team_data . attrib [ '<STR_LIT:R>' ] ) , '<STR_LIT>' : int ( home_team_data . attrib [ '<STR_LIT:H>' ] ) , '<STR_LIT>' : int ( home_team_data . attrib [ '<STR_LIT:E>' ] ) } <EOL> away_team_data = teams [ <NUM_LIT:1> ] . find ( '<STR_LIT>' ) <EOL> away_team = { '<STR_LIT:name>' : away_name , '<STR_LIT>' : int ( away_team_data . attrib [ '<STR_LIT:R>' ] ) , '<STR_LIT>' : int ( away_team_data . attrib [ '<STR_LIT:H>' ] ) , '<STR_LIT>' : int ( away_team_data . attrib [ '<STR_LIT:E>' ] ) } <EOL> try : <EOL> w_pitcher_data = game . find ( '<STR_LIT>' ) <EOL> w_pitcher_name = w_pitcher_data . find ( '<STR_LIT>' ) . attrib [ '<STR_LIT:name>' ] <EOL> w_pitcher = { '<STR_LIT:name>' : w_pitcher_name , '<STR_LIT>' : int ( w_pitcher_data . attrib [ '<STR_LIT>' ] ) , '<STR_LIT>' : int ( w_pitcher_data . attrib [ '<STR_LIT>' ] ) } <EOL> except : <EOL> w_pitcher = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> try : <EOL> l_pitcher_data = game . find ( '<STR_LIT>' ) <EOL> l_pitcher_name = l_pitcher_data . find ( '<STR_LIT>' ) . attrib [ '<STR_LIT:name>' ] <EOL> l_pitcher = { '<STR_LIT:name>' : l_pitcher_name , '<STR_LIT>' : int ( l_pitcher_data . attrib [ '<STR_LIT>' ] ) , '<STR_LIT>' : int ( l_pitcher_data . attrib [ '<STR_LIT>' ] ) } <EOL> except : <EOL> l_pitcher = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> try : <EOL> sv_pitcher_data = game . find ( '<STR_LIT>' ) <EOL> sv_pitcher_name = sv_pitcher_data . find ( '<STR_LIT>' ) . attrib [ '<STR_LIT:name>' ] <EOL> sv_pitcher = { '<STR_LIT:name>' : sv_pitcher_name , '<STR_LIT>' : int ( sv_pitcher_data . attrib [ '<STR_LIT>' ] ) } <EOL> except : <EOL> sv_pitcher = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } <EOL> output = { '<STR_LIT>' : game_id , '<STR_LIT>' : game_type , '<STR_LIT>' : game_league , '<STR_LIT>' : game_status , '<STR_LIT>' : game_start_time , '<STR_LIT>' : home_team , '<STR_LIT>' : away_team , '<STR_LIT>' : w_pitcher , '<STR_LIT>' : l_pitcher , '<STR_LIT>' : sv_pitcher } <EOL> games [ game_id ] = output <EOL> elif game . tag == "<STR_LIT>" : <EOL> teams = game . findall ( '<STR_LIT>' ) <EOL> home_name = teams [ <NUM_LIT:0> ] . attrib [ '<STR_LIT:name>' ] <EOL> away_name = teams [ <NUM_LIT:1> ] . attrib [ '<STR_LIT:name>' ] <EOL> if ( home_name == home and home != None ) or ( away_name == away and away != None ) or ( away == None and home == None ) : <EOL> game_type = "<STR_LIT>" <EOL> game_data = game . find ( '<STR_LIT>' ) <EOL> game_id = game_data . attrib [ '<STR_LIT:id>' ] <EOL> game_league = game_data . attrib [ '<STR_LIT>' ] <EOL> game_status = game_data . attrib [ '<STR_LIT:status>' ] <EOL> game_start_time = game_data . attrib [ '<STR_LIT>' ] <EOL> teams = game . findall ( '<STR_LIT>' ) <EOL> home_team_data = teams [ <NUM_LIT:0> ] . find ( '<STR_LIT>' ) <EOL> home_team = { '<STR_LIT:name>' : teams [ <NUM_LIT:0> ] . attrib [ '<STR_LIT:name>' ] , '<STR_LIT>' : int ( home_team_data . attrib [ '<STR_LIT:R>' ] ) , '<STR_LIT>' : int ( home_team_data . attrib [ '<STR_LIT:H>' ] ) , '<STR_LIT>' : int ( home_team_data . attrib [ '<STR_LIT:E>' ] ) } <EOL> away_team_data = teams [ <NUM_LIT:1> ] . find ( '<STR_LIT>' ) <EOL> away_team = { '<STR_LIT:name>' : teams [ <NUM_LIT:1> ] . attrib [ '<STR_LIT:name>' ] , '<STR_LIT>' : int ( away_team_data . attrib [ '<STR_LIT:R>' ] ) , '<STR_LIT>' : int ( away_team_data . attrib [ '<STR_LIT:H>' ] ) , '<STR_LIT>' : int ( away_team_data . attrib [ '<STR_LIT:E>' ] ) } <EOL> output = { '<STR_LIT>' : game_id , '<STR_LIT>' : game_type , '<STR_LIT>' : game_league , '<STR_LIT>' : game_status , '<STR_LIT>' : game_start_time , '<STR_LIT>' : home_team , '<STR_LIT>' : away_team , '<STR_LIT>' : { } , '<STR_LIT>' : { } , '<STR_LIT>' : { } } <EOL> games [ game_id ] = output <EOL> return games <EOL> class GameScoreboard ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . game_id = data [ '<STR_LIT>' ] <EOL> self . game_type = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . game_status = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . game_league = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . game_start_time = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . home_team = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . home_team_runs = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . home_team_hits = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . home_team_errors = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . away_team = data . get ( '<STR_LIT>' , '<STR_LIT>' ) . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . away_team_runs = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . away_team_hits = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . away_team_errors = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . w_pitcher = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . w_pitcher_wins = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . w_pitcher_losses = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . l_pitcher = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . l_pitcher_wins = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . l_pitcher_losses = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . sv_pitcher = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . sv_pitcher_saves = data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . w_team = '<STR_LIT>' <EOL> self . l_team = '<STR_LIT>' <EOL> if self . home_team_runs > self . away_team_runs : <EOL> self . w_team = self . home_team <EOL> self . l_team = self . away_team <EOL> elif self . away_team_runs > self . home_team_runs : <EOL> self . w_team = self . away_team <EOL> self . l_team = self . home_team <EOL> year , month , day , rest = self . game_id . split ( '<STR_LIT:_>' , <NUM_LIT:3> ) <EOL> hour , other = self . game_start_time . split ( '<STR_LIT::>' , <NUM_LIT:2> ) <EOL> minute = other [ : <NUM_LIT:2> ] <EOL> am_pm = other [ <NUM_LIT:2> : ] <EOL> if am_pm == "<STR_LIT>" : <EOL> hour = int ( hour ) + <NUM_LIT:11> <EOL> self . date = datetime . datetime ( int ( year ) , int ( month ) , int ( day ) , int ( hour ) , int ( minute ) ) <EOL> def nice_score ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . away_team , self . away_team_runs , self . home_team , self . home_team_runs ) <EOL> def __str__ ( self ) : <EOL> return self . nice_score ( ) <EOL> def box_score ( game_id ) : <EOL> """<STR_LIT>""" <EOL> data = mlbgame . data . get_box_score ( game_id ) <EOL> parsed = etree . parse ( data ) <EOL> root = parsed . getroot ( ) <EOL> linescore = root . find ( '<STR_LIT>' ) <EOL> result = { } <EOL> result [ '<STR_LIT>' ] = game_id <EOL> for x in linescore : <EOL> inning = x . attrib [ '<STR_LIT>' ] <EOL> home = x . attrib [ '<STR_LIT>' ] <EOL> away = x . attrib [ '<STR_LIT>' ] <EOL> result [ int ( inning ) ] = { '<STR_LIT>' : home , '<STR_LIT>' : away } <EOL> return result <EOL> class GameBoxScore ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . game_id = data [ '<STR_LIT>' ] <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> self . innings = [ ] <EOL> for x in sorted ( data ) : <EOL> try : <EOL> result = { '<STR_LIT>' : int ( x ) , '<STR_LIT>' : int ( data [ x ] [ '<STR_LIT>' ] ) , '<STR_LIT>' : int ( data [ x ] [ '<STR_LIT>' ] ) } <EOL> except ValueError : <EOL> result = { '<STR_LIT>' : int ( x ) , '<STR_LIT>' : data [ x ] [ '<STR_LIT>' ] , '<STR_LIT>' : int ( data [ x ] [ '<STR_LIT>' ] ) } <EOL> self . innings . append ( result ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for x in self . innings : <EOL> yield x <EOL> def print_scoreboard ( self ) : <EOL> """<STR_LIT>""" <EOL> output = '<STR_LIT>' <EOL> innings = [ ] <EOL> away = [ ] <EOL> home = [ ] <EOL> for x in self : <EOL> innings . append ( x [ '<STR_LIT>' ] ) <EOL> away . append ( x [ '<STR_LIT>' ] ) <EOL> home . append ( x [ '<STR_LIT>' ] ) <EOL> output += "<STR_LIT>" <EOL> for x in innings : <EOL> output += str ( x ) + "<STR_LIT:U+0020>" <EOL> output += '<STR_LIT:\n>' <EOL> for x in innings : <EOL> output += "<STR_LIT>" <EOL> output += "<STR_LIT>" <EOL> for y , x in enumerate ( away , start = <NUM_LIT:1> ) : <EOL> if y >= <NUM_LIT:10> : <EOL> output += str ( x ) + "<STR_LIT:U+0020>" <EOL> else : <EOL> output += str ( x ) + "<STR_LIT:U+0020>" <EOL> output += "<STR_LIT>" <EOL> for y , x in enumerate ( home , start = <NUM_LIT:1> ) : <EOL> if y >= <NUM_LIT:10> : <EOL> output += str ( x ) + "<STR_LIT:U+0020>" <EOL> else : <EOL> output += str ( x ) + "<STR_LIT:U+0020>" <EOL> return output </s>
