def make_sentence(username, prompt=""): sentence = '' # Try to find the user user = session.query(model.User).filter(model.User.name==username).first() if not user: raise exceptions.UserNotFoundException( 'Username "{}" not found'.format(username)) sentence = '' word = '' if prompt: # Load up an initial word word = session.query(model.WordEntry)\ .filter(model.WordEntry.user == user.id, model.WordEntry.word_prev == prompt)\ .order_by(func.rand()).first() if word: sentence += word.word_prev + " " else: word = session.query(model.WordEntry)\ .filter(model.WordEntry.user == user.id, model.WordEntry.word_prev == '')\ .order_by(func.rand()).first() if not word: raise exceptions.UserHasntSpoken( 'I haven\'t seen "{}" say anything'.format(username)) word = word.word_next sentence += word for i in xrange(SENTENCE_WORD_LIMIT): last_words = ' '.join(sentence.split()[-2:]) word = get_next_word(user, word, last_words) if word: sentence += ' ' + word else: break sentence = "*{}:* ".format(str(user)) + str(sentence.encode('utf8')) # Slack surrounds URLs with < and >, which breaks their linking. So we # strip that out. sentence = re.sub( '<(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)>', r'\1', sentence) # Replace @u1010101 references with actual user names user_ids = re.finditer(r'(@[\d\w]{9})', sentence) for match in user_ids: user_id = match.group() user = session.query(model.User).filter( model.User.id==user_id.strip('@')).first() if not user: continue sentence = sentence.replace(user_id, '@' + str(user)) return sentence
def fetch_mammothRun_record(session, supergroup_key, version, host, redo_error): """ First attempts to lock the hpf.mammothRun table in order to ensure this process gets a mammoth record that no other mammothrun_driver process is working on simultaneously. When a lock is acheived (it's a blocking call), queries the mammothRun table for records of the supergroup_key and version specified with a status of 'unprocessed' (if redo_error is True, then also with status 'error'). If no record, returns None. Otherwise, sets the record's status to 'running', sets the host field, unlocks the table, and returns the record. """ from sqlalchemy import or_ from hpf.hddb.db import MammothRun from sqlalchemy.sql.expression import func session.execute("LOCK TABLES {0} WRITE".format(MammothRun.__tablename__)) if redo_error: ready_record = session.query(MammothRun).filter(MammothRun.supergroup_key==supergroup_key)\ .filter(MammothRun.version==version)\ .filter(or_(MammothRun.status=='unprocessed', MammothRun.status=='error'))\ .order_by(func.rand())\ .first() else: ready_record = session.query(MammothRun).filter(MammothRun.supergroup_key==supergroup_key).filter(MammothRun.version==version)\ .filter(MammothRun.status=='unprocessed')\ .first() if ready_record: ready_record.status = 'running' ready_record.host = host session.flush() session.execute("UNLOCK TABLES") return ready_record
def cricket_board(id): markStyles = get_mark_styles().order_by(func.rand()) markStyle = markStyles.first() markStyleIds = [] for ms in markStyles: markStyleIds.append(int(ms.id)) return render_template("matches/modes/cricket/board.html", match = getGameData(id), markStyle = markStyle, markStyleIds = markStyleIds)
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()
def random_row(model): """Retrieves a random row for the given model.""" try: # PostgreSQL, SQLite instance = model.query.order_by(func.random()).limit(1).first() except OperationalError: # MySQL instance = model.query.order_by(func.rand()).limit(1).first() return instance
def reply(): print request.remote_addr while True: quote = Quote.query.order_by(func.rand()).first() if len(quote.content) < 150: break q = {} q['content'] = quote.content q['author'] = quote.author return json.dumps(q)
def get_response(self): # this should query the base using the biggest word as a search term q = self.session.query(self.plugin.SCHEMA) if self.date: fact = q.filter(self.plugin.SCHEMA.date == self.date).order_by(func.rand()).first() else: # testing with the whole 'sentence' fact = q.filter(or_ (and_(*[self.plugin.SCHEMA.text.like('%%%s%%' % e) for e in self.context]), and_(*[self.plugin.SCHEMA.author.like('%%%s%%' % e) for e in self.context])) ).order_by(func.rand()).first() if not fact: # we only keep 'big' words (>1) and see how it goes c = [w for w in self.context if len(w) > 1] fact = q.filter(or_(*[self.plugin.SCHEMA.text.like('%%%s%%' % e) for e in c]), or_(*[self.plugin.SCHEMA.author.like('%%%s%%' % e) for e in c]) ).order_by(func.rand()).first() if fact: return repr(fact) else: return u"no match"
def load_photos(self, **filters) -> List[Photo]: q = self.session.query(Photo) owner_id = filters.get('owner_id') if owner_id is not None: q = q.filter( Photo.owner_id == owner_id ) albums = filters.get('albums') if albums is not None: q = q.filter( Photo.album.in_(albums) ) restricted_albums = filters.get('restricted_albums') if restricted_albums is not None: q = q.filter( Photo.album.notin_(restricted_albums) ) start_datetime = filters.get('start_datetime') if start_datetime is not None: q = q.filter( Photo.date_time >= start_datetime ) end_datetime = filters.get('end_datetime') if end_datetime is not None: q = q.filter( Photo.date_time <= end_datetime ) posted = filters.get('posted') if posted is not None: q = q.filter( Photo.posted.is_(posted) ) random = filters.get('random') if random is not None: q = q.order_by(func.rand()) limit = filters.get('limit') if limit is not None: q = q.limit(limit) offset = filters.get('offset') if offset is not None: q = q.offset(offset) photos = q.all() return photos
def add_projects(): rand_client = db.session.query(Customer).order_by(func.rand()).first projects = [ { "customer_id": 1, "name": "Build a time tracker application" }, { "customer_id": 2, "name": "New landing page" }, { "customer_id": 3, "name": "Migrate database architectures" } ] add_objects(projects, Project)
def get_random_user(method): """ Pick random user from db acording to some rules Pick first that is true 1. If main user never scraped, then scrape it 2. If main user to long since scraped, then scrape it 3. If user with degree priority high enough to be fully scraped never was scraped, then scrape it 4. If user with degree priority high enough to be fully scraped is to long since updated, then scrape it 5. if user never scraped, then scrape it 6. If user to old since update, then scrape it todo: remove or keep rule 5 and 6? args: method (str): friendship|user|tweet. Used to pick which last updated field to check """ users=None if method == "friendship": users = _get_random_user("friendships_last_updated", user_main_delta=twitter_config.FRIENDSHIP_USER_MAIN_DELTA, user_full_delta=twitter_config.FRIENDSHIP_USER_FULL_DELTA) elif method == "user": users = _get_random_user("last_updated", user_main_delta=twitter_config.USER_MAIN_DELTA, user_full_delta=twitter_config.USER_FULL_DELTA, user_delta=twitter_config.USER_DELTA) elif method == "tweet": users = _get_random_user("tweets_last_updated", user_main_delta=twitter_config.TWEET_USER_MAIN_DELTA, user_full_delta=twitter_config.TWEET_USER_FULL_DELTA) if not users: return None if not users.first(): return None #This should shuffle the list retrieved for different type of db. Not tested on all #Might also become to slow when db gets large, not tested. if twitter_config.DATABASE == twitter_const.MYSQL: users = users.order_by(func.rand()) elif twitter_config.DATABASE == twitter_const.POSTGRESSQL or twitter_config.DATABASE == twitter_const.SQLITE: users = users.order_by(func.random()) elif twitter_config.DATABASE == twitter_const.ORACLE: users = users.order_by('dbms_random.value') return users.first()
def __init__(self, session, widget_query, num_splits=10, table_name="tmp_widget_random"): from sqlalchemy import Column from sqlalchemy.sql.expression import func, select t_w = schema.widget class tmp_widget_random(schema.TableBase): idfold = Column(t_w.idwidget.type, nullable=False, primary_key=True) idwidget = Column(t_w.idwidget.type, nullable=False, primary_key=True) __table_args__ = ({ 'prefixes': ["TEMPORARY"], 'keep_existing': True }, ) __tablename__ = table_name self.num_splits = int(num_splits) self.session = session self.tmp_wr = tmp_widget_random self.tmp_wr.__table__.create(bind=self.session.bind) if self.session.bind.dialect.name.lower() == "mysql": randfunc = func.rand() else: randfunc = func.random() num_rows = widget_query.count() row_number_column = func.row_number().over( order_by=randfunc).label('row_number') self.session.execute( self.tmp_wr.__table__.insert().from_select( [self.tmp_wr.idfold, self.tmp_wr.idwidget], select([((row_number_column-1) * int(self.num_splits)) / int(num_rows), t_w.idwidget]) \ .where(t_w.idwidget.in_(widget_query)) \ ))
def info(): req = request.values id = int(req['id']) if ('id' in req and req['id']) else 0 if id < 1: return redirect(urlManager.buildURL('/')) info = Movie.query.filter_by(id=id).first() if not info: return redirect(urlManager.buildURL('/')) info.view_counter += 1 db.session.add(info) db.session.commit() recommend_list = Movie.query.order_by(func.rand()).limit(4) return ops_render("info.html", { 'info': info, 'recommend_list': recommend_list })
def inbox_read(mid): user = get_user() if not user: return error(401, "AuthorizationError", "인증 오류") if mid == "random": mail = user.mails.order_by(func.rand()).first() else: mail = Mail.query.filter_by(mail_id=mid).one() if not mail: return error(401, "MailError", "접근 오류") f = open('app/static/web/mail/%s.html' % mail.mail_id, "r") body = f.read() f.close() ppos = user.get_config("ppos") ppos = ppos.value if ppos else "0" return resolve_name(body, user.get_nickname(mail.member.id), ppos == "1")
def test(): page = request.args.get('page') if page and page.isdigit(): page = int(page) else: page = 1 selected = db.session.query(Quizz).order_by(func.rand()).limit(10) selected = selected.from_self() pages = selected.paginate(page=page, per_page=1, error_out=False) if request.method == "POST": curr_answer = request.form['answer_form'] correct_answer = Quizz.right_answer score = 0 print(score) if not pages.has_next: return redirect(url_for('/result', score=score)) return render_template('test.html', pages=pages)
def home (request): mysql = DBSession () images = None sphinx = SphinxClient () sphinx.SetServer ('127.0.0.1', 9312) sphinx.SetMatchMode (sphinxapi.SPH_MATCH_ANY) if 'query' in request.GET and len (request.GET['query']) > 0: # do search results = sphinx.Query (request.GET['query']) matches = [] for match in results['matches']: matches.append (match['id']) if results['total'] > 0: images = mysql.query (Image.id.label ('id'), Image.filename.label ('filename'), func.count (Keyword.id).label ('match_count') ).join (Image.keywords).filter (Keyword.id.in_ (matches) ).group_by (Image).order_by ('match_count DESC').distinct () else: # get some random images images = mysql.query (Image).order_by (func.rand () ).limit (30).all () return {'images': images}
class MnMEntry(BASE): """A Mix'n'match entry.""" __tablename__ = ENTRY_TABLE id = Column( INTEGER(11, unsigned=True), unique=True, primary_key=True, autoincrement=True, ) catalog = Column(INTEGER(10, unsigned=True), nullable=False, index=True) ext_id = Column(String(255), nullable=False, index=True, default='') ext_url = Column(String(255), nullable=False, default='') ext_name = Column(String(128), nullable=False, index=True, default='') ext_desc = Column(String(255), nullable=False, default='') q = Column(Integer, index=True) user = Column(INTEGER(10, unsigned=True), index=True) timestamp = Column(String(16), index=True) random = Column(Float, index=True, default=func.rand()) type = Column(String(16), nullable=False, index=True, default='')
def info(): req = request.values id = int(req['id']) if ('id' in req and req['id']) else 0 if id < 1: return redirect(UrlManager.buildUrl("/")) info = Movie.query.filter_by(id=id).first() if not info: return redirect(UrlManager.buildUrl("/")) ''' 更新阅读数量 ''' info.view_counter += 1 db.session.add(info) db.session.commit() ''' 获取推荐 ''' recommend_list = Movie.query.order_by(func.rand()).limit(4) return ops_render("info.html", {"info": info, "recommend_list": recommend_list})
def load_photos(self, **filters) -> List[Photo]: q = self.session.query(Photo) owner_id = filters.get('owner_id') if owner_id is not None: q = q.filter(Photo.owner_id == owner_id) albums = filters.get('albums') if albums is not None: q = q.filter(Photo.album.in_(albums)) restricted_albums = filters.get('restricted_albums') if restricted_albums is not None: q = q.filter(Photo.album.notin_(restricted_albums)) start_datetime = filters.get('start_datetime') if start_datetime is not None: q = q.filter(Photo.date_time >= start_datetime) end_datetime = filters.get('end_datetime') if end_datetime is not None: q = q.filter(Photo.date_time <= end_datetime) posted = filters.get('posted') if posted is not None: q = q.filter(Photo.posted.is_(posted)) random = filters.get('random') if random is not None: q = q.order_by(func.rand()) limit = filters.get('limit') if limit is not None: q = q.limit(limit) offset = filters.get('offset') if offset is not None: q = q.offset(offset) photos = q.all() return photos
def blog(id): # 获取文章 article = Article.query.get_or_404(id) Article.query.filter_by(id=id).update({'views_num': Article.views_num + 1}) # 获取栏目 currentnav = Nav.query.get(article.nav_id) #上一篇 prev = Article.query.order_by( Article.id.desc()).filter(Article.id < id).first() #下一篇 next = Article.query.order_by( Article.id.asc()).filter(Article.id > id).first() # 相关文章 relations = Article.query.filter(Article.nav_id == article.nav_id).filter( Article.id != id).order_by(func.rand()).limit(6) return render_template('index/article/detail.html', article=article, currentnav=currentnav, prev=prev, next=next, relations=relations)
def main(num_accounts): maybe_enable_rollbar() with global_session_scope() as db_session: accounts = (db_session.query(Account).filter( Account.sync_should_run == true()).order_by( func.rand()).limit(num_accounts).all()) accounts = [acc.id for acc in accounts][:num_accounts] db_session.expunge_all() pool = Pool(size=100) results = pool.map(process_account, accounts) global_results = dict() for ret in results: for key in ret: if key not in global_results: global_results[key] = 0 global_results[key] += ret[key] print(global_results)
def get_sound_by_string(string: str, server_id: int, uploader_id: int) -> typing.Optional[Sound]: r = re.match(r'(?:(?i)ID:)?(\d+)', string) if r is not None: return session.query(Sound).get(int(r.groups()[0])) else: q = session.query(Sound).filter(Sound.name == string.lower()) \ .order_by( Sound.server_id != server_id, Sound.uploader_id != uploader_id, Sound.public == False, func.rand()) \ .first() if q is None or (q.server_id != server_id and q.uploader_id != uploader_id and not q.public): return None else: return q
def search(): by = request.args.get('by', 'invalid', type=str) search_value = request.args.get('value', '', type=str) if by == 'name': if search_value == '': search_result = "%{}%".format(g.search_form.search.data) search_value = g.search_form.search.data else: search_result = "%{}%".format(search_value) games = Games.query.filter(Games.name.like(search_result)) elif by == 'new': games = Games.query.order_by(Games.released.desc()) elif by == 'top': games = Games.query.order_by(Games.rating.desc()) elif by == 'all': games = Games.query.order_by(func.rand()) page = request.args.get('page', 1, type=int) games = games.paginate(page, app.config['POSTS_PER_PAGE'], False) user_games_objects = User_games.query.filter_by( user_id=current_user.id).all() user_games = [it.game_id for it in user_games_objects] next_url = url_for('search', by=by, value=search_value, page=games.next_num) \ if games.has_next else None prev_url = url_for('search', by=by, value=search_value, page=games.prev_num) \ if games.has_prev else None games = games.items return render_template('search.html', games=games, ugames=user_games, url_next=next_url, url_prev=prev_url, current_page=page)
def create_journal(): error = "" form = JournalForm() if request.method == 'POST': title = form.title.data entry = form.entry.data author_id = current_user.id author = current_user.first_name if len(title) == 0 or len(entry) == 0: error = "Please supply a title and entry." else: journal_submission = Journal(date_created=datetime.now().date(), time_created=datetime.now().time(), author_id=author_id, entry=entry, title=title) db.session.add(journal_submission) db.session.commit() journal_id = journal_submission.journal_id return redirect( url_for('specific_journal_page', journal_id=journal_id, user_id=author_id)) if current_user.is_authenticated: random_theme = db.session.query(JournalTheme.theme).order_by( func.rand()).first() return render_template('create_journal_entry.html', form=form, message=error, is_logged_in=True, randomtheme=random_theme[0], is_edit=False, delete_form="") else: raise PageRequiresLoginError( "User tried to access journal creation page without logging in.")
def get_results(channel_slug): channel = Channel.query.filter_by(slug=channel_slug).first() if not channel: return "404 - Not found" results = Record.query.filter_by(executed=False, channel_id=channel.id).all() if not results: random_rec = Record.query.filter_by(channel_id=channel.id).order_by( func.rand()).first() if random_rec: entry = Record(channel.id, random_rec.video.id) db.session.add(entry) db.session.commit() results = [entry] return jsonify({ "videos": map( lambda x: { 'code': x.video.code, 'r_id': x.id, 'title': x.video.title, 'duration': x.video.duration }, results) })
def process_account(account_id): ret = defaultdict(int) try: with session_scope(account_id) as db_session: acc = db_session.query(Account).get(account_id) db_session.expunge(acc) one_month_ago = datetime.datetime.utcnow() - datetime.timedelta( days=30) for _ in range(NUM_MESSAGES): with session_scope(account_id) as db_session: block = (db_session.query(Block).filter( Block.namespace_id == acc.namespace.id, Block.created_at < one_month_ago, ).order_by(func.rand()).limit(1).first()) if block is None: continue if len(block.parts) == 0: ret["null_failures"] += 1 continue message = block.parts[0].message raw_mime = get_raw_from_provider(message) if raw_mime != "": ret["successes"] += 1 else: ret["null_failures"] += 1 except Exception as e: ret[type(e).__name__] += 1 return ret
def random_songs(limit=20): session = Session() res = session.query(Song).order_by(func.rand()).limit(limit).all() session.commit() songs = [song.dictify() for song in res] return {'query': '', 'limit': limit, 'results': songs}
def picture_random(language): word = Word.query.order_by(func.rand()).first() return redirect("/%s/word/%s" % (util.get_language(language), word.uid), code=303)
def male(nyt_id=None): if current_user.is_authenticated and current_user.is_admin: row = Metadata().query.filter(and_(Metadata.review_type == "needs_audit_probably_male", Metadata.year > 1905, Metadata.year < 1923)).order_by(func.rand()).first() endpoint = row.nyt_pdf_endpoint if ".xml" not in endpoint: r = requests.get(endpoint) html = BeautifulSoup(r.text, features="html.parser") link = html.find("a", {"class":"user-action archive-user-action"}) pdf_link = link['href'].replace('.html', '.pdf') else: pdf_link = endpoint return render_template("index.html", nyt_id=row.nyt_id, row=row, endpoint=endpoint, pdf_link=pdf_link) else: return render_template("index.html", nyt_id=None, endpoint=None, pdf_link=None)
def index(nyt_id=None): if current_user.is_authenticated and current_user.is_admin: if nyt_id != None: row = Metadata().query.filter(Metadata.nyt_id == nyt_id).one_or_none() endpoint = row.nyt_pdf_endpoint r = requests.get(endpoint) html = BeautifulSoup(r.text, features="html.parser") link = html.find("a", {"class":"user-action archive-user-action"}) pdf_link = link['href'].replace('.html', '.pdf') return render_template("index.html", nyt_id=nyt_id, row=row, endpoint=endpoint, pdf_link=pdf_link) else: row = Metadata().query.filter(and_(Metadata.year > 1905, Metadata.year < 1926)).filter(Metadata.review_type.like("needs_audit%")).order_by(func.rand()).first() #pages = ['BR3', 'BR4', 'BR5', 'BR6', 'BR7', 'BR8', 'BR9', 'BR10'] #row = [i for i in row if i.page in pages] #row = row[0] # BR1, 'BR2', 'BR3', 'BR4', 'BR5', 'BR6', 'BR7', 'BR8', 'BR9', 'BR10' endpoint = row.nyt_pdf_endpoint if ".xml" not in endpoint: r = requests.get(endpoint) html = BeautifulSoup(r.text, features="html.parser") link = html.find("a", {"class":"user-action archive-user-action"}) pdf_link = link['href'].replace('.html', '.pdf') else: pdf_link = endpoint return render_template("index.html", nyt_id=row.nyt_id, row=row, endpoint=endpoint, pdf_link=pdf_link) else: return render_template("index.html", nyt_id=None, endpoint=None)
def get_random_account(self, session): q = session.query(Account).filter(Account.is_login == true()) return q.order_by(func.rand()).first()
def get_randomCrawlIdentifiers(self, limit=100): from sqlalchemy.sql.expression import func it = self._session.query(CrawlFile).filter_by(status = "Incomplete").order_by(func.rand()).limit(limit) return [i.id for i in it if "crawl-002" not in i.key]
def getIndex(self, form): ''' 觅声首页: { action: 'get_index', listentype: 'diff'/'like', channel: 'unset'/'channel_name', } { feed: feed{}, feed_next: feed{} } // 上一首由前端记录 ''' audioChannel = form["channel"] if audioChannel == "unset": audioChannel = "" # 目前有8个分类,用0-7代表,因此暂定channel为8也返回全部频道。 if audioChannel == "8": audioChannel = "" logger.info("getIndex. audioChannel = %s" % audioChannel) # 不同的数据库类型有不同的随机查询方式 if self.dbName == "sqlite": randfunc = func.random() else: # for mysql randfunc = func.rand() # 随机查询两个audio randTwoAudios = self.session.query(User, Audio).filter(and_( User.openid == "system", # index 页只显示PGC Audio.deleted == False, R_User_Create_Audio.deleted == False, User.openid == R_User_Create_Audio.user_openid, R_User_Create_Audio.audio_id == Audio.audio_id )) if audioChannel: randTwoAudios = randTwoAudios.filter(and_( R_Audio_In_AudioChannel.deleted == False, R_Audio_In_AudioChannel.audio_id == Audio.audio_id, R_Audio_In_AudioChannel.channel_id == int(audioChannel) + 1 )) randTwoAudios = randTwoAudios.order_by(randfunc).limit(2).all() # 查询对应的user,tag,以及其他信息,组装成feed openid = form["openid"] feeds = [self.packFeed(openid, user, audio) for user, audio in randTwoAudios] feedlen = len(feeds) return Status.success({ "feed": feeds[0] if feedlen > 0 else "", "feed_next": feeds[1] if feedlen > 1 else "", })
def testWebhook(message): if current_user.is_authenticated: webhookID = int(message['webhookID']) webhookType = message['webhookType'] channelID = None if 'channelID' in message: channelID = int(message['channelID']) sysSettings = settings.settings.query.first() webhookQuery = None # Acquire a Channel to Test With channelQuery = None if channelID is not None: channelQuery = Channel.Channel.query.filter_by( id=channelID).first() else: channelQuery = Channel.Channel.query.order_by(func.rand()).first() # Acquire a Topic to Test With topic = topics.topics.query.order_by(func.rand()).first() # Retrieve Current Picture pictureLocation = current_user.pictureLocation if current_user.pictureLocation is None: pictureLocation = '/static/img/user2.png' else: pictureLocation = '/images/' + pictureLocation if channelQuery is not None: # Prepare Channel Image if channelQuery.imageLocation is None: channelImage = (sysSettings.siteProtocol + sysSettings.siteAddress + "/static/img/video-placeholder.jpg") else: channelImage = (sysSettings.siteProtocol + sysSettings.siteAddress + "/images/" + channelQuery.imageLocation) if webhookType == "global": if current_user.has_role("Admin"): webhookQuery = webhook.globalWebhook.query.filter_by( id=webhookID).first() elif webhookType == "channel": webhookQuery = webhook.webhook.query.filter_by( id=webhookID).first() if webhookQuery is not None: if webhookQuery.channel.id != current_user.id: webhookQuery = None randomVideoQuery = RecordedVideo.RecordedVideo.query.order_by( func.random()).first() webhookFunc.testWebhook( webhookType, webhookQuery.id, channelname=channelQuery.channelName, channelurl=(sysSettings.siteProtocol + sysSettings.siteAddress + "/channel/" + str(channelQuery.id)), channeltopic=templateFilters.get_topicName(channelQuery.topic), channelimage=channelImage, streamer=templateFilters.get_userName(channelQuery.owningUser), channeldescription=str(channelQuery.description), streamname="Testing Stream", streamurl=(sysSettings.siteProtocol + sysSettings.siteAddress + "/view/" + channelQuery.channelLoc), streamtopic=templateFilters.get_topicName(topic.id), streamimage=(sysSettings.siteProtocol + sysSettings.siteAddress + "/static/img/video-placeholder.jpg"), user=current_user.username, userpicture=(sysSettings.siteProtocol + sysSettings.siteAddress + str(pictureLocation)), videoname=randomVideoQuery.channelName, videodate=str(randomVideoQuery.videoDate), videodescription=randomVideoQuery.description, videotopic=templateFilters.get_topicName( randomVideoQuery.topic), videourl=(sysSettings.siteProtocol + sysSettings.siteAddress + '/play/' + str(randomVideoQuery.id)), videothumbnail=(sysSettings.siteProtocol + sysSettings.siteAddress + '/videos/' + str(randomVideoQuery.thumbnailLocation)), comment="This is just a test comment!", message="This is just a test message!") db.session.commit() db.session.close() return 'OK'
def explore(): """发现页,随机展示问题,待修改 """ random_questions = Question.query.order_by(func.rand()).limit(5) return render_template('explore.html', questions=random_questions)
def train_model(self, model_name): session = Session() model_record = session.query(ModelEntry).filter(ModelEntry.name == model_name).one() model_record.state = 'training' session.commit() parent_models = self.model_manager.models[model_name].input_models for parent_model in parent_models: # Train any untrained parent models if session.query(ModelEntry).filter(ModelEntry.name == parent_model, ModelEntry.state == 'untrained')\ is not None: self.train_model(parent_model) # Wait for any parent models that are still in training, in cases where another process started the training while session.query(ModelEntry).filter(ModelEntry.name == parent_model, ModelEntry.state == 'training').one_or_none()\ is not None: time.sleep(30) model = self.model_manager.models[model_name] dataset_name = model.dataset entry_count = model.training_entry_count if dataset_name == 'core_dataset': dataframe = pandas.read_sql(session.query(Base.metadata.tables['core_dataset']).order_by(func.rand()).limit(entry_count).statement, self.database_engine) else: dataframe = pandas.read_csv(DEFAULT_DATA_PATH, sep='\s+', names=DEFAULT_DATA_HEADERS) if entry_count > len(dataframe): entry_count = len(dataframe) dataframe = dataframe.sample(entry_count) for parent_model in parent_models: self.recursive_process(parent_model, dataframe) model.train(dataframe) self.model_manager.save_model(model_name) model_record.state = 'trained' session.commit() # Update the model manager max input count - done here to happen at the end of training self.model_manager.update_max_inputs(len(self.model_manager.get_model_inputs(model_name))) session.close()
def download_questions(): questions = Questions.query.order_by(func.rand()).limit(5) question_schema = QuestionSchema(many=True) output = question_schema.dump(questions) return jsonify(output)
def index(): example_host = Host.query.filter_by(active=1).filter_by(ssl_result=6).order_by(sqlalchemy_func.rand()).first() return render_template('index.html', example_host=example_host)
def random_question(size=5): random_questions = Question.query.order_by(func.rand()).limit(5) question = [dict(link=url_for('main.question_page', id=x.id), title=x.title) for x in random_questions] return jsonify(question=question)
def goodrandomquote(): quotes = Quotes.query.filter_by(pending = False).filter(Quotes.upvotes > Quotes.downvotes).order_by(func.rand()).limit(POSTS_PER_PAGE).from_self() return render_quotes(quotes, filter_pending = False, bayesian_sort = False)
def run(self): if self.experiment is None: return experiment = self.experiment db = self.db while True: job = None try: job = db.session.query(db.ExperimentResult) \ .filter_by(experiment=self.experiment) \ .filter_by(status=-1) \ .order_by(func.rand()).limit(1).first() job.status = 0 db.session.commit() except: db.session.rollback() if job: job.startTime = func.now() db.session.commit() client_line = '/usr/bin/time -f ";time=%U;mem=%M;" ' client_line += os.path.join(PACKAGE_DIR, 'solvers', launch_command(job.solver_configuration, os.path.join(PACKAGE_DIR, 'instances', str(job.instance.idInstance)+ '_' + job.instance.name), str(job.seed))[2:]) print "running job", job.idJob, client_line stdout = open(os.path.join(TMP, str(job.idJob) + 'stdout~'), 'w') stderr = open(os.path.join(TMP, str(job.idJob) + 'stderr~'), 'w') start = time.time() p = subprocess.Popen(shlex.split(str(client_line)), preexec_fn=setlimits(self.experiment.CPUTimeLimit), stdout = stdout, stderr = stderr) returncode = p.wait() print "Job", job.idJob, "done .. Realtime:", str(time.time() - start), "s" stdout.close() stderr.close() stdout = open(os.path.join(TMP, str(job.idJob) + 'stdout~'), 'r') stderr = open(os.path.join(TMP, str(job.idJob) + 'stderr~'), 'r') time_output = stderr.read().split(';') runtime = float([d.split('=')[1] for d in time_output if d.startswith('time=')][0]) memory = int([d.split('=')[1] for d in time_output if d.startswith('mem=')][0]) job.solverOutput = stdout.read() stdout.close() stderr.close() os.remove(os.path.join(TMP, str(job.idJob) + 'stdout~')) os.remove(os.path.join(TMP, str(job.idJob) + 'stderr~')) job.resultTime = runtime #cpuinfo = open('/proc/cpuinfo') #job.solverOutput = cpuinfo.read() #cpuinfo.close() print " retcode", returncode if returncode != 10 and returncode != 0: # CPU Time limit exceeded exit code guess job.status = 21 else: job.status = 1 if 's SATISFIABLE' in job.solverOutput: job.resultCode = 11 print " CPU time:", runtime, "s", "Memory used:", memory, "kB" job.computeQueue = self.experiment.grid_queue[0].idgridQueue db.session.commit() else: if db.session.query(db.ExperimentResult) \ .filter_by(experiment=self.experiment) \ .filter_by(status=-1) \ .order_by(func.rand()).count() == 0: break # no more jobs
def cricket_board(id): markStyle = model.Model().select(markStyleModel.MarkStyle).filter_by(approved = 1).order_by(func.rand()).first() return render_template("matches/modes/cricket/board.html", match = getGameData(id), markStyle = markStyle)
def get(self): user = self.get_current_user() randUrl = self.session.query(Article).order_by(func.rand()).limit(1).first() self.write(escape.json_encode({'short_url':randUrl.aurl}))
def dashboard(): if 'username' not in session: return redirect(url_for('signin')) user = User.query.filter_by(username = session['username']).first() if user.verified == 0: print 'user is not verified' return redirect(url_for('verify')) if user.portfolioname == None or len(user.portfolioname.replace(' ', '')) < 1: print 'user does not have a portfolioname' return redirect(url_for('portfolioSetup')) user = User.query.filter_by(username = session['username']).first() uploads = Upload.query.filter_by(publisher=session['username']) following = Follow.query.filter_by(follower_username=session['username']) following_count = [] for i in following: following_count.append(i) amount_of_following = len(following_count) user.following = amount_of_following db.session.commit() followers_count = [] for person in User.query.all(): followers = Follow.query.filter_by(followed_username=person.username) if followers != None: for i in followers: followers_count.append(i) amount_of_followers = len(followers_count) User.query.filter_by(username=person.username).first().followers = amount_of_followers db.session.commit() else: amount_of_followers = 0 User.query.filter_by(username=person.username).first().followers = amount_of_followers db.session.commit() random_people = [] for i in User.query.order_by(func.rand()).limit(2).all(): random_people.append(i) #work on filtering posts peopleFollowing = [] for i in Follow.query.filter_by(follower_username=session['username']): peopleFollowing.append(i.followed_username) posts_query = Posts.query.all() postsFollowing = [session['username']] for i in posts_query: if i.poster_username in peopleFollowing: postsFollowing.append(i.poster_username) posts = Posts.query.filter(Posts.poster_username.in_(postsFollowing)) upload_folder = '/Users/developeraccount/Desktop/Lens/lens/src/static/accounts/%s' % session['username'] if user is None: return redirect(url_for('signin')) else: firstname = user.firstname lastname = user.lastname username = user.username figure = user.figure location = user.location following = user.following followers = user.followers twitter = user.twitter appreciations = user.appreciations instagram = user.instagram github = user.github bio = user.bio location = user.location if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename.lower()): filename = secure_filename(file.filename) # newupload = Upload(filename, '/accounts/%s/%s' % (session['username'], filename), session['username'], request.form['title'], request.form['description']) newupload = Upload(filename, 'accounts/%s/%s' % (session['username'], filename), session['username'], "none", "none") db.session.add(newupload) db.session.commit() file.save(os.path.join(upload_folder, filename)) return redirect(url_for('dashboard')) return render_template('dashboard.html', User=User, user=user, peopleFollowing=peopleFollowing, posts=posts, random_people=random_people, bio=bio, uploads=uploads, location=location, github=github, instagram=instagram, username=username, firstname=firstname, lastname=lastname, figure=figure, following=following, followers=followers, twitter=twitter, appreciations=appreciations)
def get_random(self): return Lunch.query.order_by(func.rand()).first()
def dashboard(): if 'username' not in session: return redirect(url_for('signin')) user = User.query.filter_by(username=session['username']).first() if user.verified == 0: print 'user is not verified' return redirect(url_for('verify')) if user.portfolioname == None or len(user.portfolioname.replace(' ', '')) < 1: print 'user does not have a portfolioname' return redirect(url_for('portfolioSetup')) user = User.query.filter_by(username=session['username']).first() uploads = Upload.query.filter_by(publisher=session['username']) following = Follow.query.filter_by(follower_username=session['username']) following_count = [] for i in following: following_count.append(i) amount_of_following = len(following_count) user.following = amount_of_following db.session.commit() followers_count = [] for person in User.query.all(): followers = Follow.query.filter_by(followed_username=person.username) if followers != None: for i in followers: followers_count.append(i) amount_of_followers = len(followers_count) User.query.filter_by(username=person.username).first( ).followers = amount_of_followers db.session.commit() else: amount_of_followers = 0 User.query.filter_by(username=person.username).first( ).followers = amount_of_followers db.session.commit() random_people = [] for i in User.query.order_by(func.rand()).limit(2).all(): random_people.append(i) #work on filtering posts peopleFollowing = [] for i in Follow.query.filter_by(follower_username=session['username']): peopleFollowing.append(i.followed_username) posts_query = Posts.query.all() postsFollowing = [session['username']] for i in posts_query: if i.poster_username in peopleFollowing: postsFollowing.append(i.poster_username) posts = Posts.query.filter(Posts.poster_username.in_(postsFollowing)) upload_folder = '/Users/developeraccount/Desktop/Lens/lens/src/static/accounts/%s' % session[ 'username'] if user is None: return redirect(url_for('signin')) else: firstname = user.firstname lastname = user.lastname username = user.username figure = user.figure location = user.location following = user.following followers = user.followers twitter = user.twitter appreciations = user.appreciations instagram = user.instagram github = user.github bio = user.bio location = user.location if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename.lower()): filename = secure_filename(file.filename) # newupload = Upload(filename, '/accounts/%s/%s' % (session['username'], filename), session['username'], request.form['title'], request.form['description']) newupload = Upload( filename, 'accounts/%s/%s' % (session['username'], filename), session['username'], "none", "none") db.session.add(newupload) db.session.commit() file.save(os.path.join(upload_folder, filename)) return redirect(url_for('dashboard')) return render_template('dashboard.html', User=User, user=user, peopleFollowing=peopleFollowing, posts=posts, random_people=random_people, bio=bio, uploads=uploads, location=location, github=github, instagram=instagram, username=username, firstname=firstname, lastname=lastname, figure=figure, following=following, followers=followers, twitter=twitter, appreciations=appreciations)
def run(self): if self.experiment is None: return experiment = self.experiment db = self.db while True: job = None try: job = db.session.query(db.ExperimentResult) \ .filter_by(experiment=self.experiment) \ .filter_by(status=-1) \ .order_by(func.rand()).limit(1).first() job.status = 0 db.session.commit() except: db.session.rollback() if job: job.startTime = func.now() db.session.commit() client_line = '/usr/bin/time -f ";time=%U;mem=%M;" ' client_line += os.path.join( PACKAGE_DIR, 'solvers', launch_command( job.solver_configuration, os.path.join( PACKAGE_DIR, 'instances', str(job.instance.idInstance) + '_' + job.instance.name), str(job.seed))[2:]) print "running job", job.idJob, client_line stdout = open(os.path.join(TMP, str(job.idJob) + 'stdout~'), 'w') stderr = open(os.path.join(TMP, str(job.idJob) + 'stderr~'), 'w') start = time.time() p = subprocess.Popen(shlex.split(str(client_line)), preexec_fn=setlimits( self.experiment.CPUTimeLimit), stdout=stdout, stderr=stderr) returncode = p.wait() print "Job", job.idJob, "done .. Realtime:", str(time.time() - start), "s" stdout.close() stderr.close() stdout = open(os.path.join(TMP, str(job.idJob) + 'stdout~'), 'r') stderr = open(os.path.join(TMP, str(job.idJob) + 'stderr~'), 'r') time_output = stderr.read().split(';') runtime = float([ d.split('=')[1] for d in time_output if d.startswith('time=') ][0]) memory = int([ d.split('=')[1] for d in time_output if d.startswith('mem=') ][0]) job.solverOutput = stdout.read() stdout.close() stderr.close() os.remove(os.path.join(TMP, str(job.idJob) + 'stdout~')) os.remove(os.path.join(TMP, str(job.idJob) + 'stderr~')) job.resultTime = runtime #cpuinfo = open('/proc/cpuinfo') #job.solverOutput = cpuinfo.read() #cpuinfo.close() print " retcode", returncode if returncode != 10 and returncode != 0: # CPU Time limit exceeded exit code guess job.status = 21 else: job.status = 1 if 's SATISFIABLE' in job.solverOutput: job.resultCode = 11 print " CPU time:", runtime, "s", "Memory used:", memory, "kB" job.computeQueue = self.experiment.grid_queue[0].idgridQueue db.session.commit() else: if db.session.query(db.ExperimentResult) \ .filter_by(experiment=self.experiment) \ .filter_by(status=-1) \ .order_by(func.rand()).count() == 0: break # no more jobs
def respond(): """Respond to incoming texts""" resp = twilio.twiml.Response() incoming_msg = request.values.get('Body', '') # this clears all cookies if(incoming_msg.lower()=='clear'): session['seen_intro'] = False session['seen_prompt'] = False session['gave_ans'] = False session['extra_msg'] = False resp.sms("erasing my memory of our conversation") return str(resp) # # if exchange has been completed # if session.get('extra_msg',False): # session['extra_msg'] = False # if incoming_msg.lower().startswith('y'): # session['seen_prompt'] = False # session['gave_ans'] = False # elif incoming_msg.lower().startswith('n'): # # TODO: ask for feedback? # resp.sms('ok, later!') # return str(resp) if not session.get('seen_prompt',False): # if this is the original interaction with a bot if not session.get('seen_intro', False): resp.sms("hello friend - nice to meet you! i'm a self-care SMS bot (:\n\nreply with life advice, self-care tips, or a compliment to send to a stranger, and you'll get texted some nice things by someone in return! <3") session['seen_intro'] = True # if the user is submitting additional fortunes else: resp.sms("reply with life advice, self-care tips, or a compliment to send to a stranger, and you'll get texted some nice things by someone in return! <3") session['seen_prompt'] = True elif not session.get('gave_ans',False): # saving answer here if incoming_msg: # only if there is a msg new_ans = Answer(request.values.get('SmsSid'), request.values.get('From'), incoming_msg) # TODO: error handling here db.session.add(new_ans) db.session.commit() # grabbing a fortune from a stranger # TODO: ensure that this is not a fortune from yourself random_ans = Answer.query.filter_by(is_approved=True)\ .filter((Answer.from_number!=request.values.get('From'))|(Answer.from_number == None))\ .order_by(func.rand())\ .first() if random_ans: resp.sms("what a lovely message! here's what someone submitted for you:\n\n%s" %random_ans.answer_text) if incoming_msg: # only if there is a msg random_ans.view_count = random_ans.view_count+1 db.session.add(random_ans) db.session.commit() # alerting rec-giver when rec has been seen for the first time if random_ans.from_number and random_ans.view_count==1: client = TwilioRestClient( CONFIG_VARS['TWILIO_ACCOUNT_SID'], CONFIG_VARS['TWILIO_AUTH_TOKEN'] ) msg = "hey! just wanted to let you know that your message has been sent to a stranger! <3 " message = client.messages.create( to=random_ans.from_number, from_=CONFIG_VARS['TWILIO_PHONE_NO'], body=msg ) else: resp.sms("sorry, there doesn't seem to be any messages in the queue right now! but I hope you have a lovely day nonetheless <3") # session['gave_ans'] = True elif not session.get('extra_msg',False): session['extra_msg'] = True resp.sms("if you want another message, text me again tomorrow! have a lovely rest of the day <3 ") else: return '' return str(resp)
def explore(): photos = Photo.query.order_by(func.rand()).limit(12) return render_template("main/explore.html", photos=photos)
def get_random_question(self): q = self.session.query(Question).filter(Question.is_fetch == False and Question.answernum >= 5).order_by(func.rand()).limit(10) print(q.first()) return q.first()
def get_random(self): return Starter.query.order_by(func.rand()).first()
def getRandomBookingIDs(numberToGet): teleData = telemetry.query.order_by(func.rand()).limit(numberToGet).all() return (teleData)
def get(self): user = self.current_user users = db.query(User).order_by( func.rand()).filter(User.id != user.id).limit(9).all() self.render("user/referrers.html", users=users) return
def search_random(cls, **kwargs): return cls._search(**kwargs).order_by(func.rand())