Python3版本下创建计算给定日期范围内工作日方法

osc_1i2u0r5x 2021-01-20 13:04:18
Python


眼睛都要挣不开了,代码还有很多需要改进的地方,暂时先把方法放上,明天再整理

# _*_ coding:utf-8 _*_
# Author: dai bing
# Date: 2020/12/13 22:48
# Scripts: calcworkday.py
# Desc: 给定两个日期区间,计算区间内的工作日
# 需要排除法定节假日以及统计休息日内调整为工作日内的天数
# 给定人数计算出需要多少个任务满足报工需求
#
import datetime
import time
class Tasks:
weeks = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']
pattern = "%Y-%m-%d"
def __init__(self):
super(Tasks, self).__init__()
self._person = None
self._start_date = None
self._end_date = None
self._statutory_holidays = None
self._non_rest_days = None
@property
def person(self):
return self._person
@person.setter
def person(self, value):
self._person = value
@property
def startDate(self):
return self._start_date
@startDate.setter
def startDate(self, value):
sdate = None
try:
if isinstance(value, str):
sdate = datetime.datetime.strptime(value, self.pattern)
else:
raise Exception
except Exception as e:
print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
print(e)
exit(11)
self._start_date = sdate
@property
def endDate(self):
return self._end_date
@endDate.setter
def endDate(self, value):
edate = None
try:
if isinstance(value, str):
edate = datetime.datetime.strptime(value, self.pattern)
else:
raise Exception
except Exception as e:
print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
print(e)
exit(12)
self._end_date = edate
@property
def statutoryHolidays(self):
return self._statutory_holidays
@statutoryHolidays.setter
def statutoryHolidays(self, value):
change_statutory_holidays = []
try:
if isinstance(value, list):
for sholiday in value:
# print(datetime.datetime.strptime(sholiday, pattern))
change_statutory_holidays.append(datetime.datetime.strptime(sholiday, self.pattern))
elif isinstance(value, str):
change_statutory_holidays.append(datetime.datetime.strptime(value, self.pattern))
except ValueError:
print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
exit(13)
self._statutory_holidays = change_statutory_holidays
@property
def nonRestDay(self):
return self._non_rest_days
@nonRestDay.setter
def nonRestDay(self, value):
change_work_day = []
pattern = "%Y-%m-%d"
try:
if isinstance(value, list):
for non_work_day in value:
change_work_day.append(datetime.datetime.strptime(non_work_day, pattern))
elif isinstance(value, str):
change_work_day.append(datetime.datetime.strptime(value, pattern))
except TypeError:
print("输入值{}类型为{},需要输入日期型字符串或日期型数组.".format(value, type(value)))
exit(14)
self._non_rest_days = change_work_day
# 计算指定日期区间内的工作日
# statutory_h排除法定节假日
# work_do休息日调休变更为工作日
def getWorkDay(self, statutory_h, work_do):
day_off = 5, 6
# print(day_off)
days_work = [x for x in range(7) if x not in day_off]
# 排除法定节假日
statutory_holidays = statutory_h
# 增加调休工作日
non_rest_days = work_do
# print("statutory_holidays: ", len(statutory_holidays))
# print("work_day_off:", len(work_day_off))
tag_date = self.startDate
flag = 0
while True:
if tag_date > self.endDate:
break
if tag_date.weekday() in days_work and tag_date not in statutory_holidays or tag_date in non_rest_days:
yield tag_date
tag_date += datetime.timedelta(days=1)
# print(days_work)
# print("flag:", flag)
def getjobs(self):
jobs = 1
coefficient = 1
while True:
coefficient = 113 * jobs / int(self.daysCount()) / self.person
jobs += 1
# print("coefficient:{},jobs:{}".format(coefficient, jobs))
if coefficient > 4:
if coefficient < 5:
print("[{}]{}月份共有{}位需要报工人员,需要{}个任务拆分进行报工。"
.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
datetime.datetime.now().month + 1, self.person, jobs))
break
def daysCount(self):
"""工作日统计,返回数字"""
return len(list(self.getWorkDay(self.statutoryHolidays, self.nonRestDay)))
if __name__ == "__main__":
print(__name__)
版权声明
本文为[osc_1i2u0r5x]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4386107/blog/4915414

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database