我正在使用 python 2.7、tornado web 服务、peewee 数据库。
场景获取每月26号到下个月25号的请假报告。
所以我在下面的示例场景中做了这个(但从逻辑上讲我坚持了下来)
我从 UI 获取 from_date、to_date(选择 2016 年 4 月 26 日至 2016 年 5 月 25 日的报告)
Leavetable 是一个表名称(它包含员工 ID、休假起始日期、休假截止日期、工作天数)
working_days只不过是每次休假的计数(29-04-2016 - 02-05-2016) = 2 (周六和周日已排除)
特定月份单个员工的总休假时间 样本叶子是
16年4月24日 - 16年4月26日,
28-04-16 - 29-04-16 ,
2016年5月15日至2016年5月29日
我的代码(帮我改一下)
value=0
for report in Leavetable.select().where(Leavetable.Employee_ID==employees_id):
db_from_date = report.From_Date
if (from_date<=db_from_date and to_date>=db_from_date):
workingday=float(report.Working_Days)+value
value= workingday
print value
对于上面的相同休假,它的值 = 3
24-04-16 - 26-04-16(需要 1 天但无法通过 这段代码)
28-04-16 - 29-04-16(拍摄时间(2天)
2016年5月15日至2016年5月29日(26、27、28、29必须排除这4天)
请指导我解决这个逻辑的最简单的解决方案是什么,请执行必要的操作。
根据 Solarflare 指南,我添加了以下代码。
但即使从逻辑上讲,它也不适合。上述场景从2016年5月15日到2016年5月29日的休假在我做2016年4月15日到2016年5月15日的报告时这个休假也算在内
value=0
for report in Leavetable.select().where(Leavetable.Employee_ID==employees_id):
for dt in rrule(DAILY, dtstart=report.From_Date,until=report.To_Date):
if (dt.weekday() < 5):
if (report.To_Date>=from_date and report.From_Date<=to_date):
value += 1
print value
最佳答案
我用基本的 if 逻辑修改了上面的代码,上面的场景得到了满足下面我列出的代码可能是一些变量名称可能很难理解,如果这里有任何注释:
if (db_from_date>= prev_month and db_to_date<=this_month):
counting = float(working_day)
leave_availed=value+counting
value=leave_availed
elif(db_from_date >= prev_month and db_from_date <= this_month and db_to_date > this_month):
counting = float(working_day)
check_point =counting + 0.5
extra_days = workdays.networkdays(this_month_date,db_to_date,holidays)
if int(check_point)==check_point:
extra_days=extra_days - 0.5
count=counting-extra_days
leave_availed=value+count
value=leave_availed
elif(db_from_date < prev_month and db_to_date>=prev_month and db_to_date <= this_month):
counting = float(working_day)
extra_days = workdays.networkdays(db_from_date,prev_month_date,holidays)
count = counting - extra_days
leave_availed=value+count
value=leave_availed
elif(db_from_date < prev_month and db_to_date > this_month):
count = workdays.networkdays(prev_month,this_month,holidays)
leave_availed=value+count
value=leave_availed
else:
leave_availed=0
关于python - python 中休假报告逻辑和函数所需的指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024385/