本文整理汇总了Python中models.Score.query方法的典型用法代码示例。如果您正苦于以下问题:Python Score.query方法的具体用法?Python Score.query怎么用?Python Score.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Score
的用法示例。
在下文中一共展示了Score.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self,request):
"""Return all scores ordered by total points"""
if request.number_of_results:
scores = Score.query(Score.won == True).order(Score.attempts_allowed,Score.guesses).fetch(request.number_of_results)
else:
scores = Score.query(Score.won == True).order(Score.attempts_allowed,Score.guesses).fetch()
return ScoreForms(items=[score.to_form() for score in scores])
示例2: _cache_winning_chance
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def _cache_winning_chance(user_name):
"""Populates memcache with the average moves remaining of Games"""
## the scores user earned when it is user in games
user_chance = 0
oppo_chance = 0
user_scores = Score.query(Game.user.get().name == user_name).fetch()
if user_scores:
total = 2*len(user_scores)
wins = sum([score.user_score_to_int() for score in user_scores])
user_chance = wins/float(total)
## score the user earned when it is the opponent in games
oppo_scores = Score.query(Game.user.get().name == GLOBAL_CURRENT_USER_NAME).fetch()
if user_scores:
total = 2*len(oppo_scores)
wins = sum([score.user_score_to_int() for score in oppo_scores])
oppo_chance = wins/float(total)
if user_chance == 0 or oppo_chance == 0:
chance = user_chance | oppo_chance
else:
change = (user_chance+oppo_chance)/2
print "Winning chance of {} is {}".format(GLOBAL_CURRENT_USER_NAME, chance)
memcache.set(MEMCACHE_WINNING_CHANCE,
'The winning chance is {:.2f}'.format(chance))
示例3: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self, request):
"""Gets the high scores for the game"""
if request.number_of_results is not None:
scores = Score.query().order(Score.score).fetch(request.number_of_results)
else:
scores = Score.query().order(Score.score)
return ScoreForms(items=[score.to_form() for score in scores])
示例4: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self, request):
""" Get the user scores, Requires the user info's"""
user = self._get_user(request)
if request.number_of_results:
scores = Score.query(Score.user == user.key).order(Score.score).fetch(limit=request.number_of_results)
else:
scores = Score.query(Score.user == user.key).order(Score.score)
return ScoreForms(items=[score.to_form() for score in scores])
示例5: get_user_rankings
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_user_rankings(self, request):
"""Returns user rankings"""
users = User.query(User.name)
scores = Score.query([Score.user == user.key])
wins = Score.query([Score.won == True for user in users])
games = Game.query(User.name)
ranks = [(sum(wins)/sum(games) * 100) for user in users]
return ranks
示例6: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self, request):
"""Return all scores ordered by total points"""
if request.limit:
scores = Score.query().order(-Score.num_of_wons).fetch(request.limit)
else:
scores = Score.query().order(-Score.num_of_wons).fetch()
return ScoreForms(items=[score.to_form() for score in scores])
示例7: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self, request):
"""Return a specified number of high scores"""
# Checks the value of number_of_results to determine if it is a number
# to return a specific number of highscores if it is not then function
# will only return 10 highscores
if isinstance(request.number_of_results, int):
_number_of_results = request.number_of_results
scores = Score.query(Score.won==True).order(Score.total_attempts).fetch(limit=_number_of_results)
return ScoreForms(items=[score.to_form() for score in scores])
else:
scores = Score.query(Score.won==True).order(Score.total_attempts).fetch(limit=10)
return ScoreForms(items=[score.to_form() for score in scores])
示例8: get_user_rankings
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_user_rankings(self, request):
"""Get user rankings"""
users = User.query()
for user in users:
games = Score.query(Score.user == user.key)
win = Score.query().filter(Score.result == 'Win').fetch()
draw = Score.query().filter(Score.result == 'Draw').fetch()
lose = Score.query().filter(Score.result == "Lose").fetch()
# The final score is base on both performance and participation.
final_score = 5*len(win) + 3*len(win)+ 1*len(lose)
user.rankingscore = final_score
user.put()
return UserForms(items=[user.to_form() for user in User.query().order(-User.rankingscore)])
示例9: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self, request):
"""
Returns some or all of an individual User's scores
"""
# A game and its score are associated with a user.
# If that user # can't be identified by the given name,
# throw an # exception. Otherwise, get the scores.
user = User.query(User.name == request.user_name).get()
if not user:
raise endpoints.NotFoundException(
'A User with that name does not exist!')
# Scores, in this case, are the number of allegedly
# unflagged bombs in each game.
scores = Score.query(Score.user == user.key)
scores.order(Score.unflagged)
if request.number_of_results:
selectedScores = scores.fetch(limit=request.number_of_results)
else:
selectedScores = scores.fetch()
return ScoreForms(items=[score.to_form() \
for score in selectedScores])
示例10: get_user_rankings
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_user_rankings(self, request):
"""Get the rankings of each player"""
items = []
users = User.query()
for user in users:
wins = 0
guesses = 0
#Use .fetch() to turn query into an iterable list you can take the len() of
scores = Score.query(Score.user == user.key).fetch()
if scores:
for score in scores:
guesses += score.guesses
if score.won:
wins += 1
winning_percentage = 100 * wins/float(len(scores))
items.append(user.to_form(wins, guesses, winning_percentage))
#Lambda defines nameless inline function
#items.sort(key=lambda u: u.winning_percentage, reverse = True)
#http://stackoverflow.com/questions/12749398/using-a-comparator-function-to-sort\
def _compare_user_games(a, b):
"""Sort user rankings by winning_percentage, then # wins, then # guesses"""
if a.winning_percentage != b.winning_percentage:
return int(a.winning_percentage - b.winning_percentage)
elif a.wins != b.wins:
return int(a.wins - b.wins)
else:
return b.guesses - a.guesses
items = sorted(items, cmp = _compare_user_games, reverse = True)
return UserForms(items=items)
示例11: get_user_ranking
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_user_ranking(self, request):
"""Returns the performance of the player as a Ranking."""
user = User.query(User.name == request.user_name).get()
if not user:
raise endpoints.NotFoundException(
'A User with that name does not exist!')
scores = Score.query(Score.user == user.key).fetch()
if not scores:
raise endpoints.NotFoundException(
'No scores were found for that user!')
wins = sum(s.won == True for s in scores)
percent_won = (float(wins)/len(scores)) * 100
number_of_guesses = sum(score.total_incorrect for score in scores)
avg_wrong = float(number_of_guesses)/len(scores)
ranking = Ranking.query(Ranking.user == user.key).get()
if ranking:
ranking.wins = wins
ranking.percent_won = percent_won
ranking.avg_wrong = avg_wrong
ranking.put()
return ranking.to_form("Ranking has been updated for {}".format(
user.name))
else:
ranking = Ranking.new_ranking(user=user.key,
wins=wins,
percent_won=percent_won,
avg_wrong=avg_wrong)
return ranking.to_form("Ranking created for {}".format(user.name))
示例12: get_game_history
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_game_history(self, request):
"""Return Game History"""
game = get_by_urlsafe(request.urlsafe_game_key, Game)
return GameHistoryForm(history=game.history)
for u in users:
scores = Score.query(Score.user == u.key).fetch
tot_guesses = sum([score.guesses for score in scores])
示例13: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self, request):
"""Return the high scores for a given card type. Low scores are good
for Concentration
A "high score" is the fewest number of guesses required to match all of
the cards, so this method returns Score instances by the number of
guesses in ascending order. Because scores can't be compared across
board sizes, the number of card types is a required argument.
Args:
num_card_types (int): The number of card types.
number_of_results (int): The number of results to return.
Returns:
scores(Score[]): An array of Score instances sorted by guesses.
Raises:
BadRequestException: If num_card_types is not between 2 and 13, or
if number_of_results is not greater than zero.
"""
if request.num_card_types not in range(2, 14):
raise endpoints.BadRequestException(
'Number of card types must be between 2 and 13')
if request.number_of_results < 1:
raise endpoints.BadRequestException(
'Number of results must be greater than zero.')
scores = Score.query(Score.num_card_types ==
request.num_card_types).order(
Score.guesses).fetch(
limit=request.number_of_results)
return ScoreForms(items=[score.to_form() for score in scores])
示例14: get_high_scores
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_high_scores(self,request):
scores = Score.query().order(-Score.score)
if request.number_of_results == -1:
return ScoreForms(items=[score.to_form() for score in scores])
else:
results = scores.fetch(limit=request.number_of_results)
return ScoreForms(items=[score.to_form() for score in results])
示例15: get_user_rankings
# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import query [as 别名]
def get_user_rankings(self, request):
"""Gets user rankings"""
items = []
# get all users
query = User.query(User.name != 'Computer')
for user in query:
# get score count
score_query = Score.query(Score.user == user.key)
score_count = score_query.count()
# get win count
win_query = score_query.filter(Score.won == True)
win_count = win_query.count()
if win_count == 0:
ratio = float(0)
else:
ratio = float(win_count) / float(score_count)
items.append([user.name, ratio])
# sort tuples by ratio
items.sort(key=lambda tup: tup[1], reverse=True)
return RankingForms(items=[RankingForm(user_name=item[0], win_ration=item[1]) for item in items])