def test_init_file(self): """Create DBConfig from file or path. """ # sending file obj, or its path, should have same effect d1 = DBConfig(config_file=self.tmp) d2 = DBConfig(config_file=self.tmp.name) self.assertEqual(d1.settings, d2.settings) self.assertEqual(d2.settings, self._aliased_cfg())
def test_init_file(self): """Create DBConfig from file or path. """ # sending file obj, or its path, should have same effect tmp = "dbconfigtest.json" with open(tmp, "w") as f: json.dump(self.cfg, f) f.flush() # reset file to beginning with open(tmp, "r") as f: d1 = DBConfig(config_file=f) d2 = DBConfig(config_file=tmp) self.assertEqual(d1.settings, d2.settings) self.assertEqual(d2.settings, self._aliased_cfg())
def __init__(self, config_dict=None): """ enabled: no database: abinit collection: test #host: 0.0.0.0 #port: 8080 #user: gmatteo #password: helloworld """ self.config = {} # Don't initialize DBConfig if not dict or dict contains `enabled: no` if config_dict and config_dict.pop("enabled", True): self.config = DBConfig(config_dict=config_dict)
def store_reaxys_reactions_db(reactions, dbfile="db.json", collection="reactions"): """ Insert reaction information into a MongoDB database. :param reactions: List of reactions, defined as above. :param dbfile: A config file indicating the database into which the reactions will be inserted. :param collection: Collection within the database in which to store the reactions. :return: List of rxn_ids that were """ # Set up MongoDB database with pymatgen-db and pymongo config = DBConfig(config_file=dbfile) client = MongoClient(config.host, config.port, username=config.user, password=config.password) db = client[config.dbname] collection = db[collection] just_added = [] if "rxn_id" not in collection.index_information(): collection.create_index("rxn_id", unique=True) for reaction in reactions: try: reaction["rxn_id"] = reaction["meta"]["rxn_id"] collection.insert_one(reaction) just_added.append(reaction["rxn_id"]) except: continue return just_added
def test_init_dict(self): """Create DBConfig from dict object. """ d1 = DBConfig(config_dict=self.cfg) self.assertEqual(d1.settings, self._aliased_cfg())
def get_settings(config_file): cfg = DBConfig(config_file) return cfg.settings