Joseph Kimani
Senior Full Stack Developer, Experiments
joseph...@busaracenter.org | +254 710778099
Daykio Plaza, 5th Floor | Ngong Lane, Nairobi
www.busaracenter.org
class Subsession(BaseSubsession):
def chunks(self, lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def set_groups(self):
ethnicities = Constants.ethnicity
size = 2
ps = self.get_players()
# Uncomment to shuffle players before matching
# from random import shuffle
# shuffle(ps)
# Set macro groups based on ethnicity
ethnic_groups = {k: [p for p in ps if p.participant.vars['ethnicity'] == k] for k in ethnicities}
if self.round_number == 1:
# Set pairs within each ethnic group
group_matrix = list(pair for ethnic_group in ethnic_groups.values() for pair in self.chunks(ethnic_group, size))
elif self.round_number == 2:
from itertools import zip_longest
# Set list of players with alternating ethnic groups (i.e. A, B, C, A, B, C...)
ethnic_mix = list(p for mix in zip_longest(*ethnic_groups.values()) for p in mix if p)
# Set pairs from list with alternating ethnic groups (i.g. [A, B], [C, A], [B, C]
group_matrix = list(self.chunks(ethnic_mix, size))
return self.set_group_matrix(group_matrix)
In pages:
class GroupingPage(WaitPage):
wait_for_all_groups = True
after_all_players_arrive = 'set_groups'
--
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/CAFDbpJ%3DGLr7LUVF%3D-wY2q7FzXVa_F6depR2CDh2SDvHdVoBdDQ%40mail.gmail.com.