Apache上的Python脚本生成奇怪的“名称未定义”错误

时间:2014-01-05 12:46:35

标签: python-2.7 apache2

我正在用Python编写一个wsgi脚本,我收到以下错误:

mod_wsgi (pid=22229): Exception occurred processing WSGI script '/srv/www/wsgi/rtt_by_sig-strength.py'., referer: http://t000.droid.net.in.tum.de/
Traceback (most recent call last):, referer: http://t000.droid.net.in.tum.de/
  File "/srv/www/wsgi/rtt_by_sig-strength.py", line 67, in application, referer: http://t000.droid.net.in.tum.de/
    allData = measrFilter.getFilteredList(["timestamp", "device_id", "device_name", "rtt"]), referer: http://t000.droid.net.in.tum.de/
  File "/srv/www/wsgi/measrFilter.py", line 23, in getFilteredList, referer: http://t000.droid.net.in.tum.de/
    elif dataPoint == "device_id":, referer: http://t000.droid.net.in.tum.de/
NameError: global name 'getFlatPathNames' is not defined, referer: http://t000.droid.net.in.tum.de/

这是脚本(我想添加行号,但不知道如何):
编辑:请注意,脚本的详细内容实际上并不重要。只需略过它就可以看到错误了。

import json
import os

PATH = "/srv/www/data/client"
device_names = {}

def avgList(listToAvg):
    result = 0
    for val in listToAvg:
        result = result + val
    return result / len(listToAvg)  

def getFlatPathNames(path):
    flatdir = [os.path.join(root, name)
                for root, dirs, files in os.walk(path)
                for name in files
                if name.endswith((".dat"))]
    return flatdir

def getDataPoint(jsonDat, dataPoint):
    if dataPoint == "timestamp":
        return jsonDat["result"]["timestamp"]
    elif dataPoint == "device_id":
        return jsonDat["result"]["device_id"]
    elif dataPoint == "device_name":
        device_id = jsonDat["result"]["device_id"]
        if not device_id in device_names:
            device_name = len(device_names)
            device_names[device_id] = device_name
        return device_names[device_id]
    elif dataPoint == "rtt":
        return avgList(jsonDat["result"]["network"]["measr"]["latency"][0]["rtt_ms"])
    else:
        raise ValueError("Unsupported dataPoint")

def getFilteredList(dataPoints):
    returnValue = []
    for path in getFlatPathNames(PATH):
        txt = open(path, "r").read()
        datList = ['{' + x + '}' for x in txt.strip('{}').split('}{')] # TODO: This is a huge timewaster, I think. We should really think of another way of parsing several json data from one file
        for dat in datList:
            item = []
            jsonDat = json.loads(dat)
            try:
                for dataPoint in dataPoints:
                    item.append(getDataPoint(jsonDat, dataPoint))
                returnValue.append(item)
            except (TypeError, ZeroDivisionError):
                continue
    return returnValue

由于以下几个原因,我对这个错误感到很困惑:

  1. 第23行不在函数“getFilteredList()”中作为错误状态但在“getDataPoint()”中。
  2. 这一行附近的函数是“getFlatPathNames()”函数。
  3. “getFlatPathNames()”在同一个文件中定义,并在调用它的位置上方。
  4. 有人可以澄清吗? THX!

0 个答案:

没有答案