python程序优雅之美

因为我的c语言基础很不好,或者说基本等于没有学习过c,大家也可以想象我熟悉的pascal和c还是有很大的差别的。python、php、perl等很多脚本语言都受到了c的影响。我并不是很喜欢这种类c的语言的语法,当然还是因为不习惯和曾经太多与强大的delphi的根深蒂固的影响。

但是我看到基于google app engine的这段简单的python语言之后,我还是惊讶于pyhon的简洁,以及google的强大。(倒是很希望baidu可以推出类似的竞争产品,这样就可以不担心被屏蔽了)

import cgi
 
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
 
class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write("""
      <html>
        <body>
          <form action="/sign" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form>
        </body>
      </html>""")
 
 
class Guestbook(webapp.RequestHandler):
  def post(self):
    self.response.out.write('<html><body>You wrote:')
    self.response.out.write(cgi.escape(self.request.get('content')))
    self.response.out.write('</body></html>')
 
application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/sign', Guestbook)],
                                     debug=True)
 
def main():
  run_wsgi_app(application)
 
if __name__ == "__main__":
  main()

非常清楚的URL映射架构和相关的函数方法,清晰的html输出(虽然还很简单),一个主函数。

相关内容

This entry was posted in 编程 and tagged , . Bookmark the permalink.

2 Responses to python程序优雅之美

  1. lala says:

    外行了,没看出优雅之美

  2. py says:

    py确实不错。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">