def from_mlb_api(cls, line_score, away_team=None, home_team=None, hide_spoilers=False ): columns = [ DataTableColumn("team", width=6, label="", align="right", padding=1), ] if "teams" in line_score: tk = line_score["teams"] else: tk = line_score data = [] for s, side in enumerate(["away", "home"]): line = AttrDict() if isinstance(line_score["innings"], list): for i, inning in enumerate(line_score["innings"]): if not s: columns.append( DataTableColumn(str(i+1), label=str(i+1), width=3) ) line.team = away_team else: line.team = home_team if hide_spoilers: setattr(line, str(i+1), "?") elif side in inning: if isinstance(inning[side], dict) and "runs" in inning[side]: setattr(line, str(i+1), parse_int(inning[side]["runs"])) else: if "runs" in inning[side]: inning_score.append(parse_int(inning[side])) else: setattr(line, str(i+1), "X") for n in range(i+1, 9): if not s: columns.append( DataTableColumn(str(n+1), label=str(n+1), width=3) ) if hide_spoilers: setattr(line, str(n+1), "?") if not s: columns.append( DataTableColumn("empty", label="", width=3) ) for stat in ["runs", "hits", "errors"]: if not stat in tk[side]: continue if not s: columns.append( DataTableColumn(stat, label=stat[0].upper(), width=3) ) if not hide_spoilers: setattr(line, stat, parse_int(tk[side][stat])) else: setattr(line, stat, "?") data.append(line) # raise Exception([c.name for c in columns]) return cls(columns, data=data)
def from_json(cls, line_score, away_team=None, home_team=None, hide_spoilers=False): columns = [ DataTableColumn("team", width=6, label="", align="right", padding=1), ] if "teams" in line_score: tk = line_score["teams"] else: tk = line_score data = [] for s, side in enumerate(["away", "home"]): i = -1 line = AttrDict() if "periods" in line_score and isinstance(line_score["periods"], list): for i, period in enumerate(line_score["periods"]): if not s: columns.append( DataTableColumn(str(i + 1), label=str(i + 1) if i < 3 else "O", width=3)) line.team = away_team else: line.team = home_team if hide_spoilers: setattr(line, str(i + 1), "?") elif side in period: if isinstance(period[side], dict) and "goals" in period[side]: setattr(line, str(i + 1), parse_int(period[side]["goals"])) else: setattr(line, str(i + 1), "X") for n in list(range(i + 1, 3)): if not s: columns.append( DataTableColumn(str(n + 1), label=str(n + 1), width=3)) if hide_spoilers: setattr(line, str(n + 1), "?") if not s: columns.append(DataTableColumn("empty", label="", width=3)) for stat in ["goals", "shotsOnGoal"]: if not stat in tk[side]: continue if not s: columns.append( DataTableColumn(stat, label=stat[0].upper(), width=3)) if not hide_spoilers: setattr(line, stat, parse_int(tk[side][stat])) else: setattr(line, stat, "?") data.append(line) return cls(columns, data=data)