code: def ensure_session(engine_or_session):
      
      if isinstance(engine_or_session, Engine):
          ses = sessionmaker(bind=engine_or_session)()
          auto_close = True
      elif isinstance(engine_or_session, Session):
          ses = engine_or_session
          auto_close = False
      return ses, auto_close
docs: If it is an engine, then create a session from it. And indicate that
      this session should be closed after the job done.