In the following Python 3 code, the correct value is written into the daysSchedule
but when iterating to the next value.
class Data:
def getDaysSchedule(scheduleUrl, filterTeam = ''):
with urllib.request.urlopen(scheduleUrl) as request:
data = json.loads(request.read().decode())
daysSchedule = GameList()
for gameData in data["dates"][0]["games"]:
game = Game()
game.gameId = gameData["gamePk"]
game.home.name = gameData["teams"]["home"]["team"]["name"]
game.away.name = gameData["teams"]["away"]["team"]["name"]
print('\n' + str(game.gameId)) # CORRECT VALUE HERE
daysSchedule.games = daysSchedule.games[:] + [game] # HERE HAVE TRIED APPEND BUT THAT DIDN'T WORK EITHER
# DEBUG CODE
menuItems = daysSchedule.games
for testItem in menuItems:
testId = testItem.gameId
title = testItem.home.name + ' VS. ' + testItem.away.name
print(title)
return daysSchedule
I get output from the debug chunk of code above like the following. Each one of this is a write out of one of the rows. And you will see a new row gets added with correct values, but all the previous rows incorrectly get overwritten with this value too.
633855
Chicago Cubs VS. San Diego Padres
Chicago Cubs VS. San Diego Padres
633834
Arizona Diamondbacks VS. New York Mets
Arizona Diamondbacks VS. New York Mets
Arizona Diamondbacks VS. New York Mets
633853
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
633882
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
What in the world is going on -- besides me obviously not having enough caffeine today.
Here is the Games class
class Game(object):
gameId = ''
link = ''
date = ''
time = ''
status = Status()
home = Team()
away = Team()
series = Series()
venue = Venue()
... and the GameList class:
class GameList(object):
games = [Game()]