2013-02-14 28 views
0

我使用BusinessHours PyPi包來計算日期和公共假期之間的日期,但它似乎在吐出'未定義'的錯誤(如下)。Python BusinessHours庫錯誤

他們給你一個關於如何調用這個包的例子。

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

+0

您是否在代碼中定義了'date'函數,或者您想使用'datetime.datetime.date'函數代替 – avasal 2013-02-14 05:58:31

+0

該代碼來自於我在PyPi上獲得的庫,我認爲您是對的,這個東西是打算使用datetime.datetime.date函數。 – dlyxzen 2013-02-14 06:55:14

回答

0

那包沒有寫得很好。如果您查看the code,它不會導入任何東西,但會嘗試使用需要導入的小球的功能(例如date)。

看起來提交包裹的人只是沒有做好工作。你可能需要實現你自己的代碼來做到這一點(也許使用他們的一些邏輯作爲一般指南)。

+0

嗨琥珀,我希望避免不得不重寫它:(我認爲可能有一個簡單的修復,也許它沒有導入正確的日期時間模塊,由於我的python版本或什麼的,或者我需要改變它到datetime.datetime.date像上面提到的avasal – dlyxzen 2013-02-14 06:57:47

+0

那麼,你可以嘗試修補庫 - 將正確的導入添加到庫的代碼文件。 – Amber 2013-02-14 07:00:21