I'm currently working on an API package. Basically what it does is scrapes team data and stores them in list of Team objects. The team object contains data such as Team.name,rank, country etc.
If a user wants to filter that list to find something in it, I have this really messy and ugly (but working) function:
def find_team(self, name=None, country=None, rank_exactly=None, score_exactly=None, rank_change=None, team_id=None,
score_lower=None, score_higher=None, partial_name=None, rank_lower=None, rank_higher=None):
"""
Finds a team by provided keyword attributes, returns a list of teams matching the filter settings
Keyword Arguments:
name - exact team name (case insensitive)
country - team origin country
rank_exactly - rank exactly
rank_lower - only teams with lower rank than provided
rank_higher - only teams with higher rank than provided
rank_change - the state of ank change
score_exactly - score exactly
score_lower - only teams with lower score than provided
score_higher - only teams with higher score than provided
team_id = exact team id
partial_name - part of name
"""
if not self.teams:
raise AttributeError("Object team list is empty")
results = []
for team in self.teams:
all_args=[]
if name:
if name.lower() == team.name.lower():
all_args.append(True)
else:
all_args.append(False)
if country:
if country.lower() == team.country.lower():
all_args.append(True)
else:
all_args.append(False)
if rank_exactly:
if rank_exactly == team.rank:
all_args.append(True)
else:
all_args.append(False)
if rank_higher:
if int(rank_higher) > int(team.rank):
all_args.append(True)
else:
all_args.append(False)
if rank_lower:
if int(rank_lower) < int(team.rank):
all_args.append(True)
else:
all_args.append(False)
if score_exactly:
if score_exactly == team.score:
all_args.append(True)
else:
all_args.append(False)
if rank_change:
if rank_change == team.rank_change:
all_args.append(True)
else:
all_args.append(False)
if team_id:
if team_id == team.team_id:
all_args.append(True)
else:
all_args.append(False)
if score_higher:
if int(score_higher) < int(team.score.replace(',','')):
all_args.append(True)
else:
all_args.append(False)
if score_lower:
if int(score_lower) > int(team.score.replace(',','')):
all_args.append(True)
else:
all_args.append(False)
if partial_name:
if partial_name.lower() in team.name.lower():
all_args.append(True)
else:
all_args.append(False)
if all(all_args):
results.append(team)
return results
How could I make this more Pythonic and just better in general?