2012-01-11 2 views
0

私はまだ1年も経っていないのに、Pythonにはまだ新しく、Googleのアプリエンジンでかなり大きなプロジェクトを構築することでそれを学んできました。これはコードとHTMLテンプレートの10k +ラインの巨大さに成長しました。私はかなり厳しいMVCアーキテクチャに従うためにコードのかなりの部分をリファクタリングしています。GAE Pythonのクラスメソッド "テンプレート"を作成

私の質問はpythonに直接関係するものです。私は自分が作りたいものを正確に言う言葉を知らないので、例を使用したいと思います。

これは、ビューを表示するための私の現在の「基本」のコードです:

class MainHandler(webapp.RequestHandler): 
    def get(self): 
     tUser = users.get_current_user() 
     tContext = { 
      'user':  tUser, 
      'login':  users.create_login_url(self.request.uri), 
      'logout': users.create_logout_url(self.request.uri), 
     } 

     #User is logged in 
     if (tUser): 
      #code for loading view information and adding to the context dict 
      #CUSTOMIZATION GOES HERE 
     else: 
      self.redirect("/") 

     tmpl = os.path.join(os.path.dirname(__file__), 'logged-in.html') 
     self.response.out.write(render(tmpl, tContext)) 

私は多分、「カスタマイズを追加/先頭に追加の方法で、この定型コードとそれ何とか抽象を利用したいと思います"各クラスメソッドのコード?

私は何とかこれを行うためにデコレータを使用することができるかもしれないと思うが、私は正しい方向に私を指すようにstackoverflowの外で何Pythonのメンターを持っていません。私は可能な最もpythonicメソッドを好むか、少なくとも一般的にこの状況で "ベストプラクティス"と考えられているものを好むでしょう。

pythonのバージョンは2.7.2です。

編集

注、私はデコレータでこれを行うことができれば、私は完全に異なるクラスとのpythonファイルからデコレータを呼び出すことができるようにするために、そして何が必要なのか?デコレータを1つのファイルに入れて他の場所から参照できるようにしたいので、私のコードは合理的であるように正規化されています。 =)

編集2

これは私がコンソールに出て働いていたテストコードであり、私は夜のために残しているか、私はより多くのそれを洗練でしょう。しかし、これはクラスのプロパティにアクセスして修正するように見えます。これは、GAEでこれを取り除く必要があると思います。

class Decorators(): 
@staticmethod 
def BeginInit(pInFunction): 
    def OutFunction1(self): 
     print str(self.innerv) 
     pInFunction(self) 
    return OutFunction1 

@staticmethod 
def EndInit(pInFunction): 
    def OutFunction2(self): 
     self.innerv = 5 
     pInFunction(self) 
     print "this is test 3" 
    return OutFunction2 

class Test2Handler(): 
    innerv = 10 
    @Decorators.BeginInit 
    @Decorators.EndInit 
    def TestPrint(self): 
     print self.innerv 
     print "this is test 2" 

プリント

10 
5 
this is test 2 
this is test 3 

答えて

1

代わりにデコレータを使用しての、あなたはとても

class HandlerBase(webapp.RequestHandler): 

    def get_context(self): 
     return {} 

    def get(self): 
     tUser = users.get_current_user() 
     tContext = { 
      'user':  tUser, 
      'login':  users.create_login_url(self.request.uri), 
      'logout': users.create_logout_url(self.request.uri), 
     } 
     # tContext holds the defaults for your context 

     #User is logged in 
     if (tUser): 
      # if the dict returned by self.get_context() contains a key 
      # that's already in tContext, tContext[key] will be overridden 
      # with self.get_context()[key] 
      tContext.update(self.get_context()) 
     else: 
      self.redirect("/") 

     tmpl = os.path.join(os.path.dirname(__file__), 'logged-in.html') 
     self.response.out.write(render(tmpl, tContext)) 


class MainHandler(HandlerBase): 

    def get_context(self): 
     # the contents of the following dict will be added to 
     # the context in HandlerBase 
     return {'greeting': 'Hi!'} 
+0

興味深いのように、ええ、これがまたうまくいく、あなたの要求ハンドラの基本クラスを使用することができます。 =) –

関連する問題