Old sorting players code not working in oTree 5

116 views
Skip to first unread message

Eva Vriens

unread,
Nov 26, 2021, 6:48:46 AM11/26/21
to oTree help & discussion
Hi all,

I transferred my project from oTree 3 to oTree 5 and encountered a problem with my grouping code. I sort all players and assign them to groups based on a random number that is 1 if they are inactive.

def shuffle_groups(subsession: Subsession):
# Create groups and assign player roles
# Sort players > from player list where inactive player has score 1
sorted_players = sorted(
subsession.get_players(), key=lambda player: player.participant.vars['score']
)

print('sorted players list', sorted_players)
group_matrix = []
ppg = Constants.players_per_group
for i in range(0, len(sorted_players), ppg):
group_matrix.append(sorted_players[i: i + ppg])
print(group_matrix)
subsession.session.vars['group_matrix'] = group_matrix
subsession.set_group_matrix(group_matrix)

The sorted_players list previously would return the id_in_subsession value of subsession.get_players() but with players sorted according to score. Now it returns the following:

sorted players list [Player(score=0.0826409206826..., inactive=False, round_number=1, id_in_group=1), Player(score=0.1581785318933..., inactive=False, round_number=1, id_in_group=2), Player(score=0.6265872080679..., inactive=False, round_number=1, id_in_group=3), Player(score=0.7151678241322..., inactive=False, round_number=1, id_in_group=4), Player(score=0.8716570581466..., inactive=False, round_number=1, id_in_group=5)]

Does anyone know how I should update the code to work in the new oTree lite format?

Many thanks!
Eva Vriens

Chris @ oTree

unread,
Nov 26, 2021, 11:18:42 AM11/26/21
to Eva Vriens, oTree help & discussion
The list looks sorted to me. Can you say more specifically what is different from your expectation?

Sent from my phone

On Nov 26, 2021, at 3:48 AM, Eva Vriens <evav...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/678b23dc-9c53-4c81-9195-f0cb7fda44fbn%40googlegroups.com.

Chris @ oTree

unread,
Nov 26, 2021, 11:25:06 AM11/26/21
to Eva Vriens, oTree help & discussion
Ok I see now. That is just a change to what is displayed when you use print(). In oTree 5, printing a player object shows multiple attributes, whereas in oTree 3 it only showed their ID in subsession. That should not change the functionality of your game. 

Sent from my phone

On Nov 26, 2021, at 8:18 AM, ch...@otree.org wrote:



Chris @ oTree

unread,
Nov 26, 2021, 11:34:42 AM11/26/21
to Eva Vriens, oTree help & discussion
Anyway print() is just for debugging, so if you want to see a specific attribute like id_in_subsession you should print that attribute. 

Sent from my phone

On Nov 26, 2021, at 8:25 AM, ch...@otree.org wrote:

Ok I see now. That is just a change to what is displayed when you use print(). In oTree 5, printing a player object shows multiple attributes, whereas in oTree 3 it only showed their ID in subsession. That should not change the functionality of your game. 

Eva Vriens

unread,
Nov 26, 2021, 12:01:32 PM11/26/21
to oTree help & discussion
Ok, I understand, thanks for the reply. For some reason it is not filling in the player IDs in the group matrix though. I'm getting the following error message: 

Application error (500)StatementError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (builtins.TypeError) cannot pickle 'weakref' object [SQL: UPDATE otree_session SET _vars=? WHERE otree_session.id = ?] [parameters: [{'_vars': {'last_round': 37, 'group_matrix': [[Player(???), Player(???), Player(???), Player(???), Player(???)]]}, 'otree_session_id': 2}]]

Error during __repr__ of Player: InvalidRequestError("This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)\n(builtins.TypeError) cannot pickle 'weakref' object\n[SQL: UPDATE otree_session SET _vars=? WHERE otree_session.id = ?]\n[parameters: [...]]")


The error occurs before printing the group_matrix line, so the problem is in appending the group matrix with the information from sorted_players. 

Eva

Op vrijdag 26 november 2021 om 17:34:42 UTC+1 schreef Chris @ oTree:

Chris @ oTree

unread,
Nov 26, 2021, 12:26:14 PM11/26/21
to Eva Vriens, oTree help & discussion
This line is a problem:

subsession.session.vars['group_matrix'] = group_matrix

You can’t store player objects into session.vars. Instead, I recommend making a list of just the .id_in_subsession of each player, and store that. 

Sent from my phone

On Nov 26, 2021, at 9:01 AM, Eva Vriens <evav...@gmail.com> wrote:

Ok, I understand, thanks for the reply. For some reason it is not filling in the player IDs in the group matrix though. I'm getting the following error message: 
Reply all
Reply to author
Forward
0 new messages