def load_weights_from_db(self): collection = db.getCollection("pacman_weights") #Change this name to something different if you want to have your own persistence for personal debugging... but DO NOT COMMIT THE CHANGE OF THIS LINE! loaded = collection.find_one() #there is only one object in the database if loaded is None: print "Couldn't load the weights from the database. Going to go ahead and assume that the db was wiped, and set some fresh 0 weights in there." weights = {} collection.save({"the_weights": weights}) loaded_weights = collection.find_one()["the_weights"] else: loaded_weights = loaded["the_weights"] db.kthxbye(collection) #Because MongoDB can't store the Counter, but rather stores it as a dict, we re-create a counter (so we get nice 0 default values) from the dict by iteratively populating. Kinda slow, but meh. weights_counter = util.Counter() for key in loaded_weights: weights_counter[key] = loaded_weights[key] return weights_counter
def save_weights_to_db(self): collection = db.getCollection("pacman_weights") collection.update({}, {"$set": {"the_weights": self.weights}}) db.kthxbye(collection)