예제 #1
0
    def insert(self):
        import pymongo.errors

        if self.overwrite:
            return self.save()

        autoinc_key = self.run_entry.get("_id") is None
        while True:
            if autoinc_key:
                c = self.runs.find({}, {"_id": 1})
                c = c.sort("_id", pymongo.DESCENDING).limit(1)
                self.run_entry["_id"] = (
                    c.next()["_id"] + 1 if self.runs.count_documents({}, limit=1) else 1
                )
            try:
                self.runs.insert_one(self.run_entry)
                return
            except pymongo.errors.InvalidDocument as e:
                raise ObserverError(
                    "Run contained an unserializable entry."
                    "(most likely in the info)\n{}".format(e)
                )
            except pymongo.errors.DuplicateKeyError:
                if not autoinc_key:
                    raise
예제 #2
0
    def final_save(self, attempts):
        import pymongo
        try:
            self.runs.update_one({'_id': self.run_entry['_id']},
                                 {'$set': self.run_entry},
                                 upsert=True)
            return

        except pymongo.errors.InvalidDocument:
            self.run_entry = force_bson_encodeable(self.run_entry)
            print(
                "Warning: Some of the entries of the run were not "
                "BSON-serializable!\n They have been altered such that "
                "they can be stored, but you should fix your experiment!"
                "Most likely it is either the 'info' or the 'result'.",
                file=sys.stderr)

            with NamedTemporaryFile(suffix='.pickle',
                                    delete=False,
                                    prefix='sacred_mongo_fail_') as f:
                pickle.dump(self.run_entry, f)
                print("Warning: saving to MongoDB failed! "
                      "Stored experiment entry in '{}'".format(f.name),
                      file=sys.stderr)

        raise ObserverError("Warning: saving to MongoDB failed!")
예제 #3
0
파일: mongo.py 프로젝트: songbingyu/sacred
 def save(self):
     try:
         self.runs.update_one({'_id': self.run_entry['_id']},
                              {'$set': self.run_entry})
     except pymongo.errors.InvalidDocument:
         raise ObserverError('Run contained an unserializable entry.'
                             '(most likely in the info)')
예제 #4
0
 def save(self):
     try:
         self.runs.save(self.run_entry)
     except AutoReconnect:
         pass  # just wait for the next save
     except InvalidDocument:
         raise ObserverError('Run contained an unserializable entry.'
                             '(most likely in the info)')
예제 #5
0
    def save(self):
        import pymongo

        try:
            self.runs.update_one({"_id": self.run_entry["_id"]},
                                 {"$set": self.run_entry})
        except pymongo.errors.InvalidDocument:
            raise ObserverError("Run contained an unserializable entry."
                                "(most likely in the info)")
예제 #6
0
    def save(self):
        import pymongo.errors

        try:
            self.runs.update_one({'_id': self.run_entry['_id']},
                                 {'$set': self.run_entry})
        except pymongo.errors.AutoReconnect:
            pass  # just wait for the next save
        except pymongo.errors.InvalidDocument:
            raise ObserverError('Run contained an unserializable entry.'
                                '(most likely in the info)')
예제 #7
0
    def insert(self):
        if self.overwrite:
            return self.save()

        autoinc_key = self.run_entry['_id'] is None
        while True:
            if autoinc_key:
                c = self.runs.find({}, {'_id': 1})
                c = c.sort('_id', pymongo.DESCENDING).limit(1)
                self.run_entry['_id'] = c.next()['_id'] + 1 if c.count() else 1
            try:
                self.runs.insert_one(self.run_entry)
            except InvalidDocument:
                raise ObserverError('Run contained an unserializable entry.'
                                    '(most likely in the info)')
            except DuplicateKeyError:
                if not autoinc_key:
                    raise
            return