def setup(self): db = Database.get_instance() Database.drop() for k in range(self.N): db['test'].insert({ 'param1': k, "param2": "value%d" % k, "param3": (k%2==0) })
def save(self): ''' Save the list of object in bulk ''' if len(self) > 0: cls = self.__getitem__(0).__class__ if not hasattr(cls, 'getClassName'): raise Exception("Objects must extend BaseObject") col = Database._get_collection(cls.getClassName()) objs = list() for o in self: doc = o.toDict() doc['_id'] = o.getId() del doc['id'] objs.append(doc) col.insert(objs)
model_file = sys.argv[sys.argv.index('-m') + 1] class_path = sys.argv[sys.argv.index('-p') + 1] over_write = "--force" in sys.argv if over_write: print "WARNING: override enabled" # Build model from config start_time = time.time() model = ConfigParser().parse(model_file).get() print "Building model" filenames = dict() for cls_name in model.keys(): file_name = build_model_class(class_path, cls_name, model.get(cls_name), over_write) filenames[file_name] = True # Clean old file print "Cleaning up" for filename in os.listdir(class_path): if filename.find("__") < 0 and not filenames.has_key(filename): os.remove(os.path.join(class_path, filename)) print "Deleted %s" % filename # Ensure indexes Database.build_indexes(model) print "Built model in %.3f seconds" % (time.time() - start_time)
def _get_db_inst(self): ''' Return the database instance ''' if not self.db: self.db = Database.get_instance() return self.db