0

can anyone tell me please the difference between these two code snippets:

1.

import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
  def get(self):
    user = users.get_current_user()

    if user:
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.out.write('Hello, ' + user.nickname())
    else:
      self.redirect(users.create_login_url(self.request.uri))

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

2.

import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
  def get(self):
    user = users.get_current_user()

    if user:
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.out.write('Hello, ' + user.nickname())
    else:
      self.redirect(users.create_login_url(self.request.uri))

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

The thing is that i'm trying to go over Google AppEngine introduction material and whenever i try to type the code myself, something's different and it doesn't work. And whenever i just copy it from their website, it works, although it looks identical. Checked the coding in the View panel, it's same, ANSI (i'm using Notepad++). Tried to play with indentation as well and it didn't made any difference.

Any comments would be appreciated. Thank you.

1
  • What happens if you copy-paste it, then change ONE line of code? If it works keep changing lines until everyone has been. Honnestly this does not make much sense. Unless everytime you type it you make a mistake. Or there is some other factor you are not talking about.
    – Iznogood
    Nov 29, 2012 at 0:35

2 Answers 2

0

You should always start your Pyhton code with these 3 lines:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

And your editor should encode in utf-8. Do not use ANSI.

The last line is optional, but I recommend it, to stay away from encoding problems. There are a few exceptions. So if you use unicode literals, you have to change the line with the headers:

import webapp2
from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()

        if user:
            self.response.headers['Content-Type'.encode()] = 'text/plain'.encode()
            self.response.out.write('Hello, ' + user.nickname())
        else:
            self.redirect(users.create_login_url(self.request.uri))

app = webapp2.WSGIApplication([('/', MainPage)],
                          debug=True)

And your app.yaml should look like:

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: false

handlers:
- url: /.*
  script: helloworld.app

libraries:
- name: webapp2
  version: latest

But Notepad++ is not a good development environment for app engine. You need a good Python IDE. I recommend Eclipse with PyDev. To setup, use this totorial see this question: Debug google app engine project line by line

And if you are a total newbie? go to this great web development course using google app engine : http://www.udacity.com/overview/Course/cs253/CourseRev/apr2012

1
  • Still wasn't able to figure out the problem, apart that when i switched to SublimeText everything's fine now.
    – Belhor
    Dec 2, 2012 at 20:17
0

Found a problem, it was indentation thing. You can read about this here and here There's difference whether you are using "space" or "tab" to indent and it can cause problems, also problems can occur from settings in your program (in my case in Sublime Text's setting of "Tab" indent,it can be set to 4 or 8 etc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.