0

我正在 Django 上开发一个 webapp,但在 Python 2.6.6 版本上出现错误,但它在 2.7 上运行良好

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

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

1 回答 1

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)
于 2013-01-06T17:59:16.137 回答