如何使用python Django最小化代码

时间:2019-04-18 06:14:30

标签: python-2.7 django-rest-framework

我有一个嵌套的if条件来检查条件。如何最小化代码?我应该怎样缩短代码? 我创建了一个函数,并且在t函数内部,我正在检查用户提供的仪表的ID,并更新数据库中的表。

def updatereport(meterdetails_id,indicator ='inr'):

if meterdetails_id == 1:

    happycount1 = Report.objects.filter(date = timezone.datetime.today()).values("happycount")

    if len(happycount1)<=0:
        Report.objects.create(date = timezone.datetime.today(),happycount=0)
    else:

        if indicator == 'inr':

            Report.objects.filter(date=timezone.datetime.today()).update(happycount=happycount1.get()["happycount"]+1)
        else:

            Report.objects.filter(date=timezone.datetime.today()).update(happycount=happycount1.get()["happycount"] - 1)
elif meterdetails_id == 3:
    disappointedcount1 = Report.objects.filter(date = timezone.datetime.today()).values("disappointedcount")
    if len(disappointedcount1) <= 0:
        Report.objects.create(date=timezone.datetime.today(), disappointedcount=0)

    else:

        if indicator == 'inr':
            Report.objects.filter(date = timezone.datetime.today()).update(disappointedcount = disappointedcount1.get()["disappointedcount"] + 1)

        else:

            Report.objects.filter(date = timezone.datetime.today()).update(disappointedcount = disappointedcount1.get()["disappointedcount"]-1)
elif meterdetails_id == 2:
    depressedcount1 = Report.objects.filter(date = timezone.datetime.today()).values("depressedcount")
    if len(depressedcount1) <= 0:
        Report.objects.create(date = timezone.datetime.today(), depressedcount=0)
    else:
        if indicator=='inr':
            Report.objects.filter(date = timezone.datetime.today()).update(depressedcount = depressedcount1.get()["depressedcount"] + 1)
        else:

            Report.objects.filter(date = timezone.datetime.today()).update(depressedcount = depressedcount1.get()["depressedcount"] - 1)

elif meterdetails_id == 5:
    okcount1 = Report.objects.filter(date = timezone.datetime.today()).values("okcount")
    if len(okcount1) <= 0:
        Report.objects.create(date = timezone.datetime.today(), okcount=0)
    else:
        if indicator =='inr':
            Report.objects.filter(date = timezone.datetime.today()).update(okcount = okcount1.get()["okcount"] + 1)
        else:

            Report.objects.filter(date = timezone.datetime.today()).update(okcount = okcount1.get()["okcount"] - 1)

    elif meterdetails_id == 6:
    sadcount1 = Report.objects.filter(date = timezone.datetime.today()).values("sadcount")
    if len(sadcount1) <= 0:
        Report.objects.create(date = timezone.datetime.today(), sadcount=0)
    else:
        if indicator =='inr':
            Report.objects.filter(date = timezone.datetime.today()).update(sadcount = sadcount1.get()["sadcount"] + 1)
        else:

            Report.objects.filter(date = timezone.datetime.today()).update(sadcount = sadcount1.get()["sadcount"] -1)
      elif meterdetails_id == 7:
    angrycount1 = Report.objects.filter(date = timezone.datetime.today()).values("angrycount")
    if len(angrycount1) <= 0:
        Report.objects.create(date = timezone.datetime.today(), angrycount = 0)
    else:
        if indicator =='inr':
            Report.objects.filter(date = timezone.datetime.today()).update(angrycount = angrycount1.get()["angrycount"] + 1)
        else:
            # print ('here')
            Report.objects.filter(date = timezone.datetime.today()).update(angrycount = angrycount1.get()["angrycount"] - 1)

models.py

 class Report(models.Model):
happycount=models.IntegerField(default=0,null=True,blank=True)

disappointedcount=models.IntegerField(default=0,null=True,blank=True)
depressedcount=models.IntegerField(default=0,null=True,blank=True)
okcount=models.IntegerField(default=0,null=True,blank=True)
sadcount=models.IntegerField(default=0,null=True,blank=True)
angrycount=models.IntegerField(default=0,null=True,blank=True)
date=models.DateField('date',default=timezone.datetime.today(),null=True,blank=True)

def __unicode__(self):
    return '%s' % self.date

ordering = ["date"]
verbose_name = "report"
verbose_name_plural = "reports"

1 个答案:

答案 0 :(得分:0)

def updatereport(meterdetails_id, indicator='inr'):
    if meterdetails_id == 1:
        proper_function_name('happycount', indicator)
    elif meterdetails_id == 3:
        proper_function_name('disappointedcount', indicator)
    elif meterdetails_id == 2:
        proper_function_name('depressedcount', indicator)    
    elif meterdetails_id == 5:
        proper_function_name('okcount', indicator)
    elif meterdetails_id == 6:
        proper_function_name('sadcount', indicator)
    elif meterdetails_id == 7:
        proper_function_name('angrycount', indicator)


def proper_function_name(count_name, indicator):
    count = Report.objects.filter(date=timezone.datetime.today()).values(count_name)
    if len(count) <= 0:
        Report.objects.create(date=timezone.datetime.today(), **{count_name: 0})
    else:
        if indicator =='inr':
            Report.objects.filter(date=timezone.datetime.today()).update(**{count_name: count.get()[count_name] + 1})
        else:
            Report.objects.filter(date=timezone.datetime.today()).update(**{count_name: count.get()[count_name] - 1})

我调用了函数proper_function_name,但您应该想出更好的方法:P也请考虑使用Enum而不是硬编码的meterdetails_ids,例如:

class MeterDetail(Enum):
    happycount=1
    disappointedcount=3
    ...

因此在代码后面,您将拥有

if meterdetails_id == MeterDetail.happycount:
   ...

代替:

if meterdetails_id == 1:
    ...