HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythondjangoMinor

URL selection function that is designed to randomly assign users to different surveys until full

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
selectionfulluntildesignedfunctionrandomlydifferentthatuserssurveys

Problem

I'm trying to condense some of my code to make it more maintainable and to learn a few new tricks.

Below is a Python 2.7 / Django 1.6 function which is designed to assign users to one of three surveys in my application. I require a total of 30 responses in total, 10 each. Each user is to be assigned at random to one of the three surveys to complete, once a survey e.g. SurveyTwo has had 10 participants it is taken out of the selection pool.

The function returns a segment of the URL which is then used to update a button

START


My below code works fine, but in reality I have nine such paths and I am looking to improve on my implementation method.

Also, please suggest any tips on styling or techniques.

PathMax = 10 
TotalMax = 30
TotalMaxCounter = 0
SurveyWizardOneCounter = 0
SurveyWizardTwoCounter = 0 
SurveyWizardThreeCounter = 0 
SurveyWizardFourCounter = 0 

SURVEY_URLS = ['/surveyone/', '/surveytwo/', '/surveythree/']

def start(request):    
    global TotalMaxCounter    
    if TotalMaxCounter  PathMax:
                SURVEY_URLS.pop(0)

        elif survey_url == '/surveytwo/':
            print 'SurveyWizardTwoCounter is: %s TotalMaxCounter is: %s' % (SurveyWizardTwoCounter, TotalMaxCounter)
            if SurveyWizardTwoCounter > PathMax:
                SURVEY_URLS.pop(1)

        elif survey_url == '/surveythree/':
            print 'SurveyWizardThreeCounter is: %s TotalMaxCounter is: %s' % (SurveyWizardThreeCounter, TotalMaxCounter)
            if SurveyWizardThreeCounter > PathMax:
                SURVEY_URLS.pop(2)

        return render(request, 'start.html', {'survey_url': survey_url})    
    else:
        return render(request, 'surveyfull.html')

Solution

You seem to have a fair bit of hard-coding there. I would suggest instead something like:

PATH_MAX = 10
WIZARD_COUNTER = {'/surveyone/': 0,
                  '/surveytwo/': 0,
                  '/surveythree/': 0,
                  ...}

def start(request):  
    if WIZARD_COUNTER:
        survey_url = random.choice(WIZARD_COUNTER)
        WIZARD_COUNTER[survey_url] += 1
        if WIZARD_COUNTER[survey_url] == PATH_MAX:
            del WIZARD_COUNTER[survey_url]
        return render(request, 'start.html', {'survey_url': survey_url})   
    else:
        return render(request, 'surveyfull.html')


This makes it much easier to alter the number of surveys in the future, you just need a dictionary mapping {url: count}. If you want to have different counts, the simplest way is to set the PATH_MAX to whichever you want the most of, then preset individual counts to PATH_MAX - count_for_that_survey.

Code Snippets

PATH_MAX = 10
WIZARD_COUNTER = {'/surveyone/': 0,
                  '/surveytwo/': 0,
                  '/surveythree/': 0,
                  ...}

def start(request):  
    if WIZARD_COUNTER:
        survey_url = random.choice(WIZARD_COUNTER)
        WIZARD_COUNTER[survey_url] += 1
        if WIZARD_COUNTER[survey_url] == PATH_MAX:
            del WIZARD_COUNTER[survey_url]
        return render(request, 'start.html', {'survey_url': survey_url})   
    else:
        return render(request, 'surveyfull.html')

Context

StackExchange Code Review Q#70939, answer score: 3

Revisions (0)

No revisions yet.