• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python func.random函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sqlalchemy.sql.expression.func.random函数的典型用法代码示例。如果您正苦于以下问题:Python random函数的具体用法?Python random怎么用?Python random使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了random函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: article

def article(article_slug):
    article = Article.query.filter(Article.slug == article_slug).first()
    if not article.published:
        abort(403)

    article.views += 1

    db.session.add(article)
    db.session.commit()

    from sqlalchemy.sql.expression import func
    related_articles = Article.query.search(article.keywords[0]).\
        filter(Article.id != article.id).order_by(func.random()).limit(3)

    _base_query = Article.query.public()

    # Tags
    tags = Tag.query.order_by(Tag.views.desc()).all()

    # recommended articles top 5
    recommended_articles = _base_query.filter_by(recommended=True).limit(9)
    popular_articles = _base_query.\
        order_by(Article.views.desc()).limit(9)

    random_articles = _base_query.order_by(func.random()).limit(9)

    return render_template('article.html',
                           article=article,
                           tags=tags,
                           related_articles=related_articles,
                           recommended_articles=recommended_articles,
                           popular_articles=popular_articles,
                           random_articles=random_articles)
开发者ID:charshine,项目名称:codingpy,代码行数:33,代码来源:site.py


示例2: profile_product_redirect

def profile_product_redirect(attr_type):
    '''fetch random product'''
    if attr_type == "hs":
        p = getattr(attr_models, attr_type.capitalize()).query.order_by(func.random()).first_or_404()
    else:
        p = getattr(attr_models, attr_type.capitalize()).query.order_by(func.random()).first_or_404()

    return redirect(url_for(".profile_product", attr_type=attr_type, attr_id=p.get_display_id()))
开发者ID:eltonfreitas,项目名称:oec,代码行数:8,代码来源:views.py


示例3: motive

def motive():
	prefix = Prefix.query.order_by( func.random() ).first()

	if 'GET' == request.method :
		m = Madlib.query.order_by( func.random() ).first()
	else:
		m = Madlib.query.get( request.form['madlib_id'] )

	sentence = m.text

	regex = re.compile( r"\{\{(.+?)\}\}" )

	if 'GET' == request.method :
		# fill in with random words from the dictionary
		sentence = regex.sub( fill_in_word, sentence )

	else:
		# for each match, replace with next word in submitted form
		tags = regex.findall( sentence )

		i=0
		for tag in tags:
			word = request.form["madlib-blank-" + str( i )].strip()
			i += 1

			# entering blanks means we pick a random word
			if '' == word:
				t = Tag.query.filter( Tag.text == tag ).first()
				if None is t or [] == t.words:
					word = tag
				word = choice( t.words ).text

			else:
				# add new words to the dictionary

				# force to lowercase unless it's a proper noun
				if tag not in ['person', 'place'] :
					word = word.lower()

				w = Word.query.filter( Word.text == word ).first()

				if None is w:
					w = Word( word, [ tag ] )
					db_session.add( w )

				else:
					# add these tags if they don't exist
					t = Tag.query.filter( Tag.text == tag ).first()
					w.tags.append( t )

			sentence = sentence.replace( "{{" + tag + "}}", word, 1 )

		db_session.commit()

		# TODO save generated sentence for posterity - maybe only on up-vote?

	return render_template( 'motive.html', prefix=prefix, motivation=sentence )
开发者ID:codebykat,项目名称:ulterior,代码行数:57,代码来源:views.py


示例4: r

 def r(self):
     if self.url.startswith('sqlite'):
         return func.random()
     if self.url.startswith('mysql'):
         return func.rand()
     if self.url.startswith('postgresql'):
         return func.random()
     if self.url.startswith('oracle'):
         return 'dbms_random.value'
     raise NotImplementedError()
开发者ID:atzm,项目名称:amazonas,代码行数:10,代码来源:sqldb.py


示例5: feed_discover

def feed_discover():
    off = request.args.get("start_index")
    if off:
        entries = db.session.query(db.Books).order_by(func.random()).offset(off).limit(config.NEWEST_BOOKS)
    else:
        entries = db.session.query(db.Books).order_by(func.random()).limit(config.NEWEST_BOOKS)
        off = 0
    xml = render_template('feed.xml', entries=entries, next_url="/feed/discover?start_index=%d" % (int(config.NEWEST_BOOKS) + int(off)))
    response = make_response(xml)
    response.headers["Content-Type"] = "application/xml"
    return response
