2015-12-14 137 views
0

解决了天真的日期时间问题后,我正在面对生成图形的视图上的新问题。现在我得到mktime参数超出范围。 我不知道如何解决它。我没有写代码,我从我的同事那里使用它,我似乎无法理解它为什么失败。我认为它与超时运行的函数有关,并且错误会弹出。OverflowError mktime参数超出范围

@login_required(login_url='/accounts/login/') 
def loggedin(request): 
    data = [] 
    data2 = [] 
    data3 = [] 
    dicdata2 = {} 
    dicdata3 = {} 
    datainterior = [] 
    today = timezone.localtime(timezone.now()+timedelta(hours=1)).date() 
    tomorrow = today + timedelta(1) 
    semana= today - timedelta(7) 
    today = today - timedelta(1) 
    semana_start = datetime.combine(today, time()) 
    semana_start = timezone.make_aware(semana_start, timezone.utc) 
    today_start = datetime.combine(today, time()) 
    today_start = timezone.make_aware(today_start, timezone.utc) 
    today_end = datetime.combine(tomorrow, time()) 
    today_end = timezone.make_aware(today_end, timezone.utc) 
    for modulo in Repository.objects.values("des_especialidade").distinct(): 
     dic = {} 
     mod = str(modulo['des_especialidade']) 
     dic["label"] = str(mod) 
     dic["value"] = Repository.objects.filter(des_especialidade__iexact=mod).count() 
     data.append(dic) 
    for modulo in Repository.objects.values("modulo").distinct(): 
     dic = {} 
     mod = str(modulo['modulo']) 
     dic["label"] = str(mod) 
     dic["value"] = Repository.objects.filter(modulo__iexact=mod, dt_diag__gte=semana_start).count() 
     datainterior.append(dic) 
     # print mod, Repository.objects.filter(modulo__iexact=mod).count() 
     # data[mod] = Repository.objects.filter(modulo__iexact=mod).count() 
    dicdata2['values'] = datainterior 
    dicdata2['key'] = "Cumulative Return" 
    dicdata3['values'] = data 
    dicdata3['color'] = "#d67777" 
    dicdata3['key'] = "Diagnosticos Identificados" 
    data3.append(dicdata3) 
    data2.append(dicdata2) 



    #-------sunburst 
    databurst = [] 
    dictburst = {} 
    dictburst['name'] = "CHP" 
    childrenmodulo = [] 
    for modulo in Repository.objects.values("modulo").distinct(): 
     childrenmodulodic = {} 
     mod = str(modulo['modulo']) 
     childrenmodulodic['name'] = mod 
     childrenesp = [] 
     for especialidade in Repository.objects.filter(modulo__iexact=mod).values("des_especialidade").distinct(): 
      childrenespdic = {} 
      esp = str(especialidade['des_especialidade']) 
      childrenespdic['name'] = esp 
      childrencode = [] 
      for code in Repository.objects.filter(modulo__iexact=mod,des_especialidade__iexact=esp).values("cod_diagnosis").distinct(): 
       childrencodedic = {} 
       codee= str(code['cod_diagnosis']) 
       childrencodedic['name'] = 'ICD9 - '+codee 
       childrencodedic['size'] = Repository.objects.filter(modulo__iexact=mod,des_especialidade__iexact=esp,cod_diagnosis__iexact=codee).count() 
       childrencode.append(childrencodedic) 
      childrenespdic['children'] = childrencode 



      #childrenespdic['size'] = Repository.objects.filter(des_especialidade__iexact=esp).count() 
      childrenesp.append(childrenespdic) 
     childrenmodulodic['children'] = childrenesp 
     childrenmodulo.append(childrenmodulodic) 
    dictburst['children'] = childrenmodulo 
    databurst.append(dictburst) 
    # print databurst 



    # --------stacked area chart 
    datastack = [] 
    for modulo in Repository.objects.values("modulo").distinct(): 
     datastackdic = {} 
     mod = str(modulo['modulo']) 
     datastackdic['key'] = mod 
     monthsarray = [] 
     year = timezone.localtime(timezone.now()+timedelta(hours=1)).year 
     month = timezone.localtime(timezone.now()+timedelta(hours=1)).month 
     last = timezone.localtime(timezone.now()+timedelta(hours=1)) - relativedelta(years=1) 
     lastyear = int(last.year) 
     lastmonth = int(last.month) 
     #i = 1 
     while lastmonth <= int(month) or lastyear<int(year): 
      date = str(lastmonth) + '/' + str(lastyear) 
      if (lastmonth < 12): 
       datef = str(lastmonth + 1) + '/' + str(lastyear) 
      else: 
       lastmonth = 01 
       lastyear = int(lastyear)+1 
       datef = str(lastmonth)+'/'+ str(lastyear) 
       lastmonth = 0 
      datainicial = datetime.strptime(date, '%m/%Y') 
      datainicial = timezone.make_aware(datainicial, timezone.utc) 
      datafinal = datetime.strptime(datef, '%m/%Y') 
      datafinal = timezone.make_aware(datafinal, timezone.utc) 
      #print "lastmonth",lastmonth,"lastyear", lastyear 
      #print "datainicial:",datainicial,"datafinal: ",datafinal 
      filtro = Repository.objects.filter(modulo__iexact=mod) 
      count = filtro.filter(dt_diag__gte=datainicial, dt_diag__lt=datafinal).count() 
      conv = datetime.strptime(date, '%m/%Y') 
      ms = datetime_to_ms_str(conv) 
      monthsarray.append([ms, count]) 
      #i += 1 
      lastmonth += 1 
     datastackdic['values'] = monthsarray 
     datastack.append(datastackdic) 
     #print datastack 


    if request.user.last_login is not None: 
     #print(request.user.last_login) 
     contador_novas = Repository.objects.filter(dt_diag__lte=today_end, dt_diag__gte=today_start).count() 
    return render_to_response('loggedin.html', 
           {'user': request.user.username, 'contador': contador_novas, 'data': data, 'data2': data2, 
           'data3': data3, 
           'databurst': databurst, 'datastack':datastack}) 


