views:

308

answers:

3

I have a single code file for my Google App Engine project. This simple file has one class, and inside it a few methods. Why does this python method gives an error saying global name not defined?
Erro NameError: global name 'gen_groups' is not defined

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()
A: 

You need to call it explicitly with an instance:

groups = self.gen_groups(input)

Similarly for some of the other calls you're making in there, e.g. gen_album.

Also, see Knowing When to Use self and __init__ for more information.

ars
It gives TypeError: gen_groups() takes exactly 1 argument (2 given) when I added self.
erotsppa
This error is not going to appear from the code you showed us, where gen_groups is quite clearly defined to take 2 arguments: it follows that you must be running some DIFFERENT code instead.
Alex Martelli
No, after I added the self.gen_groups(input), it started giving that error
erotsppa
Are you saying that the ONLY difference from the code you pasted is the change to self.gen_groups(input)?
ars
I see the OP's not responding on this thread, but meanwhile he did accept an answer which basically is any use at all only if the code he showed is, as I said, NOT what he was running (specifically his def statement for gen_groups). I rarely ever downvote but I'm sure being SORELY tempted here, given this behavior...:-(
Alex Martelli
Alex, agreed. Your diagnosis was spot on and I'm not sure why the OP responded otherwise. From his profile, it is amusing that he gives more downvotes than upvotes (6:3)!
ars
+1  A: 

It's an instance method, you need to use self.gen_groups(...) and self.gen_albums(...).

Edit: I'm guessing the TypeError you are getting now is because you removed the 'self' argument from gen_groups(). You'll need to put it back in:

def get_groups(self, lines):
    ...
too much php
It gives TypeError: gen_groups() takes exactly 1 argument (2 given) when I added self.
erotsppa
It's not a class method. It's an instance method.
hughdbrown
@hughdbrown: Python is not my first language, forgive my slightly incorrect terminology.
too much php
A: 

You have to use it like this:

self.gen_groups(input)

There is not implicit "self" in Python.

Achim
It gives TypeError: gen_groups() takes exactly 1 argument (2 given) when I added self.
erotsppa