gpt4 book ai didi

python - 从 python 中的函数返回的问题在哪里?

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:48 25 4
gpt4 key购买 nike

我不明白为什么函数在 python 中不返回,我的函数计算两个日期之间的天数(星期五和星期六)但它不返回。

这是我的代码:

# -*- coding: utf-8 -*-

import datetime
import calendar
calendar.setfirstweekday(calendar.SUNDAY)

from odoo import models, fields, api

class HrMission(models.Model):
_name = "hr.employee.mission"
_description = "hr mission"
_inherit = "hr.employee.mission"

days_compensation =fields.Float(compute='get_compensation', compstring='Jours de récupération', help="Jours de récupération si la mission contient les jours de repos",
required=True, readonly=True,)

@api.multi
@api.depends('mission_start_date', 'mission_end_date')
def get_compensation(self):
for rec in self:
if rec.mission_start_date and rec.mission_end_date:
time1 = datetime.datetime.strptime(rec.mission_start_date, "%Y-%m-%d")
time2 = datetime.datetime.strptime(rec.mission_end_date, "%Y-%m-%d")
week = {}
leave_value = {}
# Compute Number Of Friday And Saturday
for i in range((time2 - time1).days):
day = calendar.day_name[(time1 + datetime.timedelta(days=i+1)).weekday()]
week[day] = week[day] + 1 if day in week else 1
fri = week.get('Friday') if 'Friday' in week else 0 # Result Number 1 Of friday If "Start Date", "End date" --> "02/04/2019", "07/04/2019"
sat = week.get('Saturday') if 'Saturday' in week else 0 # Same thing that Friday, Numbre 1 for Saturday
friandsat = fri + sat # Result 2
rec.days_compensation = friandsat

最佳答案

您需要将其显式放入您的 get_compensation() 函数中:

return friandsat

否则你的函数不知道它需要返回一些东西。

关于python - 从 python 中的函数返回的问题在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55454601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com