Return random ORM instance.

        :type engine_or_session: Union[Engine, Session]
        :type limit: int

        :rtype: List[ExtendedBase]
163997
def random(cls, engine_or_session, limit=5):
        
        ses, auto_close = ensure_session(engine_or_session)
        result = ses.query(cls).order_by(func.random()).limit(limit).all()
        if auto_close:  # pragma: no cover
            ses.close()
        return result
299819
def cloneQuery(self, limit=_noItem, sort=_noItem):
        
        newq = self.query.cloneQuery(limit=limit, sort=sort)
        return self.__class__(newq)
889378
def create_database_session(engine):
    """"""

    try:
        Session = sessionmaker(bind=engine)
        return Session()
    except OperationalError as e:
        raise DatabaseError(error=e.orig.args[1], code=e.orig.args[0])
1114440
def get_random(self):
        
        from random import randint

        count = self.count()

        if count < 1:
            raise self.EmptyDatabaseException()

        random_integer = randint(0, count - 1)

        statements = self.statements.find().limit(1).skip(random_integer)

        return self.mongo_to_object(list(statements)[0])
1331338
def get_or_create(cls, **kwargs):
        '''
        
        '''
        q = cls._get_instance(**kwargs)
        if q:
            return q
        q = cls(**kwargs)
        _action_and_commit(q, session.add)
        return q
80074
def from_everything(everything, engine, limit=None):
    
    if isinstance(everything, Table):
        return from_table(everything, engine, limit=limit)

    if type(everything) is DeclarativeMeta:
        return from_object(everything, engine, limit=limit)

    if isinstance(everything, Query):
        return from_query(everything, engine, limit=limit)

    if isinstance(everything, Select):
        return from_sql(everything, engine, limit=limit)

    if isinstance(everything, ResultProxy):
        return from_resultproxy(everything)

    if isinstance(everything, list):
        return from_data(everything)
274741
def random(self, *args, **kwargs):
        
        indexer = Random()
        self.add(indexer)

        return self
581628
def get_random(self):
        
        available = list(self.unchecked | self.good)
        if not available:
            return None
        return random.choice(available)
516742
def hydrate(cls, db, limit):
        

        # Extract the limit name from the keyword arguments
        cls_name = limit.pop('limit_class')

        # Is it in the registry yet?
        if cls_name not in cls._registry:
            utils.find_entrypoint(None, cls_name)

        # Look it up in the registry
        cls = cls._registry.get(cls_name)

        # Instantiate the thing
        return cls(db, **limit) if cls else None
537855
def generator(self, random, args):
        """"""
        if self.duplicates:
            max_count = [self.capacity // item[0] for item in self.items]
            return [random.randint(0, m) for m in max_count]
        else:
            return [random.choice([0, 1]) for _ in range(len(self.items))]
939842
def random_new(algo: int = RNG_CMWC) -> tcod.random.Random:
    
    return tcod.random.Random(algo)
826338
def connect_engine(self):
        
        try:
            self.connection = self.engine.connect()
            return True
        except sa.exc.OperationalError as opex:
            LOG.fatal("Could not connect to the database. The error was: '%s'",
                      str(opex))
        return False
306973
def _use(cls, ec):
        
        # class ConnectModel(cls):
        #     pass
        ConnectModel = type(cls.__name__, (cls,), {})

        ConnectModel.tablename = cls.tablename
        ConnectModel._base_class = cls
        if isinstance(ec, (str, unicode)):
            ConnectModel._engine_name = ec
        elif isinstance(ec, Session):
            ConnectModel._engine_name = ec.engine_name
            ConnectModel._connection = ec
        return ConnectModel
1002356
def _create(cls, model_class, *args, **kwargs):
        """"""
        session = cls._meta.sqlalchemy_session
        session_persistence = cls._meta.sqlalchemy_session_persistence
        if cls._meta.force_flush:
            session_persistence = SESSION_PERSISTENCE_FLUSH

        obj = model_class(*args, **kwargs)
        if session is None:
            raise RuntimeError("No session provided.")
        session.add(obj)
        if session_persistence == SESSION_PERSISTENCE_FLUSH:
            session.flush()
        elif session_persistence == SESSION_PERSISTENCE_COMMIT:
            session.commit()
        return obj
388554
async def random_connection(self):
        
        addr = choice(list(self._connections.keys()))
        return await self.env.connect(addr)
710497
def pick_a_model_randomly(models: List[Any]) -> Any:
    
    try:
        return random.choice(models)
    except IndexError as e:
        raise ModelPickerException(cause=e)
355926
def get_session(url):
    
    engine = create_engine(url)
    db_session = scoped_session(sessionmaker(engine))
    Base.metadata.create_all(engine)
    return db_session