import numpy as np import pandas as pd import os #creates a list with range r1,r2 and interval def createList(r1, r2, interval): return np.arange(r1, r2 + 1, interval) #creates enumID list def createEnumList(teamID): return np.arange(teamID+1,teamID+5,1) #search and deletes file with path def searchDelete(path): if os.path.exists(path): os.remove(path) print("File: " + path + " deleted") else: print("The file does not exist") #Change here if required r1, r2, interval = 110, 270, 10 teams = createList(r1, r2, interval) matchPairList = ["00","10","01","11"] main_df = pd.read_csv('./_static/treatments/treatment_randomization_otree_final.csv', dtype=str) #create room files recursively for all teams for team in teams: if team % 100 == 0: pass else: for matchID in matchPairList: temp_enum_list = createEnumList(team) enum_list = (str(x) for x in temp_enum_list) temp_df = main_df[main_df["enumID"].isin(enum_list)].loc[:,["game_room", "matchID_final"]].drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False) print(temp_df.head) sub_df = temp_df[temp_df["matchID_final"] == matchID].loc[:,"game_room"] file_dir = "./_rooms/" file_name = "enums_" + str(team) + "_" + matchID file_path = file_dir + file_name searchDelete(file_path) with open(file_path,'a') as room_file: dfAsString = sub_df.to_csv(header=False, index=False, sep=' ') room_file.write(dfAsString) #create rooms dictionary to be copy pasted in settings.py searchDelete("./_rooms/rooms_dict") with open('./_rooms/rooms_dict', 'a') as room_dict: if team % 100 != 0 : for team in teams: for matchID in matchPairList: room_dict.write('{}\n'.format("\tdict(")) room_dict.write('{}\n'.format("\t\t" + "name=\'" + "enums_" + str(team) + "_" + matchID + "\',")) room_dict.write('{}\n'.format("\t\t" + "display_name=\'" + "Enum team " + str(team) + "_" + matchID + "\',")) room_dict.write('{}\n'.format("\t\t" + "participant_label_file=\'" + "_rooms/enums_" + str(team) + "_" + matchID + "\',")) room_dict.write('{}\n'.format("\t),"))