Python BusinessHours库错误
我使用BusinessHours PyPi包计算到日期之间的工作日和公共假日,但它似乎抛出了一个“未定义”错误(见下文) 他们给出了一个如何调用包的示例Python BusinessHours库错误,python,Python,我使用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
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()
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;
利用这些信息,我制作了下面的测试脚本
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月份的一个随机工作日)
当我运行脚本时,我得到以下错误
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
这是包中的代码部分
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()
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,这个包写得不好。如果您查看,它不会导入任何内容,而是尝试使用需要导入的mobules中的函数(例如,
date
)
看来提交包裹的人做得不好。为此,您可能需要实现自己的代码(可能使用它们的一些逻辑作为一般指南)。您是否在代码中定义了
date
函数,或者您希望使用datetime.datetime.date
函数来代替该函数该代码来自我在PyPi上获得的库,我认为你是对的,写这篇文章的人可能打算使用datetime.datetime.date函数。嗨,Amber,我希望避免重写它:(我认为可能有一个简单的修复方法,可能是因为我的python版本或其他原因,它没有导入正确的datetime模块。或者我需要像上面提到的avasal那样将其更改为datetime.datetime.date。好吧,您可以尝试修补库-将正确的导入添加到库的代码文件中。