oTree Forum >

Efficient way of storing a partner's player object

#1 by JSM (edited )

Hello,

I run an experiment with groups of 2 where one player makes a decision on one page and the partner makes decision based on the partners inputs on the next page. This back and forth goes on for several pages.

I frequently find myself having to write pages like this:

class MyPage(Page):
    form_model = 'player'
    formfields = [...]
    
    def vars_for_template(player):
        my_group = player.get_others_in_group()
        partner = my_group[0]
        # Some more code
    def js_vars(player):
        my_group = player.get_others_in_group()
        partner = my_group[0]
        # Some more code
    def is_displayed(player):
        my_group = player.get_others_in_group()
        partner = my_group[0]
        # Some Code

I understand that calling the get_others_in_group() function multiple times is not efficient and so I am looking for a way to store the player object of the partner maybe once and then just reusing this variable every time I need to access variables of a partner. Or is there any way to reduce the amounts of time to call the get_others_in_group() function?

Is there some way to do this in oTree?

I would be thankful for any idea/help!

#2 by woodsd42

If there's only a few decisions you could save it at the Player level.  E.g. have fields like:

class Player(BasePlayer):
    partners_price = models.IntegerField()
    partners_quantity = models.IntegerField()
   
Then you could call get_others_in_group() once in before_next_page() or something, given it sounds like its sequential.


Otherwise, if there's a lot of decisions you could save it at the participant.vars level, perhaps with the relevant entries as a list or dict.  However, I don't know how efficient accessing participant.vars is - probably not that bad though.


Finally, another option would be to save the partners id_in_group at the Player level.  Then you could call get_player_by_id(n) instead.  I don't know if that's more efficient though.

Write a reply

Set forum username