ero*_*ppa 0 python google-app-engine
我的Google App Engine项目只有一个代码文件.这个简单的文件有一个类,里面有几个方法.为什么这个python方法会出错,说全局名称没有定义?
Erro NameError:未定义全局名称"gen_groups"
import wsgiref.handlers
from google.appengine.ext import webapp
from django.utils import simplejson
class MainHandler(webapp.RequestHandler):
def gen_groups(self, lines):
""" Returns contiguous groups of lines in a file """
group = []
for line in lines:
line = line.strip()
if not line and group:
yield group
group = []
elif line:
group.append(line)
def gen_albums(self, groups):
""" Given groups of lines in an album file, returns albums """
for group in groups:
title = group.pop(0)
songinfo = zip(*[iter(group)]*2)
songs = [dict(title=title,url=url) for title,url in songinfo]
album = dict(title=title, songs=songs)
yield album
def get(self):
input = open('links.txt')
groups = gen_groups(input)
albums = gen_albums(groups)
print simplejson.dumps(list(albums))
def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
这是一个实例方法,你需要使用self.gen_groups(...)和self.gen_albums(...).
编辑:我猜TypeError你现在得到的是因为你删除了'自我'的论点gen_groups().你需要把它放回去:
def get_groups(self, lines):
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5305 次 |
| 最近记录: |