2012-10-14 119 views
0

我在我的一個視圖中有以下代碼。問題是我無法弄清楚如何將它重構爲一個for循環。唯一的區別是'項目'。部分。我需要在同一個模板中的所有三個變量值,所以我不認爲我可以從urls.py傳遞信息。任何想法,我可以重寫這個,所以我不重複自己?重構此視圖代碼

謝謝。

def average(request): 

    cal_list = [] 
    cal_list2 = [] 
    cal_list3 = [] 

    exams = Test.objects.filter(test__test_name__iexact="one") 
    for item in exams: 
     cal_list.append(int(item.start)) 
     result = sum(cal_list)/float(165) * 100 
     result = result/len(cal_list) 
     result = int(round(result)) 
     result = str(result) + '%' 

    for item in exams: 
     cal_list2.append(int(item.s1)) 
     result2 = sum(cal_list2)/float(165) * 100 
     result2 = result2/len(cal_list2) 
     result2 = int(round(result2)) 
     result2 = str(result2) + '%' 

    for item in exams: 
     cal_list3.append(int(item.s2)) 
     result3 = sum(cal_list3)/float(165) * 100 
     result3 = result3/len(cal_list3) 
     result3 = int(round(result3)) 
     result3 = str(result3) + '%' 

    return direct_to_template(request, 'a.html', {'result': result, 'result2': result2, 'result3': result3}) 

回答

1

我只想創造一個功能:

def calculate_result(items): 
    items = tuple(int(i) for i in items) 
    result = sum(items)/16500.0 
    result = result/len(items) 
    return '%i' % round(result) + '%' 

result0 = calculate_result(item.start for item in exams) 
result1 = calculate_result(item.s1 for item in exams) 
result2 = calculate_result(item.s2 for item in exams) 
+0

Thanks @Nathan。我的工作很好,我應該能夠重複使用這個概念進行類似的計算。 –

+0

不客氣。 :) –

0

你去那裏的簡單重構,從只有所需的東西迭代和移動計算圈外的保存循環。

def average(request): 

    cal_list = [] 
    cal_list2 = [] 
    cal_list3 = [] 

    exams = Test.objects.filter(test__test_name__iexact="one") 
    for item in exams: 
     cal_list.append(int(item.start)) 
     cal_list2.append(int(item.s1)) 
     cal_list3.append(int(item.s2)) 

    result = sum(cal_list)/float(165) * 100 
    result = result/len(cal_list) 
    result = int(round(result)) 
    result = str(result) + '%' 

    result2 = sum(cal_list2)/float(165) * 100 
    result2 = result2/len(cal_list2) 
    result2 = int(round(result2)) 
    result2 = str(result2) + '%' 

    result3 = sum(cal_list3)/float(165) * 100 
    result3 = result3/len(cal_list3) 
    result3 = int(round(result3)) 
    result3 = str(result3) + '%' 

    return direct_to_template(request, 'a.html', {'result': result, 'result2': result2, 'result3': result3}) 
1

在你的循環,你一直覆蓋results值,這樣你就可以推斷出部分:

for item in items: 
    cal_list.append(int(item.start)) 
    cal_list2.append(int(item.s1)) 
    cal_list3.append(int(item.s2)) 

result = "%s%%" % round((sum(cal_list)/float(165) * 100)/len(cal_list)) 
result2 = "%s%%" % round((sum(cal_list2)/float(165) * 100)/len(cal_list2)) 
result3 = "%s%%" % round((sum(cal_list3)/float(165) * 100)/len(cal_list3)) 
0

可以使用aggregate在數據庫層執行總和。

from django.db import models 

def average(request): 
    queryset = Test.objects.filter(test__test_name__iexact="one") 
    total = queryset.count() 
    results = queryset.aggregate(
     result1=models.Sum('start'), 
     result2=models.Sum('s1'), 
     result3=models.Sum('s2')) 
    def transform(resultsum): 
     result = resultsum/float(165) * 100 
     result = result/total 
     result = int(round(result)) 
     result = str(result) + '%' 
     return result 
    result1 = transform(results['result1']) 
    result2 = transform(results['result2']) 
    result3 = transform(results['result3']) 
    return direct_to_template(request, 'a.html', {'result': result1, 'result2': result2, 'result3': result3})