Last active
June 29, 2016 13:26
-
-
Save qgreg/1ec6fb9dee3bf9013eabcf9e74f260b6 to your computer and use it in GitHub Desktop.
Using In Operator to Test for a Named Attribute for a List Data Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def provideRatings(teams=None): | |
""" Provides ratings object for generating ratings table in a template. If no teams | |
are provided, provide all teams for now result. | |
Arg: A teams object. | |
Returns: ratings object | |
""" | |
# Store player | |
player_id = login_session['player_id'] | |
# If not teams are provided, provide active teams | |
if not teams: | |
teams = activeTeams() | |
# Generate rankings for the teams | |
ratings_global_sort = db.session.query( | |
BallRatingPlayer.ball_id.label('ball_id'), | |
BallRatingPlayer.rating.label('p_rating'), | |
BallRatingPlayer.wins.label('p_wins'), | |
BallRatingPlayer.games.label('p_games'), | |
BallRatingGlobal.rating.label('g_rating'), | |
Ball.name.label('name'), Ball.team_id.label('team_id'))\ | |
.join(Ball, Ball.id == BallRatingPlayer.item_id)\ | |
.join(BallRatingGlobal, Balltem.id == BallRatingGlobal.ball_id)\ | |
.filter(Ball.category_id == teams.id)\ | |
.filter(BallRatingPlayer.player_id==player_id)\ | |
.order_by('teams_id, g_rating DESC').all() | |
# Query theams_ids for html template | |
teams_ids = db.session.query(Team.id, Team.name)\ | |
.filter(Team.id==teams.id).all() | |
# Add ordinal numbers by global ratings, including ties | |
gr_numbered = [] | |
prev_ci = None | |
for gr in ratings_global_sort: | |
print "gr", gr.teams_id, gr.ball_id, gr.g_rating | |
if prev_ci is None or gr.teams_id != prev_ci: | |
gr_team_counter = 1 | |
current_ord = 1 | |
prev_rating = 0 | |
if gr.g_rating == prev_rating: | |
gr.g_ord = current_ord | |
else: | |
gr.g_ord = gr_team_counter | |
current_ord = gr_team_counter | |
gr_numbered.append(gr) | |
prev_rating = gr.g_rating | |
prev_ci = gr.team_id | |
gr_team_counter += 1 | |
# Resort by player ratings, then add ordinal numbers | |
ratings = [] | |
prev_ci = None | |
for pr in sorted( | |
gr_numbered, | |
key = lambda obj: (obj.team_id, -obj.p_wins, -obj.p_games, -obj.p_rating)): | |
print "pr", pr.team_id, pr.ball_id, pr.p_rating | |
if prev_ci is None or pr.team_id != prev_ci: | |
pr_team_counter = 1 | |
pr_current_ord = 1 | |
prev_rating = 0 | |
if pr.p_rating == prev_rating: | |
pr.p_ord = pr_current_ord | |
else: | |
pr.p_ord = pr_team_counter | |
pr_current_ord = pr_team_counter | |
prev_ci = pr.team_id | |
ratings.append(pr) | |
prev_rating = pr.p_rating | |
pr_team_counter += 1 | |
return ratings | |
@team_bp.route('/team/<name>/') | |
def showTeam(name): | |
"""Render a team page, including all balls. | |
Args: A team name | |
Returns: Team page for the given name. | |
""" | |
# Store team information for given name and all balls for the team | |
team = Team.query.filter(Team.name==name).first_or_404() | |
balls = Ball.query.filter_by( | |
team_id=team.id).order_by(Ball.name).all() | |
# If players is logged in, get player ratings | |
if 'player_id' in login_session: | |
ratings = providePlayerRatings(team) | |
# Test that p_ord is present and has data | |
# This lists the ordinal numbers | |
for r in ratings: | |
print r.p_ord | |
else: | |
# Only global ratings. This does not contain p_ord | |
ratings = provideGlobalRatings(team) | |
# Test in operator | |
# This statement is currently False, but | |
print "Ratings print test", ('p_ord' in ratings) | |
return render_template( | |
'showCategory.html', team=team, balls=balls, | |
ratings=ratings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The surprising result is in line 98: 'p_ord' in ratings. I use it in a print statement here, but I had hoped to use it in a Jinja template to use different HTML based on if the ratings are player or global.