def printCurrentGame(game): print("\nCURRENT GAME INFO\n=================\n") current_inning = mlbgame.game_events(game.game_id)[-1] players = mlbgame.players(game.game_id) playerlist = players.home_players + players.away_players if len(current_inning.bottom) > 0: print("Bottom " + str(current_inning) + "\n") last_ab = current_inning.bottom[-1] elif len(current_inning.top) > 0: print("Top " + str(current_inning) + "\n") last_ab = current_inning.top[-1] else: print("Upcoming " + str(current_inning) + "\n") current_inning = mlbgame.game_events(game.game_id)[-2] last_ab = current_inning.bottom[-1] pitcher = getPlayer(last_ab.pitcher, playerlist) batter = getPlayer(last_ab.batter, playerlist) pitcherStatus = pitcher.team_abbrev + " " + "Pitching: " + pitcher.first + " " + pitcher.last + ", ERA: " + ( "%.2f" % pitcher.era) batterStatus = batter.team_abbrev + " " + "Batting: " + batter.first + " " + batter.last + ", AVG: " + ( "%.3f" % batter.avg)[1:] print(pitcherStatus) print(batterStatus) if (last_ab.b1 or last_ab.b2 or last_ab.b3): base_status = "Runners on: " else: base_status = "No Runners On" if last_ab.b1: base_status += "First " if last_ab.b2: base_status += "Second " if last_ab.b3: base_status += "Third" print("\n" + str(last_ab.o) + " Out(s), " + base_status + "\n") print("SCORE\n=====\n" + game.home_team + ": " + str(game.home_team_runs) + "\n" + game.away_team + ": " + str(game.away_team_runs))
def printCurrentGame(game): print("\nCURRENT GAME INFO\n=================\n") current_inning = mlbgame.game_events(game.game_id)[-1] players = mlbgame.players(game.game_id) playerlist = players.home_players + players.away_players if len(current_inning.bottom) > 0: print("Bottom " + str(current_inning) + "\n") last_ab = current_inning.bottom[-1] elif len(current_inning.top) > 0: print("Top " + str(current_inning) + "\n") last_ab = current_inning.top[-1] else : print("Upcoming " + str(current_inning) + "\n") current_inning = mlbgame.game_events(game.game_id)[-2] last_ab = current_inning.bottom[-1] pitcher = getPlayer(last_ab.pitcher, playerlist) batter = getPlayer(last_ab.batter, playerlist) pitcherStatus = pitcher.team_abbrev + " " + "Pitching: " + pitcher.first + " " + pitcher.last + ", ERA: " + ("%.2f"%pitcher.era) batterStatus = batter.team_abbrev + " " + "Batting: " + batter.first + " " + batter.last + ", AVG: " + ("%.3f"%batter.avg)[1:] print(pitcherStatus) print(batterStatus) if(last_ab.b1 or last_ab.b2 or last_ab.b3): base_status = "Runners on: " else : base_status = "No Runners On" if last_ab.b1: base_status += "First " if last_ab.b2: base_status += "Second " if last_ab.b3: base_status += "Third" print("\n" + str(last_ab.o) + " Out(s), " + base_status + "\n") print("SCORE\n=====\n" + game.home_team+ ": " + str(game.home_team_runs) + "\n" + game.away_team + ": " + str(game.away_team_runs))
def test_players_empty(self): self.assertRaises(ValueError, lambda: mlbgame.players('game_id')) self.assertRaises(ValueError, lambda: mlbgame.players('2016_08_02_nymlb_nymlb_1'))
def test_players(self): players = mlbgame.players('2016_08_02_nyamlb_nynmlb_1') coaches = players.home_coaches + players.away_coaches umpires = players.umpires players = players.home_players + players.away_players for coach in coaches: self.assertIsInstance(coach.first, str) self.assertIsInstance(coach.id, int) self.assertIsInstance(coach.last, str) self.assertIsInstance(coach.num, (int, str)) self.assertIsInstance(coach.position, str) for player in players: self.assertIsInstance(player.avg, float) self.assertIsInstance(player.bats, str) self.assertIsInstance(player.boxname, str) self.assertIsInstance(player.first, str) self.assertIsInstance(player.hr, int) self.assertIsInstance(player.id, int) self.assertIsInstance(player.last, str) self.assertIsInstance(player.num, (int, str)) self.assertIsInstance(player.parent_team_abbrev, str) self.assertIsInstance(player.parent_team_id, int) self.assertIsInstance(player.position, str) self.assertIsInstance(player.rbi, int) self.assertIsInstance(player.rl, str) self.assertIsInstance(player.status, str) self.assertIsInstance(player.team_abbrev, str) self.assertIsInstance(player.team_id, int) for ump in umpires: self.assertIsInstance(ump.first, str) self.assertIsInstance(ump.id, int) self.assertIsInstance(ump.last, str) self.assertIsInstance(ump.name, str) self.assertIsInstance(ump.position, str) coach = coaches[0] player = players[0] ump = umpires[0] self.assertEqual(coach.first, 'Terry') self.assertEqual(coach.id, 492632) self.assertEqual(coach.last, 'Collins') self.assertEqual(coach.num, 10) self.assertEqual(coach.position, 'manager') self.assertEqual(player.avg, 0.079) self.assertEqual(player.bats, 'R') self.assertEqual(player.boxname, 'Colon') self.assertEqual(player.era, 3.58) self.assertEqual(player.first, 'Bartolo') self.assertEqual(player.hr, 1) self.assertEqual(player.id, 112526) self.assertEqual(player.last, 'Colon') self.assertEqual(player.losses, 6) self.assertEqual(player.num, 40) self.assertEqual(player.parent_team_abbrev, 'NYM') self.assertEqual(player.parent_team_id, 121) self.assertEqual(player.position, 'P') self.assertEqual(player.rbi, 2) self.assertEqual(player.rl, 'R') self.assertEqual(player.status, 'A') self.assertEqual(player.team_abbrev, 'NYM') self.assertEqual(player.team_id, 121) self.assertEqual(player.wins, 9) self.assertEqual(ump.first, 'Brian') self.assertEqual(ump.id, 427192) self.assertEqual(ump.last, 'Gorman') self.assertEqual(ump.name, 'Brian Gorman') self.assertEqual(ump.position, 'home')
#!/usr/bin/env python import mlbgame import re def rename(name): return re.sub(r'_', ' ', name).title() game = mlbgame.day(2017, 7, 9, away='Orioles')[0] players = mlbgame.players(game.game_id) print(game.home_team + ' vs ' + game.away_team + ' (' + str(game.date) + ')') types = ['home', 'away'] for type in types: print(' ' + getattr(game, type + '_team') + ' Information:') team_players = getattr(players, type + '_players') team_coaches = getattr(players, type + '_coaches') print(' Coaching Staff:') for coach in team_coaches: print(' {0}: {first} {last}'.format(rename(coach['position']), **coach)) print(' Starting Lineup:') starting_lineup = list(filter(lambda x: 'bat_order' in x.keys(), team_players)) for player in sorted(starting_lineup, key=lambda k: k['bat_order']): print(' {bat_order}. {first} {last} ({game_position})'.format(**player)) print(' Umpires:') for umpire in players.umpires: print(' {0}: {first} {last}'.format(rename(umpire['position']), **umpire))