开发者ID:lemmsh,项目名称:calibre-web,代码行数:11,代码来源:web.py


示例6: recommend_repos_for_user

def recommend_repos_for_user(username, limit=10):
    """
    1. store user stars to db if not there yet
    2. find 10 most similar users in db
    3. get 10 most starred repo of these users (not starred yet)

    :param username:
    :return: [repo object, ...]
    """

    user = User.query.filter_by(username=username).first()
    if not user:
        store_user_data(username)

    user_repo_map = dict()
    repos_enc = set()

    user_list = list(User.query.order_by(func.random()).limit(100).options(joinedload('repos')))
    for u in user_list + [user]:
        user_repo_map[u.id] = set()
        for repo in u.repos:
            user_repo_map[u.id].add(repo.id)
            repos_enc.add(repo.id)

    repos_enc = list(repos_enc)
    user_repo_enc = {
        u.id: [bool(repo_id in user_repo_map[u.id]) for repo_id in repos_enc] for u in user_list + [user]
    }

    user_scores = []
    for u in user_list:
        user_scores.append((u.id, jscore(user_repo_enc[u.id], user_repo_enc[user.id])))

    user_scores.sort(key=lambda item: item[1], reverse=True)

    repo_count_map = defaultdict()
    for uid, score in user_scores[1:30]:
        for repo in user_repo_map[uid]:
            if repo in user_repo_map[user.id]:
                continue

            if repo not in repo_count_map:
                repo_count_map[repo] = 0
            repo_count_map[repo] += 1

    repo_id_list = [repo_id for repo_id, _ in sorted(
        repo_count_map.items(), key=lambda item: item[1], reverse=True
    )][:limit]

    return Repo.query.filter(Repo.id.in_(repo_id_list)).order_by(func.random()).values('name', 'url')
开发者ID:zhebrak,项目名称:rehub,代码行数:50,代码来源:controllers.py


示例7: schedule_yuyue

def schedule_yuyue(s):
    user_list = s.query(User).filter(
        User.flag == True,
        User.yuyue_cnt < User.req_yuyue_cnt,  # 已经预约量小于预约总数
        User.yuyue_start_time < time.time(),  # 开始时间到了吗
        User.last_yuyue_time + User.yuyue_interval + random.randint(157, 678) < time.time(),  # 评价时间间隔,并且加上随机性
        User.yuyue_per_day_cnt < User.rdm_yuyue_per_day_cnt,  # 每天预约量小于客户每天规定的预约量
        # User.yuyue_per_day_cnt < User.req_yuyue_cnt / User.req_day_cnt  # 今天预约量是否够了,不够的才继续
    ).order_by(func.random()).all()
    len_user = len(user_list)
    if not len_user:
        print u'当前没有符合条件的预约yy'
        return True
    print u'共有预约人数:', len_user
    idx_cnt = 0
    for user in user_list:
        if len(user.urls) == 0:
            print user.id, u'没有urls'
            continue
        print
        #global_adsl.reconnect()  # adsl重连
        # time.sleep(3)
        idx_cnt += 1
        print datetime.now(), '约第', idx_cnt, '/', len_user, '个人:', user.shop_id, user.id, user.qq, user.wangwang, user.company, ',原来约:', user.original_yuyue_cnt, '已经约:', user.yuyue_cnt, ',今天约了:', user.yuyue_per_day_cnt, ',今天还要约:', user.rdm_yuyue_per_day_cnt - user.yuyue_per_day_cnt
        # random_url = random.choice([url for url in user.urls if url.flag == True])
        random_url = random.choice(user.urls)
        try:
            m_yuyue(s, user, random_url.href)
        except Exception, e:
            print e
            print traceback.format_exc()
            continue
        # time.sleep(random.randint(1, 5))
        pass
开发者ID:wfight,项目名称:Grab,代码行数:34,代码来源:liantong.py


示例8: build_msg

