2013-12-23 38 views
0

我升級我的Mac OS 10.9環境到Python 3.3,現在當我運行一個Python腳本的lib我得到以下錯誤:的Python 3加薪類型錯誤,「Time.milliseconds預計DateTime對象

加薪類型錯誤」 Time.milliseconds預計日期時間對象

這裏的分離的代碼,顯然會導致錯誤:

@classmethod 
def to_unix(cls, timestamp): 
    """ Wrapper over time module to produce Unix epoch time as a float """ 
    if not isinstance(timestamp, datetime.datetime): 
     raise TypeError, 'Time.milliseconds expects a datetime object' 
    base = time.mktime(timestamp.timetuple()) 
    return base 

,這裏是在整個部分,其中上述代碼駐留的代碼:

@classmethod 
def from_unix(cls, seconds, milliseconds = 0): 
    """ Produce a full |datetime.datetime| object from a Unix timestamp """ 
    base = list(time.gmtime(seconds))[0:6] 
    base.append(milliseconds * 1000) # microseconds 
    return datetime.datetime(* base) 

@classmethod 
def to_unix(cls, timestamp): 
    """ Wrapper over time module to produce Unix epoch time as a float """ 
    if not isinstance(timestamp, datetime.datetime): 
     raise TypeError, 'Time.milliseconds expects a datetime object' 
    base = time.mktime(timestamp.timetuple()) 
    return base 

@classmethod 
def milliseconds_offset(cls, timestamp, now = None): 
    """ Offset time (in milliseconds) from a |datetime.datetime| object to now """ 
    if isinstance(timestamp, (int, float)): 
     base = timestamp 
    else: 
     base = cls.to_unix(timestamp) 
     base = base + (timestamp.microsecond/1000000) 
    if now is None: 
     now = time.time() 
    return (now - base) * 1000 

這可能是引用這些庫之一:

import date time 
    import time 

任何想法?升級前我沒有這個錯誤,所以可能是重新定義了時間戳或日期時間?

謝謝

+0

您沒有顯示調用代碼。追溯會很好。 –

回答

3

我不認爲這有什麼關係這一點。

相反,我覺得你得到一個SyntaxError因爲你的raise語法是在Python 3.X違法:

>>> raise TypeError, 'error' 
    File "<stdin>", line 1 
    raise TypeError, 'error' 
       ^
SyntaxError: invalid syntax 

相反,它必須是這樣的:

>>> raise TypeError('error') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: error 
>>> 

這裏是reference

+0

謝謝!這看起來很熟悉。我會在今天晚些時候對它進行測試,如果它有效,請覈准答案。 – Jazzmine