Skip to content

Instantly share code, notes, and snippets.

@ronaldgreeff
Created May 7, 2019 15:50
Show Gist options
  • Save ronaldgreeff/09d63ba9fa216648cc6e89ed12a764c9 to your computer and use it in GitHub Desktop.
Save ronaldgreeff/09d63ba9fa216648cc6e89ed12a764c9 to your computer and use it in GitHub Desktop.
def get_filename():
dt = datetime.datetime.now()
filename = '_stored_values-{}_{}_{}'.format(dt.year, dt.month, dt.day)
return filename
def get_and_store_data(current_file_name):
questions = [
'Question 1',
'Question 2',
'Question 3',
'Question 4',
'Question 5',
'Question 6',
'Question 7',
'Question 8',
'Question 9',
'Question 10',
'Question 11',
'Question 12',
'Question 13',
]
data_sets = [
{
'label': set(('CC',)),
'data': [0.0 for i in range(len(questions))],
'backgroundColor': 'rgb(255, 99, 132, 0.2)',
'borderColor': 'rgb(255, 99, 132, 1)',
},
{
'label': set(('DP',)),
'data': [0.0 for i in range(len(questions))],
'backgroundColor': 'rgb(255, 159, 64, 0.2)',
'borderColor': 'rgb(255, 159, 64, 1)',
},
{
'label': set(('CM',)),
'data': [0.0 for i in range(len(questions))],
'backgroundColor': 'rgb(255, 205, 86, 0.2)',
'borderColor': 'rgb(255, 205, 86, 1)',
},
{
'label': set(('CC', 'DP',)),
'data': [0.0 for i in range(len(questions))],
'backgroundColor': 'rgb(75, 192, 192, 0.2)',
'borderColor': 'rgb(75, 192, 192, 1)',
},
{
'label': set(('CM', 'CC',)),
'data': [0.0 for i in range(len(questions))],
'backgroundColor': 'rgb(54, 162, 235, 0.2)',
'borderColor': 'rgb(54, 162, 235, 1)',
},
{
'label': set(('DP', 'CM',)),
'data': [0.0 for i in range(len(questions))],
'backgroundColor': 'rgb(153, 102, 255, 0.2)',
'borderColor': 'rgb(153, 102, 255, 1)',
},
{
'label': set(('CC', 'DP', 'CM',)),
'data': [0.0 for i in range(len(questions))],
'backgroundColor': 'rgb(231,233,237, 0.2)',
'borderColor': 'rgb(231,233,237, 1)',
},
]
surveys = Survey.objects.filter(state='closed')
last_4_surveys = surveys.order_by('-id')[:4]
relevant_responses = list()
for survey in last_4_surveys:
responses = survey.response_set.filter(number_invalid__gte=0)
for response in responses:
relevant_responses.append(response)
answer_sums = [0.0 for i in range(len(questions))]
for data_set in data_sets:
for question in questions:
for response in relevant_responses:
if set([member_cat for member_cat in response.member.category]) == data_set['label']:
for answer in response.response_answers.filter(body__isnull=False):
if answer.question.body == question:
i = questions.index(question)
a = float(answer.body)
data_set['data'][i] = data_set['data'][i] + a
answer_sums[i] = answer_sums[i] + a
for data_set in data_sets:
for i in range(len(questions)):
if answer_sums[i] != None:
data_set['data'][i] = data_set['data'][i]/answer_sums[i]
with open(filename, 'wb') as f:
pickle.dump(data_sets, f)
os.remove(current_file_name)
def get_data():
for i in os.listdir():
if i.startswith('_stored_values-'):
current_file_name = i
if current_file_name != get_filename():
get_and_store_data(current_file_name)
with open(current_file_name, 'rb') as f:
return pickle.load(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment