Jacob_DiDiodato

Completed Survey

Aug 9th, 2017
339
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.96 KB | None | 0 0
  1. import pygame, easygui, sys, random
  2.  
  3. try:
  4.     import openpyxl
  5. except ImportError:
  6.     print "Please install openpyxl. To do that use pip install openpyxl"
  7.     raise SystemExit
  8.  
  9. def main():
  10.     #All font and text
  11.     font = pygame.font.SysFont("times", 30)
  12.     choiceALabel = font.render("Choice A", 1, (255,255,255))
  13.     choiceBLabel = font.render("Choice B", 1, (255,255,255))
  14.     submitLabel = font.render("Pick one ->", 1, (255,255,255))
  15.     #End of font and text
  16.    
  17.     attributesList = []
  18.     levelsDict = {}
  19.     attributesSurfaces = []
  20.     levelsSurfaces = []
  21.     excelInformation = {}
  22.     trial = 0
  23.     numTrials = 0
  24.     attributesPerPage = 0
  25.     try:
  26.         attributesFile = open("surveyinfo.txt", "r")
  27.         fileContents = attributesFile.read().split("\n")
  28.         #Checking to see if the file contains the magic number
  29.         if fileContents[0] != "040":
  30.             easygui.msgbox(msg="Survey Error. File not setup properly. Please use the conjointSDT.py and export to Jacob-formatted TXT.",title="Error")
  31.             return
  32.         fileContentsNoMagicNumber = fileContents[1:]
  33.         #Iterate everything besides the magic number to build the attributes and levels
  34.         for i in range(len(fileContentsNoMagicNumber)):
  35.             if i != len(fileContentsNoMagicNumber) - 1:
  36.                 attributesList.append(fileContentsNoMagicNumber[i].split(":")[0])
  37.                 levelsDict[attributesList[i]] = fileContentsNoMagicNumber[i].split(":")[1].split(",")
  38.             else:
  39.                 numTrials = int(fileContentsNoMagicNumber[i])
  40.     #If the file doesn't exist then exit the program
  41.     except IOError:
  42.         easygui.msgbox(msg="Survey Error. Missing file \"surveyinfo.txt\". Please use conjointSDT.py and export to Jacob-formatted TXT.",title="Error")
  43.         return
  44.    
  45.     if len(attributesList) > 5:
  46.         attributesPerPage = 5
  47.     else:
  48.         attributesPerPage = len(attributesList)
  49.        
  50.     #print attributesPerPage
  51.  
  52.     clock = pygame.time.Clock()
  53.  
  54.     screen = pygame.display.set_mode((640,480), pygame.NOFRAME)
  55.    
  56.     #Here we set a unique userID
  57.     userID = 1
  58.     try:
  59.         #Here we read from the file if we can and get the unique ID of the user
  60.         userIDFile = open("id.txt", "r")
  61.         userID = int(userIDFile.read())
  62.         userIDFile.close()
  63.     except IOError:
  64.         #If the file doesn't exist then we pass by the exception because it's not terrible
  65.         pass
  66.    
  67.     #Here we replace the contents of the old file with a new userID
  68.     userIDFile = open("id.txt", "w")
  69.     userIDFile.write(str(userID+1))
  70.     userIDFile.close()
  71.     #End of unique userID
  72.    
  73.     #Early questions here
  74.     age = int(easygui.enterbox("Please enter your age."))
  75.     gender = easygui.buttonbox(msg="Are you male or female?", choices=('Male', 'Female'), cancel_choice='Not Specified')
  76.     print gender
  77.     #End of early questions
  78.    
  79.     running = True
  80.     createPage = True
  81.     aFirst = True
  82.     bFirst = False
  83.     while running:
  84.         if createPage:
  85.             isAFirst = random.randint(0,1)
  86.             if isAFirst:
  87.                 aFirst = True
  88.                 bFirst = False
  89.             else:
  90.                 aFirst = False
  91.                 bFirst = True
  92.             chosenAttributes = random.sample(attributesList, attributesPerPage)
  93.             chosenLevelsForA = []
  94.             chosenLevelsForB = []
  95.             for i in range(attributesPerPage):
  96.                 chosenLevelsForA.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
  97.                 chosenLevelsForB.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
  98.             attributesSurfaces = []
  99.             levelsSurfacesForA = []
  100.             levelsSurfacesForB = []
  101.             for i in range(attributesPerPage):
  102.                 attributesSurfaces.append(font.render(chosenAttributes[i], 1, (255,255,255)))
  103.                 levelsSurfacesForA.append(font.render(chosenLevelsForA[i], 1, (255,255,255)))
  104.                 levelsSurfacesForB.append(font.render(chosenLevelsForB[i], 1, (255,255,255)))
  105.             createPage = False
  106.         for event in pygame.event.get():
  107.             if event.type == pygame.QUIT:
  108.                 running = False
  109.             if event.type == pygame.MOUSEBUTTONUP:
  110.                 mousePos = pygame.mouse.get_pos()
  111.                 #Getting the click for the radio buttons
  112.                 outcome = 0
  113.                 clickedOnButton = False
  114.                 if mousePos[0] >= 350 and mousePos[0] <= 370 and mousePos[1] >= 445 and mousePos[1] <= 465: #First button
  115.                     clickedOnButton = True
  116.                     if aFirst == True:
  117.                         outcome = 1
  118.                     else:
  119.                         outcome = 2
  120.                 elif mousePos[0] >= 535 and mousePos[0] <= 555 and mousePos[1] >= 445 and mousePos[1] <= 465: #Second button
  121.                     clickedOnButton = True
  122.                     if aFirst == True:
  123.                         outcome = 3
  124.                     else:
  125.                         outcome = 4
  126.                
  127.                 excelInformation["Trial " + str(trial + 1)] = {}
  128.                 for i in range(len(attributesList)):
  129.                     excelInformation["Trial " + str(trial + 1)][attributesList[i]] = ""
  130.                 excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = ""
  131.                 excelInformation["Trial " + str(trial + 1)]["Neither?"] = ""
  132.                 excelInformation["Trial " + str(trial + 1)]["Age"] = age
  133.                 excelInformation["Trial " + str(trial + 1)]["Gender"] = gender
  134.                    
  135.                 #print "Outcome: " + str(outcome)
  136.                
  137.                 if outcome == 1:#Picked B when B was first
  138.                     for i in range(len(chosenAttributes)):
  139.                         #print chosenAttributes[i] + ":" + chosenLevelsForA[i]#Wrong but it works
  140.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
  141.                     keepOption = easygui.ynbox("You have chosen Option B. If given the option between option B and no option would you still choose option B?", "Confirmation Screen", ("Yes", "No"))
  142.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
  143.                     if keepOption:
  144.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  145.                     else:
  146.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  147.                 elif outcome == 2:#Picked A when A was first
  148.                     for i in range(len(chosenAttributes)):
  149.                         #print chosenAttributes[i] + ":" + chosenLevelsForA[i]
  150.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
  151.                     keepOption = easygui.ynbox("You have chosen Option A. If given the option between option A and no option would you still choose option A?", "Confirmation Screen", ("Yes", "No"))
  152.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
  153.                     if keepOption:
  154.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  155.                     else:
  156.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  157.                 elif outcome == 3:#Picked A when B was first
  158.                     for i in range(len(chosenAttributes)):
  159.                         #print chosenAttributes[i] + ":" + chosenLevelsForB[i]#Wrong but it works
  160.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
  161.                     keepOption = easygui.ynbox("You have chosen Option A. If given the option between option A and no option would you still choose option A?", "Confirmation Screen", ("Yes", "No"))
  162.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
  163.                     if keepOption:
  164.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  165.                     else:
  166.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  167.                 elif outcome == 4:#Picked B when A was first
  168.                     for i in range(len(chosenAttributes)):
  169.                         #print chosenAttributes[i] + ":" + chosenLevelsForB[i]
  170.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
  171.                     keepOption = easygui.ynbox("You have chosen Option B. If given the option between option B and no option would you still choose option B?", "Confirmation Screen", ("Yes", "No"))
  172.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
  173.                     if keepOption:
  174.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  175.                     else:
  176.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  177.                 if clickedOnButton:
  178.                     trial += 1
  179.                     createPage = True
  180.                     if trial == numTrials:
  181.                         running = False
  182.                        
  183.                         try:
  184.                             wb = openpyxl.load_workbook('survey_results.xlsx')
  185.                             ws = wb.worksheets[0]
  186.                         except IOError:
  187.                             wb = openpyxl.Workbook()
  188.                             ws = wb.worksheets[0]
  189.                             ws.cell(row = 1, column = 1).value = "User #"
  190.                             ws.cell(row = 1, column = 2).value = "Trial #"
  191.                             for i in range(len(attributesList)):
  192.                                 ws.cell(row = 1, column = 3 + i).value = attributesList[i]
  193.                             ws.cell(row = 1, column = 3 + len(attributesList)).value = "A/B"
  194.                             ws.cell(row = 1, column = 4 + len(attributesList)).value = "Neither?"
  195.                             ws.cell(row = 1, column = 5 + len(attributesList)).value = "Age"
  196.                             ws.cell(row = 1, column = 6 + len(attributesList)).value = "Gender"
  197.                        
  198.                         for i in range(numTrials):
  199.                             #print userID
  200.                             ws.cell(row = 2 + i + numTrials*(userID-1), column = 1).value = userID
  201.                             ws.cell(row = 2 + i + numTrials*(userID-1), column = 2).value = i + 1
  202.                             for j in range(len(attributesList)):
  203.                                 try:
  204.                                     ws.cell(row = 2 + i + numTrials*(userID-1), column = 3 + j).value = int(excelInformation["Trial " + str(i + 1)][attributesList[j]])
  205.                                 except ValueError:
  206.                                     ws.cell(row = 2 + i + numTrials*(userID-1), column = 3 + j).value = excelInformation["Trial " + str(i + 1)][attributesList[j]]
  207.                             ws.cell(row = 2 + i + numTrials*(userID-1), column = 3 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Option Chosen"]
  208.                             ws.cell(row = 2 + i + numTrials*(userID-1), column = 4 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Neither?"]
  209.                             ws.cell(row = 2 + i + numTrials*(userID-1), column = 5 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Age"]
  210.                             ws.cell(row = 2 + i + numTrials*(userID-1), column = 6 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Gender"]
  211.                            
  212.                         wb.save("survey_results.xlsx")
  213.                        
  214.                         #print excelInformation
  215.                    
  216.         #Drawing the UI
  217.         screen.fill((211,211,211))
  218.         pygame.draw.line(screen, (0,0,0), (453, 0), (453, 480))
  219.         pygame.draw.line(screen, (0,0,0), (266, 0), (266, 480))
  220.         pygame.draw.line(screen, (0,0,0), (0, 72), (640, 72))
  221.         pygame.draw.line(screen, (0,0,0), (0, 144), (640, 144))
  222.         pygame.draw.line(screen, (0,0,0), (0, 216), (640, 216))
  223.         pygame.draw.line(screen, (0,0,0), (0, 288), (640, 288))
  224.         pygame.draw.line(screen, (0,0,0), (0, 360), (640, 360))
  225.         pygame.draw.line(screen, (0,0,0), (0, 432), (640, 432))
  226.         pygame.draw.ellipse(screen, (255,255,255), (350,445,20,20))
  227.         pygame.draw.ellipse(screen, (0,0,0), (349,444,22,22), 1)
  228.         pygame.draw.ellipse(screen, (255,255,255), (535,445,20,20))
  229.         pygame.draw.ellipse(screen, (0,0,0), (534,444,22,22), 1)
  230.         screen.blit(choiceALabel, (303 + int(aFirst)*187, 20))
  231.         screen.blit(choiceBLabel, (303 + int(bFirst)*187, 20))
  232.         screen.blit(submitLabel, (screen.get_width()/5 - submitLabel.get_width()/2, screen.get_height() - 40))
  233.        
  234.         #print chosenAttributes
  235.         #print chosenLevels
  236.        
  237.         for i in range(0, attributesPerPage):
  238.             screen.blit(attributesSurfaces[i], (5, 90 + 72*i))
  239.             screen.blit(levelsSurfacesForA[i], (360 - levelsSurfacesForA[i].get_width()/2, 90 + 72*i))
  240.             screen.blit(levelsSurfacesForB[i], (545 - levelsSurfacesForB[i].get_width()/2, 90 + 72*i))
  241.    
  242.         pygame.display.update()
  243.        
  244.         msElapsed = clock.tick(60)
  245.  
  246. if __name__ == "__main__":
  247.     pygame.init()
  248.     main()
  249.     pygame.quit()
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment