0

我正在使用 BusinessHours PyPi 包来计算日期之间的工作日和公共假期,但它似乎吐出了“未定义”错误(如下)。

他们给你一个如何调用包的例子。

eg: Class:  BusinessHours(datetime1,datetime2,worktiming=[9 ,18],weekends=[6,7],holidayfile=None)

Class Parameters:
datetime1   - The datetime object with the starting date.
datetime2   - The datetime object with the ending date.
worktiming  - The working office hours . A list containing two values start time and end time
weekends    - The days in a week which have to be considered as weekends sent as a list, 1 for monday ...  7 for sunday.
holidayfile - A file consisting of the predetermined office holidays.Each date starts in a new line and currently must only be in the format dd-mm-yyyy

from BusinessHours import BusinessHours
from datetime import datetime
testing = BusinessHours(datetime(2007,10,15),datetime.now())
print testing.getdays()

使用此信息,我制作了以下测试脚本

from BusinessHours import BusinessHours
from datetime import datetime
holidaytextfile = 'holidays.txt' 

testing = BusinessHours(datetime(2013,02,01),datetime(2013,02,28),worktiming=[9 ,18],weekends=[6,7],holidayfile=holidaytextfile)
print testing.getdays()

我的假期文件包含以下内容(只是 2 月的一个随机工作日)

07-02-2013

当我运行脚本时,出现以下错误。

Traceback (most recent call last):
File "business_days", line 9, in ?
print testing.getdays()
File "/usr/lib/python2.4/site-packages/BusinessHours.py", line 63, in getdays
holidate = date(int(year) , int(month) , int(day))
NameError: global name 'date' is not defined

这是包中的代码部分。

 60             for definedholiday in definedholidays:
 61                 flag=0;
 62                 day , month , year = definedholiday.split('-')
 63                 holidate = date(int(year) , int(month) , int(day))
 64                 for weekend in self.weekends:
 65                      #check if mentioned holiday lies in defined weekend , shouldnt deduct twice
 66                     if(holidate.isoweekday==weekend):
 67                         flag=1;
 68                         break;

感谢任何人给我的任何帮助,我的 Python 版本是 2.4.3

4

1 回答 1

0

那个包写得不好。如果您查看代码,它不会导入任何内容,而是尝试使用需要导入的 mobules 中的函数(例如date)。

看起来提交包裹的人只是没有做好。您可能需要实现自己的代码来执行此操作(可能使用他们的一些逻辑作为一般指南)。

于 2013-02-14T06:02:33.167 回答