def add_history_to_db(self, history_dict): """add df to posrgresql database Args: df (pd.DataFrame): _description_ """ history_dict["layers"] = self.n_layers history_dict["neurons"] = self.fc_dim history_dict["batch_size"] = self.batch_size history_dict["epochs"] = self.epoch_avg history_dict["method"] = self.method history_dict["name"] = self.name history_dict["lr"] = self.lr history_dict["n_avg"] = self.n_avg history_dict["n_patience"] = self.n_patience history_dict["csv_dir"] = str(self.csv_dir) history_dict["data_name"] = str(self.data_name) history_dict["description"] = str(self.case_detail) history_dict["client"] = MachineConfig.client_name if history_dict["remain_parameters"]: history_dict["remain_parameters"] = list( map(str.strip, history_dict["remain_parameters"].split(","))) if history_dict["choosed_parameters"]: history_dict["choosed_parameters"] = list( map(str.strip, history_dict["choosed_parameters"].split(","))) if self.cv: history_dict["cv"] = self.cv history_dict["n_splits"] = self.n_splits history_dict["test_ratio"] = self.test_ratio if self.is_test: history_dict["is_test"] = self.is_test if self.method == "MIXED": history_dict["alpha"] = self.alpha if self.data_class == "Duct": history_step = models.DuctTable(**history_dict) elif self.data_class == "Toy": history_step = models.ToyTable(**history_dict) elif self.data_class == "Bubble": history_step = models.BubbleTable(**history_dict) else: raise "Not implemented yet" db = SessionLocal() try: db.add(history_step) db.commit() db.refresh(history_step) finally: db.close()
async def websocket_endpoint(websocket: WebSocket, user: str): await manager.connect(websocket) db = None try: db = SessionLocal() while True: data = await websocket.receive_text() await manager.send_personal_message(data, websocket) except WebSocketDisconnect: if db: db.close() manager.disconnect(websocket)
def get_current_user(token: str = Depends(oauth2_scheme)): credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) try: payload = jwt.decode(token, Config.SECRET_KEY, algorithms=[Config.ALGORITHM]) nickname: str = payload.get("sub") if nickname is None: raise credentials_exception except PyJWTError: raise credentials_exception db = SessionLocal() user = get_user_by_nickname(db, nickname) db.close() if user is None: raise credentials_exception return user
def get_db() -> Generator: try: db = SessionLocal() yield db finally: db.close()
def get_db(): db = SessionLocal() try: yield db finally: db.close()