2009-09-17 24 views
24
class TestSpeedRetrieval(webapp.RequestHandler): 
    """ 
    Test retrieval times of various important records in the BigTable database 
    """ 
    def get(self): 
     commandValidated = True 
     beginTime = time() 
     itemList = Subscriber.all().fetch(1000) 

     for item in itemList: 
      pass 
     endTime = time() 
     self.response.out.write("<br/>Subscribers count=" + str(len(itemList)) + 
      " Duration=" + duration(beginTime,endTime)) 

私はクラスの名前を渡す関数に上記を有効にするにはどうすればよいですか? 上記の例では、Subscriber.all()。fetchステートメントのSubscriberはクラス名で、Google BigTableでPythonでデータテーブルを定義する方法です。あなたは "「このように」との間に、あなたのコードのように、直接クラスのオブジェクトを渡すと場合Python:クラス名を関数のパラメータとして渡しますか?

 TestRetrievalOfClass(Subscriber) 
or  TestRetrievalOfClass("Subscriber") 

おかげで、 ニール・ウォルターズ

答えて

17
class TestSpeedRetrieval(webapp.RequestHandler): 
    """ 
    Test retrieval times of various important records in the BigTable database 
    """ 
    def __init__(self, cls): 
     self.cls = cls 

    def get(self): 
     commandValidated = True 
     beginTime = time() 
     itemList = self.cls.all().fetch(1000) 

     for item in itemList: 
      pass 
     endTime = time() 
     self.response.out.write("<br/>%s count=%d Duration=%s" % (self.cls.__name__, len(itemList), duration(beginTime,endTime)) 

TestRetrievalOfClass(Subscriber) 
+0

クール、私はあなただけで、他の変数のようにクラスを渡すことができます知りませんでした。私はそのつながりを失っていた。 – NealWalters

+7

関数、メソッド、クラス、モジュールすべては、あなたが回ることができるPythonのファーストクラスのオブジェクトです –

10

は、私はこのような何かをしたいですまたは "、 の名前を__name__属性として取得できます。 (後あなたのコードのように、「または」)の名前を皮切り

はそれが本当に難しい(とは明確な)になりますが、クラスオブジェクトが含まれていてもよい場所についての何らかの指示がない限り、クラスオブジェクトを取得する - ので、代わりにクラスオブジェクトを渡すのはなぜですか?

+1

+1:クラスはファーストクラスのオブジェクトです - クラスを変数やパラメータなどに割り当てることができます。これはC++ではありません。 –

+0

OBJ = globals()[className]()classNameは、クラス名の文字列を含む変数です。 http://www.python-forum.org/pythonforum/viewtopic.php?f=3&t=13106 – NealWalters

+1

@NealWalters、クラスは上部に比べて他の場所で定義されている場合は動作しません。ここに古いポストからこれを手に入れました現在のモジュール(関数内、別のクラス内、別のモジュール内、など)にレベルがあるので、一般的には良い考えではありません。 –

1

私が使用したNedのコードのわずかなバリエーションです。これはWebアプリケーション ですので、getルーチンをURL:http://localhost:8080/TestSpeedRetrievalで実行して起動します。私はのinitの必要性を見ていませんでした。

class TestSpeedRetrieval(webapp.RequestHandler): 
    """ 
    Test retrieval times of various important records in the BigTable database 
    """ 
    def speedTestForRecordType(self, recordTypeClassname): 
     beginTime = time() 
     itemList = recordTypeClassname.all().fetch(1000) 
     for item in itemList: 
      pass # just because we almost always loop through the records to put them somewhere 
     endTime = time() 
     self.response.out.write("<br/>%s count=%d Duration=%s" % 
     (recordTypeClassname.__name__, len(itemList), duration(beginTime,endTime))) 

    def get(self): 

     self.speedTestForRecordType(Subscriber) 
     self.speedTestForRecordType(_AppEngineUtilities_SessionData) 
     self.speedTestForRecordType(CustomLog) 

出力:

Subscriber count=11 Duration=0:2 
_AppEngineUtilities_SessionData count=14 Duration=0:1 
CustomLog count=5 Duration=0:2 
関連する問題