def build_msg(cursor, speaker, start):
    location = 'target' if speaker.startswith('#') else 'source'
    count = cursor.query(Babble_count).filter(Babble_count.type == location, Babble_count.key == speaker).first()
    if count is None:
        return "%s hasn't said anything =(" % speaker
    markov = cursor.query(Babble).filter(getattr(Babble, location) == speaker).offset(random.random() * count.count).first()
    if start is None:
        prev = markov.key
    else:
        # FIXME: use Babble_count?
        if len(start) == 1:
            markov = cursor.query(Babble).filter(Babble.key.like(start[0] + ' %'))
        elif len(start) == 2:
            markov = cursor.query(Babble).filter(Babble.key == " ".join(start))
        else:
            return "Please specify either one or two words for --start"
        markov = markov.filter(getattr(Babble, location) == speaker).order_by(func.random()).first()
        if markov:
            prev = markov.key
        else:
            return "%s hasn't said %s" % (speaker, " ".join(start))
    msg = prev
    while len(msg) < 256:
        data = cursor.query(Babble).filter(Babble.key == prev, getattr(Babble, location) == speaker).all()
        if not data:
            break
        next_word = weighted_next(data)
        msg = "%s %s" % (msg, next_word)
        prev = "%s %s" % (prev.split()[1], next_word)
    return "%s says: %s" % (speaker, msg)
开发者ID:jwoglom,项目名称:ionbot,代码行数:30,代码来源:babble.py


示例9: mark_available_phrases

def mark_available_phrases(
        user_id: int, language: str,
        limit: int) -> List[int]:
    phrases = db.session.query(
        Phrase.id
    ).filter(
        Phrase.user_id == user_id,
        Phrase.language == language,
        Phrase.status == Phrase.Status.visible.value,
        Phrase.date_available < datetime.now(),
        Phrase.progress_status < Phrase.ProgressStatus.after_two_week.value,
    ).order_by(
        func.random()
    ).limit(limit)
    phrase_ids = [phrase.id for phrase in phrases]

    if not phrase_ids:
        return []

    redis_store.lpush(
        PHRASE_REDIS_KEY_TEMPLATE.format(
            user_id=user_id,
            language=language,
        ),
        *phrase_ids
    )
    return phrase_ids
开发者ID:nlyubchich,项目名称:ask_linguist,代码行数:27,代码来源:bl.py


示例10: preview

    def preview(self, article_id):
        article = Article.query.get_or_404(article_id)
        related_articles = Article.query.search(article.keywords). \
            filter(Article.id != article.id).limit(3)

        _base_query = Article.query.public()

        # Tags
        tags = Tag.query.order_by(Tag.views.desc()).limit(10)

        # recommended articles top 5
        recommended_articles = _base_query.filter_by(recommended=True).limit(5)
        popular_articles = _base_query. \
            order_by(Article.views.desc()).limit(5)

        from sqlalchemy.sql.expression import func

        random_articles = _base_query.order_by(func.random()).limit(5)

        return self.render('article.html',
                           article=article,
                           tags=tags,
                           related_articles=related_articles,
                           recommended_articles=recommended_articles,
                           popular_articles=popular_articles,
                           random_articles=random_articles)
开发者ID:yongli82,项目名称:lingyan,代码行数:26,代码来源:admins.py


示例11: category

def category(name):
    random = db.session.query(db.Books).order_by(func.random()).limit(config.RANDOM_BOOKS)
    if name != "all":
        entries = db.session.query(db.Books).filter(db.Books.tags.any(db.Tags.name.like("%" +name + "%" ))).order_by(db.Books.last_modified.desc()).all()
    else:
        entries = db.session.query(db.Books).all()
    return render_template('index.html', random=random, entries=entries, title="Category: %s" % name)
开发者ID:mutschler,项目名称:calibreserver,代码行数:7,代码来源:web.py


示例12: get_questions

def get_questions(api_key='',method=1):
  ''' Accepts api_key and a paramteter method. Method
      determines the criteria by which the questions
      are generated.Currently only one method is de-
      fined. "1 Random Question Selection"
  '''
  #-- Selects 1000 questions at random
  if method ==1:
    questions = Question.query.order_by(func.random()).limit(100).all()
    #questions = questions[1:100]
    return (questions,1)
  #-- Action corresponding to method 2
  elif method == 2:
  #  pass
    questions = []    
    for c in Category.query.all():
      question_list = c.questions
      random.shuffle(question_list)
      if len(question_list) > 20 :
        question_list = question_list[0:20]
      for q in question_list:
        if q.question not in questions:
          questions.append(q.question)
    return (questions,2)
  #-- Action corresponding to method 3 etc,

  elif method == 3:
    pass

  else:
    pass
开发者ID:vijeenroshpw,项目名称:dizeezFB,代码行数:31,代码来源:app.py


示例13: classifier

def classifier():
    user = current_user
    exp = request.args.get('experiment', 'creativity')
    eclass = experiment_classes[exp]
    pattern = experiment_classes[exp]['pattern']
    classes = experiment_classes[exp]['labels']
    if request.method == 'POST':
        if 'class' in request.form:
            labels = request.form.getlist('class')
            value = 1
            img_id = int(request.form['img_id'])
            for label in labels:
                db.session.add(Classification(img_id=img_id, user_id=user.id, label=label, value=value))
            db.session.commit()

    q_existing = db.session.query(Classification, Image).filter(Classification.user_id==user.id).filter(Image.id==Classification.img_id)
    q_existing = q_existing.subquery()
    q_existing = aliased(Image, q_existing)
    q_existing = Image.query.join(q_existing)
    q_all = Image.query.filter(Image.url.like(pattern))
    q = q_all.except_(q_existing)
    q = q.order_by(func.random())
    nb = q.count()
    if nb == 0:
        return render_template('done.html')
    q = q.limit(1)
    img = q.one()
    img.url = parse(img.url)
    return render_template('classify_one.html', img=img, classes=classes, w=eclass.get('w', 250), h=eclass.get('h', 250), page_width=eclass.get('page_width'), nb=nb)
开发者ID:mehdidc,项目名称:annot,代码行数:29,代码来源:app.py


示例14: getRandomFeatureId

 def getRandomFeatureId(self, bodId):
     models = models_from_bodid(bodId)
     with self.getSession() as session:
         t = session.query(models[0]).limit(500).subquery('t')
         query = session.query(t).order_by(func.random()).limit(1)
         reslt = query.one()
     return reslt[0]
开发者ID:ioda-net,项目名称:mf-chsdi3,代码行数:7,代码来源:__init__.py


示例15: get_issues

def get_issues(id=None):
    '''Regular response option for issues.
    '''

    filters = request.args
    filters, querystring = get_query_params(request.args)

    if id:
        # Get one issue
        filter = Issue.id == id
        issue = db.session.query(Issue).filter(filter).first()
        if issue:
            return jsonify(issue.asdict(True))
        else:
            # If no issue found
            return jsonify({"status": "Resource Not Found"}), 404

    # Get a bunch of issues
    query = db.session.query(Issue).order_by(func.random())

    for attr, value in filters.iteritems():
        if 'project' in attr:
            proj_attr = attr.split('_')[1]
            query = query.join(Issue.project).filter(getattr(Project, proj_attr).ilike('%%%s%%' % value))
        elif 'organization' in attr:
            org_attr = attr.split('_')[1]
            query = query.join(Issue.project).join(Project.organization).filter(getattr(Organization, org_attr).ilike('%%%s%%' % value))
        else:
            query = query.filter(getattr(Issue, attr).ilike('%%%s%%' % value))

    response = paged_results(query, int(request.args.get('page', 1)), int(request.args.get('per_page', 10)), querystring)
    return jsonify(response)
开发者ID:PatrickLaban,项目名称:cfapi,代码行数:32,代码来源:app.py


示例16: index

def index(page=1):
    _base_query = Article.query.public()
    # Latest 10 articles
    template_name = 'index.html' if page == 1 else 'includes/items.html'
    all_articles = _base_query.order_by(Article.created_at.desc()).\
        paginate(page, Article.PER_PAGE, False).items

    # Tags
    tags = Tag.query.order_by(Tag.views.desc()).all()

    slides = _base_query.filter_by(slider=True).order_by(
        Article.created_at.desc()).limit(5)

    # recommended articles top 5
    recommended_articles = _base_query.filter_by(recommended=True).limit(9)
    popular_articles = _base_query.\
        order_by(Article.views.desc()).limit(9)

    from sqlalchemy.sql.expression import func
    random_articles = _base_query.order_by(func.random()).limit(9)

    return render_template(template_name,
                           all_articles=all_articles,
                           recommended_articles=recommended_articles,
                           popular_articles=popular_articles,
                           random_articles=random_articles,
                           slides=slides,
                           tags=tags,
                           page=page)