def datetime_to_ms_str(dt): 
    return str(1000 * mktime(dt.timetuple())) 
+0

引起错误的'dt.timetuple()'的值是多少? – Alasdair

+0

某处在3001. 我无法弄清楚为什么函数会继续运行。 –

+0

所以这个问题并不是mktime的问题,它是当你期望的时候没有结束的循环。当你在每个循环中打印lastmonth和lastyear时,你会得到什么值?你期望什么?设置'lastmonth = 0'对我来说看起来是个错误。 – Alasdair

回答

1

我认为问题在于这种情况。

while lastmonth <= int(month) or lastyear<int(year): 

month=12月,期间,所以lastmonth <= int(month)永远是True。所以这个循环往往会返回True,即使一次lastyear更多的是当前的year

如果循环处于上一年,或者循环处于当前年份而月份不在将来,您希望循环。因此,我想你想将其更改为以下:

while lastyear < year or (lastyear == year and lastmonth <= month): 

为了确保代码工作,并了解它,你需要添加大量的打印语句的循环,看看如何lastmonthlastyear更改,并检查循环是否在您期望的时候退出。您还需要测试其他值yearmonth,以便下个月不会中断。理想情况下,您想将这部分代码提取到单独的函数中。如果仅返回一个(month, year)整数列表,而不是同时执行大量日期格式化,那么理解循环会更容易。那么添加单元测试会更容易。

+0

你是一个天才。它解决了我的问题。现在,请你能解释为什么它像我一样愚蠢的工作吗? –

+0

事实上,我认为我之前写的答案是错误的,只会在十二月份发挥作用。看到我更新的答案。理解代码的最好方法是添加大量的打印语句,并检查它是否按照您的预期方式工作。 – Alasdair

+0

你最后的答案仍然有效,所以我会使用那个。无论如何,我需要理解这个逻辑事物,因为在这种情况下,我会变得非常困惑。 –