[docs]classChemicalRegistry:""" A registry for storing and retrieving chemical information. Uses a local database to store chemicals, and queries PubChem if a chemical is not found. """uow:ChemicalUnitOfWorkpubchem:PubChemService
[docs]defget_chemical(self,name:str)->Chemical:""" Get a chemical by name. If the chemical is not found in the local database, query PubChem for the chemical information. """withself.uow:chemical=self.uow.repo.get_by("name",name.lower())ifchemicalisnotNone:returnchemicallogger.info(f"Chemical {name} not found in local database, querying PubChem")chemical=self.pubchem.lookup(name)ifchemicalisNone:logger.error(f"Chemical {name} not found in PubChem")raiseValueError(f"Chemical {name} not found")self.uow.repo.add(chemical)self.uow.session.expunge(chemical)self.uow.commit()returnchemical
[docs]defadd_chemical(self,chemical:Chemical):""" Add a chemical to the local database. """withself.uow:ifself.uow.repo.get_by("name",chemical.name)isnotNone:raiseValueError(f"Chemical {chemical.name} already exists")chemical.name=chemical.name.lower()self.uow.repo.add(chemical)self.uow.commit()