ゼロと無限の間に

フリーでオープンソースなJavaScriptとかPHPとか。

ユーザ用ツール

サイト用ツール


python-box:appengine-basehandler

Google App EngineのRequestHandlerを拡張+ちょっと便利な関数群 - AppEngine-BaseHandler

Google App Engine(Python版)にも少し慣れてきたので、よく使う関数をリサイクルできるようにしてみる。

selfに実装するものはRequestHandlerを拡張したBaseHandlerに、依存性の無いものはただのfunctionとして作ってみた。
Pythonの作法的にはどうなんだろう?

ライセンス

ソースコード

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
AppEngine BaseHandler
 
A little useful RequestHandler on Google App Engine,
and some common functions.
 
See: http://0-oo.net/sbox/python-box/appengine-basehandler
License: http://0-oo.net/MIT_license.txt (The MIT license)
'''
 
__author__ = 'dgbadmin@gmail.com'
__version__ = '0.2.0'
 
import xml.sax.saxutils
import webapp2
 
 
class BaseHandler(webapp2.RequestHandler):
 
  def p(self, out, brFlg=False):
    '''
    write out, with BR tag if you want
    '''
    self.response.out.write(out)
    if brFlg == True:
      self.response.out.write('<br />')
    self.response.out.write('\n')
 
 
  def simple_header(self, title, option=''):
    '''
    Minimum HTML header
    '''
    self.p('<html>')
    self.p('<head>')
    self.p('<title>' + h(title) + '</title>')
    self.p(option)
    self.p('</head>')
    self.p('<body>')
 
 
  def simple_footer(self):
    '''
    Minimum HTML footer
    '''
    self.p('</body>')
    self.p('</html>')
 
 
 
# Common functions
 
def h(out):
  '''
  HTML escape
  '''
  return xml.sax.saxutils.escape(out, {'"': "&quot;"})
 
 
def sort_dict(dic, by='key', reverse=False):
  '''
  Note: This function returns tuple 
  '''
  if by == 'key':
    i = 0
  elif by == 'value':
    i = 1
  else:
    raise Exception('Unexpected "by"')
 
  return sorted(dic.items(), lambda x,y : cmp(x[i], y[i]), reverse=reverse)
python-box/appengine-basehandler.txt · 最終更新: 2017/05/13 05:45 by dgbadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki