2016-06-25 16 views
1

私は竜巻とモータを使用しています。私は次のクラスを持っています:コンストラクタ内のDBの動的初期化

class N(): 
     def __init__(self,collectionName host='localhost', port=27017): 
     self.con=motor.MotorClient(host,port) 
     self.xDb=self.con.XDb 
     setattr(self,collectionName,self.xDb[collectionName]) 

これは実際に私が拡張したい親クラスです。子クラスは、このクラス 'を呼び出すと、collectionNameを設定します。問題は、このクラスにいくつかの他のメソッドもあるということです。

@tornado.gen.coroutine 
    def dropDB(self): 
     yield self.xDb.drop_collection(self.COLLECTION??) 

私は動的に私は自己を決定することができます方法です何のinitでコレクションを設定するため、上記の壊れています。変数私は基本メソッドで使用するように設定?別の変数を設定し

答えて

2

class N(): 
    def __init__(self, collectionName, host='localhost', port=27017): 
     # ... your existing code ... 
     self.collectionName = collectionName 

    @tornado.gen.coroutine 
    def dropDB(self): 
     yield self.xDb.drop_collection(self.collectionName) 

drop_collection名前またはMotorCollectionオブジェクトを取得するので、そこにあなたがselfにこのデータを格納することができ、他の方法がありますが、私が示した方法が最も簡単かもしれません。

http://motor.readthedocs.io/en/stable/api/motor_database.html#motor.motor_tornado.MotorDatabase.drop_collection

関連する問題