繁体   English   中英

Python - 工作2.7但2.6上的错误

[英]Python - Working on 2.7 but error on 2.6

我正在开发一个关于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)

我会这样写:

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']}
    ])

BTW,这段代码:

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

也可能是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM