I have built a web app in Python for Google App Engine. It is mature piece of code that I have run many times without any problems. However, when I was making some changes this morning, mysterious white space errors started popping up all over the place. I figured out what was going on and removed a few of them by hand before detabbing the whole text file (in TextWrangler) and changing my settings to auto-expand tabs. I think I have chased out all the bugs. When I run python -m tabnanny on my file, I get no reported errors.
However, a new bug has appeared in a previously solid chunk of code. I don’t know if the error is in the class or previous class it’s calling so I am including a big chunk of code below. The error message in GAE log is as follows:
<type 'exceptions.NameError'>: name 'self' is not defined
Traceback (most recent call last):
File "/base/data/home/apps/lpflipstud/1.354982193405081399/example.py", line 99, in <module>
class HomeHandler(BaseHandler):
File "/base/data/home/apps/lpflipstud/1.354982193405081399/example.py", line 103, in HomeHandler
logging.info(self.current_user)
Here is the code. It is a slightly modified version of sample code provided by facebook for Google App Engine interface with a facebook app:
class BaseHandler(webapp.RequestHandler):
#Provides access to the active Facebook user in self.current_user.
#The property is lazy-loaded on first access, using the cookie saved
#by the Facebook JavaScript SDK to determine the user ID of the active
#user. See http://developers.facebook.com/docs/authentication/ for
#more information.
@property
def current_user(self):
if not hasattr(self, "_current_user"):
self._current_user = None
cookie = facebook.get_user_from_cookie(
self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET)
if cookie:
# Store a local instance of the user data so we don't need
# a round-trip to Facebook on every request
user = User.get_by_key_name(cookie["uid"])
if not user:
graph = facebook.GraphAPI(cookie["access_token"])
profile = graph.get_object("me")
id=str(profile["id"])
at = cookie["access_token"]
user = User(key_name=str(profile["id"]),
id=str(profile["id"]),
name=profile["name"],
profile_url=profile["link"],
access_token=cookie["access_token"])
user.put()
elif user.access_token != cookie["access_token"]:
user.access_token = cookie["access_token"]
user.put()
#add list of friend ids
tic = time.clock()
url = "https://graph.facebook.com/"+user.id+"/friends? access_token="+user.access_token
response = urllib2.urlopen(url)
fb_response = response.read()
x = re.findall(r'"\d+"',fb_response)
friend_list = [a.strip('"') for a in x]
user.friends = friend_list
toc = time.clock()
t = toc - tic
user.put()
self._current_user = user
return self._current_user
class HomeHandler(BaseHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), "homepage.html")
logging.info(self.current_user)
logging.info(self.current_user)is outside of any scope that definesself(and would run when the class is being created, which is probably not what you want). You need to call it inside a method, e.g.