我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用sqlalchemy.func.random()。
def generate_fake(count=100): from random import seed, randint import forgery_py seed() for i in range(count): w = Word(content=forgery_py.lorem_ipsum.word(), eg=forgery_py.lorem_ipsum.sentence(), rank=randint(0, 3), USA_voice=forgery_py.basic.text(spaces=False), UK_voice=forgery_py.basic.text(spaces=False), translations=forgery_py.lorem_ipsum.sentence(), phonetic_symbol='[' + forgery_py.lorem_ipsum.word() + ']' ) if not w.query.filter_by(content=w.content).first(): db.session.add(w) db.session.commit()
def _check_update_remote_address(self, msg, peer): if not msg.sender_address: return msghost, msgport = msg.sender_address.split(':') dbhost, dbport = peer.address.split(':') if msghost != dbhost and msghost == peer.protocol.address[0]: # Allow the Peer to update our records of its host if it told us to # and it matches where its connection is coming from. We only do it # if it told us to to prevent wierd outgoing only routes or such to # cause any problems. We also only trust it if those match so it # can't make us connect to random addresses or cause problems or # whatever. if log.isEnabledFor(logging.INFO): log.info(\ "Remote Peer said address [{}] has changed, updating our"\ " record [{}].".format(msg.sender_address, peer.address)) peer.address = "{}:{}".format(msghost, msgport) elif msgport != dbport: if log.isEnabledFor(logging.INFO): log.info(\ "Remote Peer said port [{}] has changed, updating our"\ " record [{}].".format(msgport, dbport)) peer.address = "{}:{}".format(dbhost, msgport) else: return def dbcall(): with self.node.db.open_session() as sess: dbp = sess.query(Peer).get(peer.dbid); dbp.address = peer.address sess.commit() yield from self.loop.run_in_executor(None, dbcall)
def init_words(self): first_words = Word.query.filter_by(rank=self.rank).order_by(func.random()).limit(self.word_totals).all() self.words += first_words db.session.add(self) db.session.commit() return self.words # ?????????????????? # ??????????0.17
def generate_new_words(self, proportion=0.17): today_new_words = [] cu_new_word_totals = int(self.word_totals * proportion) words = Word.query.filter_by(rank=self.rank).order_by(func.random()).all() i = 0 for new_word in words: if new_word not in self.words: today_new_words.append(new_word) i += 1 if i == cu_new_word_totals: break return today_new_words
def get_next_response(user_id, exercise_id): """ Get a random ungraded response or throw an exception Parameters ---------- user_id : int exercise_id: int Returns ------- response The response object from a list of responses """ response = Response.get_random_ungraded_response(user_id, exercise_id, active=True) grades = ResponseGrade.get_grades(user_id, exercise_id, active=True) labels = [int(grade[1]) if grade[1] else grade[1] for grade in grades] labels_array = np.array(labels, dtype=float) unique, counts = np.unique(labels_array[~np.isnan(labels_array)], return_counts=True) stats = dict(zip(unique, counts)) stats['Total:'] = len(labels) log.info(stats) if response: return response else: raise ResponsesNotFound()
def get_random_ungraded_response(cls, user_id, exercise_id, active=None): subquery = db.session.query(ResponseGrade.response_id) \ .join(Response).filter(ResponseGrade.user_id == user_id).subquery() query = db.session.query(Response).filter( Response.exercise_id == exercise_id) \ .filter(~Response.id.in_(subquery)).filter().order_by(func.random()) if active: query = query.filter(Response.active == True) return query.first()
def tablesample(selectable, sampling, name=None, seed=None): """Return a :class:`.TableSample` object. :class:`.TableSample` is an :class:`.Alias` subclass that represents a table with the TABLESAMPLE clause applied to it. :func:`~.expression.tablesample` is also available from the :class:`.FromClause` class via the :meth:`.FromClause.tablesample` method. The TABLESAMPLE clause allows selecting a randomly selected approximate percentage of rows from a table. It supports multiple sampling methods, most commonly BERNOULLI and SYSTEM. e.g.:: from sqlalchemy import func selectable = people.tablesample( func.bernoulli(1), name='alias', seed=func.random()) stmt = select([selectable.c.people_id]) Assuming ``people`` with a column ``people_id``, the above statement would render as:: SELECT alias.people_id FROM people AS alias TABLESAMPLE bernoulli(:bernoulli_1) REPEATABLE (random()) .. versionadded:: 1.1 :param sampling: a ``float`` percentage between 0 and 100 or :class:`.functions.Function`. :param name: optional alias name :param seed: any real-valued SQL expression. When specified, the REPEATABLE sub-clause is also rendered. """ return _interpret_as_from(selectable).tablesample( sampling, name=name, seed=seed)