2017-01-23 60 views
0

我正在使用我從網上下載的腳本來訪問我們的服務API。 我試圖運行該功能,但不管我想要做什麼,都會收到錯誤。Python中的新手[2.7]函數不工作

from PyBambooHR import PyBambooHR 

bamboo = PyBambooHR(subdomain='domain', api_key='apicode') 
changes = bamboo.get_employee_changes() 

當我運行它,我得到以下錯誤:

ValueError: Error: since argument must be a datetime.datetime instance

現在,不管我設置的參數,我仍然得到錯誤。我也試着語法來源:https://www.bamboohr.com/api/documentation/changes.php

功能是:

def get_employee_changes(self, since=None): 
    """ 
    Returns a list of dictionaries, each with id, action, and lastChanged keys, representing 
    the employee records that have changed since the datetime object passed in the since= argument. 

    @return List of dictionaries, each with id, action, and lastChanged keys. 
    """ 
    if not isinstance(since, datetime.datetime): 
     raise ValueError("Error: since argument must be a datetime.datetime instance") 

    url = self.base_url + 'employees/changed/' 
    params = {'since': since.strftime('%Y-%m-%dT%H:%M:%SZ')} 
    r = requests.get(url, params=params, headers=self.headers, auth=(self.api_key, '')) 
    r.raise_for_status() 

    return utils.transform_change_list(r.content) 

感謝您的幫助

+3

顯然,雖然參數似乎有一個默認值,你**不能* *只需調用'bamboo.get_employee_changes()';你需要提供一個日期時間'since',你想看到變化。目前還不清楚爲什麼維護人員提供了無用的默認值。 – jonrsharpe

+2

我不明白爲什麼有人會使用默認值定義一個函數,然後在使用默認值時引發異常,也不知道爲什麼他們選擇檢查異常情況,然後引發異常,而不是僅僅嘗試任務,讓它提出自己的例外。 – TigerhawkT3

+0

這是一個奇怪的默認參數。 – miradulo

回答

0

正如你看到的在功能參數因爲類型日期時間的。預計日期時間爲

import datetime 
changes = bamboo.get_employee_changes(since=datetime.datetime.now() - datetime.timedelta(days=365)) 

應該給你的變化,去年以來

+0

謝謝你,馬蒂亞斯。奇蹟般有效 :) – user347977

0

傳遞datetime.datetime類型的變量,而調用函數bamboo.get_employee_changes()