tl;dr: read the last paragraph
I have a function which is supposed to return three row
objects in a list. And the row objects are as following:
- row from table
mining
- belongs to the current
savestate
process
is either 1, 2 or 3, respectively for the three members in the list- of those the one with the biggest
finish_date
The minings have no direct relationship with savestates, so I have to trace the correct savestate through table called turn
. The relationships are as follows: savestate 1:n turn 1:1 mining.
This is what I have so far:
def get_latest_minings(save_id):
return_list = []
#get all turn.ids that belong to this savestate:
savestate_turns = [s.id for s in db(db.turn.savestate_id == save_id).select(db.turn.id)]
#get all minings that belong to these turns:
save_minings = db(db.mining.turn_id.belongs(savestate_turns)).select()
#loop to get three objects:
for i in range(1,4):
#from save_minings, get all minings, whose process is i:
line_minings = save_minings.find(lambda row: row.process == i)
#initialize loop variables:
latest_date = 0
latest = None
#loop to find the biggest finish_date:
for m in line_minings:
if m.finish_date > latest_date:
latest_date = m.finish_date
latest = m
#add the row with the biggest finish_date to the list:
return_list.append(latest)
#return locals() for testing purposes:
return locals()
#actual return:
#return return_list
This however doesn't work as intended. This is what it returns:
https://www.dropbox.com/s/ns6mq9414vw25s9/get_latest_minings.png?dl=0
I have run some separate tests and I have found the problem to be with the line: line_minings = save_minings.find(lambda row: row.process == i)
. Every other line works as it should. What is wrong here? Another question: can this be optimized more? I'm particularly curious about tracing the correct savestate.