开发者ID:charshine,项目名称:codingpy,代码行数:29,代码来源:site.py


示例17: madlib

def madlib():
	m = Madlib.query.order_by( func.random() ).first()

	regex = re.compile( r"\{\{(.+?)\}\}" )
	blanks = regex.findall( m.text )

	return render_template( 'madlib.html', madlib=m, blanks=blanks )
开发者ID:codebykat,项目名称:ulterior,代码行数:7,代码来源:views.py


示例18: category

def category(slug, page=1):
    category = Category.query.filter_by(slug=slug).first()
    _base_query = Article.query.public().filter_by(category=category)

    all_articles = _base_query.order_by(Article.created_at.desc()).\
        paginate(page, Article.PER_PAGE, False).items

    # Tags
    tags = Tag.query.order_by(Tag.views.desc()).all()

    # recommended articles top 5
    recommended_articles = _base_query.filter_by(recommended=True).limit(5)
    popular_articles = _base_query.\
        order_by(Article.views.desc()).limit(5)

    from sqlalchemy.sql.expression import func
    random_articles = _base_query.order_by(func.random()).limit(5)

    return render_template('index.html',
                           page=page,
                           tags=tags,
                           category=category,
                           all_articles=all_articles,
                           recommended_articles=recommended_articles,
                           popular_articles=popular_articles,
                           random_articles=random_articles)
开发者ID:charshine,项目名称:codingpy,代码行数:26,代码来源:site.py


示例19: visualize_redirect

def visualize_redirect(app_name='tree_map'):
    '''fetch random country'''
    c = Country.query.get(choice(random_countries))
    latest_hs_year = [available_years['hs92'][-1]]

    if app_name in ["tree_map", "stacked", "network"]:
        year = [available_years['hs92'][0], available_years['hs92'][-1]] if app_name == "stacked" else latest_hs_year
        redirect_url = url_for('.visualize', lang=g.locale, app_name=app_name, \
                        classification="hs92", trade_flow="export", \
                        origin_id=c.id_3char, dest_id="all", prod_id="show", year=year)
    elif app_name in ["geo_map", "rings"]:
        '''fetch random product'''
        p = Hs92.query.filter(Hs92.hs92 != None).filter(func.length(Hs92.hs92) == 4) \
                            .order_by(func.random()).limit(1).first()
        origin = "show"
        if app_name == "rings":
            origin = c.id_3char
        redirect_url = url_for('.visualize', lang=g.locale, app_name=app_name, \
                        classification="hs92", trade_flow="export", \
                        origin_id=origin, dest_id="all", prod_id=p.hs92, year=latest_hs_year)
    elif app_name in ["scatter"]:
        redirect_url = url_for('.visualize', lang=g.locale, app_name=app_name, \
                        classification="hs92", trade_flow="gdp", \
                        origin_id="show", dest_id="all", prod_id="all", year=latest_hs_year)
    else:
        abort(404)
    return redirect(redirect_url)
开发者ID:hartmado,项目名称:oec,代码行数:27,代码来源:views.py


示例20: return_radom_question_set

def return_radom_question_set(n):
    """ It goes through the question bank
    and selects n random Questions along with it's answer list
    it also randomizes the answer choice sequence
    """
    #select.order_by(func.random())
    questions= Questions.query.order_by(func.random()).first()
    #print (questions.question)
    #print (questions)
    questions_answers = Questions.query.order_by(func.random()).join(Answers).limit(3)
#    questions_answers = Questions.query.rder_by(func.random()).join(Answers).limit(10)

    for k in questions_answers :
        print (k.question)
    #print (questions_answers.column_descriptions)
    q_a= Questions.query.join(Answers)
开发者ID:sibamohanty,项目名称:exam_violin,代码行数:16,代码来源:serve_questions.py



注:本文中的sqlalchemy.sql.expression.func.random函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python func.sum函数代码示例发布时间:2022-05-27
下一篇:
Python func.max函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap