2013-01-06 50 views
0

我的工作在Django的web應用程序,但我得到的Python版本2.6.6的錯誤,但它是在2.7的Python - 2.6

def check(attribute): 
    global rates 
    if (attribute.temperature_value < rates[0] or 
     attribute.temperature_value > rates[1] or 
     attribute.heartbeat_value<rates[2] or 
     attribute.heartbeat_value>rates[3]): 
     return True 
    else: 
     return False 

工作罰款2.7,但工作的錯誤Django調試器顯示有語法錯誤。

另外,如果我嘗試刪除功能我得到另一個錯誤在另一條線路上說

EOL while scanning string literal (views.py, line 109) 

上線109:

data = \ 
    DataPool(
    series= 
    [{'options': { 
    'source': values}, 
     'terms': [ 
     'current_time', 
     'temperature_value', 
     'heartbeat_value']} 
    ]) 

感謝您的幫助。

回溯:

File "/home/innovo/.local/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 
    101.        request.path_info) 
File "/home/innovo/.local/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve 
    300.      sub_match = pattern.resolve(new_path) 
File "/home/innovo/.local/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve 
    209.    return ResolverMatch(self.callback, args, kwargs, self.name) 
File "/home/innovo/.local/lib/python2.6/site-packages/django/core/urlresolvers.py" in callback 
    216.   self._callback = get_callable(self._callback_str) 
File "/home/innovo/.local/lib/python2.6/site-packages/django/utils/functional.py" in wrapper 
    27.   result = func(*args) 
File "/home/innovo/.local/lib/python2.6/site-packages/django/core/urlresolvers.py" in get_callable 
    92.     lookup_view = getattr(import_module(mod_name), func_name) 
File "/home/innovo/.local/lib/python2.6/site-packages/django/utils/importlib.py" in import_module 
    35.  __import__(name) 

Exception Type: SyntaxError at/
Exception Value: EOL while scanning string literal (views.py, line 114) 
+4

,你能否告訴既充分回溯的? –

+2

作爲一個說明,看到一個在全局變量上工作的函數是一個不好的跡象 - 而不是將參考作爲參數傳遞給參數。你也可以簡化'if x:return true; else:返回False'到'return x'。 –

+1

就我所知,字符串文字在2.6和2.7之間的工作方式應該沒有什麼區別,所以我對問題描述有點懷疑。 –

回答

2

我會寫這樣的:

def check(attr): 
    temp, pulse = attr.temperature_value, attr.heartbeat_value 
    def check_range(value, low, high): 
     return value < low or value > high 
    return check_range(temp, rates[0], rates[1]) or \ 
     check_range(pulse, rates[2], rates[3]) 

只有尾部的反斜槓就要解決你的問題,但。

不知道你的其他問題是什麼。這工作得很好:

class DataPool(object): 
    def __init__(self, series): 
     self.series = series 

values = list(range(5)) 

data = \ 
    DataPool(
    series= 
    [{'options': { 
    'source': values}, 
     'terms': [ 
     'current_time', 
     'temperature_value', 
     'heartbeat_value']} 
    ]) 

順便說一句,此代碼:

def check_range(value, low, high): 
    return value < low or value > high 

也可能是:

def check_range(value, low, high): 
    return not (low <= value <= high) 
+0

第一個問題解決了:)謝謝:)我正在檢查第二個問題。 – moenad

+0

你可以使用'return(check _...)'而不是'return ... \'。另外'check_range()'定義可以移到全局級別 – jfs