Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored main branch #1

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open

Sourcery refactored main branch #1

wants to merge 28 commits into from

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Jul 25, 2021

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from RasmusRynell July 25, 2021 11:01
Comment on lines 66 to 88
else:
if not bets_session.query(Bet).filter(and_(Bet.gamePk == game_pk,
elif not bets_session.query(Bet).filter(and_(Bet.gamePk == game_pk,
Bet.playerId == player_id, \
func.lower(Bet.site) == func.lower(bet[4]), \
func.lower(Bet.overUnder) == func.lower(str(bet[7]).replace(",", ".")) \
)).all():
try:
new_bet = Bet()
new_bet.playerId = player_id
new_bet.homeTeamId = home_team
new_bet.awayTeamId = away_team
new_bet.dateTime = time
new_bet.site = bet[4]
new_bet.gamePk = game_pk
new_bet.overUnder = str(bet[7]).replace(",", ".")
new_bet.oddsOver = str(bet[5]).replace(",", ".")
new_bet.oddsUnder = str(bet[6]).replace(",", ".")

bets_session.add(new_bet)
except:
print("Something went wrong, did not add {}".format(bet))
else:
print("Bet already exists")
return
try:
new_bet = Bet()
new_bet.playerId = player_id
new_bet.homeTeamId = home_team
new_bet.awayTeamId = away_team
new_bet.dateTime = time
new_bet.site = bet[4]
new_bet.gamePk = game_pk
new_bet.overUnder = str(bet[7]).replace(",", ".")
new_bet.oddsOver = str(bet[5]).replace(",", ".")
new_bet.oddsUnder = str(bet[6]).replace(",", ".")

bets_session.add(new_bet)
except:
print("Something went wrong, did not add {}".format(bet))
else:
print("Bet already exists")
return
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_bet_to_db refactored with the following changes:

Comment on lines 132 to 162
else:
if "skaterStats" in player_info["stats"]:
res_skater_stats = player_info["stats"]["skaterStats"]

new_skater_stats = SkaterStats()

new_skater_stats.playerId = player_info["person"]["id"]
new_skater_stats.gamePk = info["gamePk"]
new_skater_stats.position = player_info["position"]["code"]
new_skater_stats.team = team_info["team"]["id"]

new_skater_stats.timeOnIce = convert_string_to_time(res_skater_stats["timeOnIce"])
new_skater_stats.assists = res_skater_stats["assists"]
new_skater_stats.goals = res_skater_stats["goals"]
new_skater_stats.shots = res_skater_stats["shots"]
new_skater_stats.hits = res_skater_stats["hits"]
new_skater_stats.powerPlayGoals = res_skater_stats["powerPlayGoals"]
new_skater_stats.powerPlayAssists = res_skater_stats["powerPlayAssists"]
new_skater_stats.penaltyMinutes = res_skater_stats["penaltyMinutes"]
new_skater_stats.faceOffWins = res_skater_stats["faceOffWins"]
new_skater_stats.faceoffTaken = res_skater_stats["faceoffTaken"]
new_skater_stats.takeaways = res_skater_stats["takeaways"]
new_skater_stats.giveaways = res_skater_stats["giveaways"]
new_skater_stats.shortHandedGoals = res_skater_stats["shortHandedGoals"]
new_skater_stats.shortHandedAssists = res_skater_stats["shortHandedAssists"]
new_skater_stats.blocked = res_skater_stats["blocked"]
new_skater_stats.plusMinus = res_skater_stats["plusMinus"]
new_skater_stats.evenTimeOnIce = convert_string_to_time(res_skater_stats["evenTimeOnIce"])
new_skater_stats.powerPlayTimeOnIce = convert_string_to_time(res_skater_stats["powerPlayTimeOnIce"])
new_skater_stats.shortHandedTimeOnIce = convert_string_to_time(res_skater_stats["shortHandedTimeOnIce"])

session.add(new_skater_stats)
elif "skaterStats" in player_info["stats"]:
res_skater_stats = player_info["stats"]["skaterStats"]

new_skater_stats = SkaterStats()

new_skater_stats.playerId = player_info["person"]["id"]
new_skater_stats.gamePk = info["gamePk"]
new_skater_stats.position = player_info["position"]["code"]
new_skater_stats.team = team_info["team"]["id"]

new_skater_stats.timeOnIce = convert_string_to_time(res_skater_stats["timeOnIce"])
new_skater_stats.assists = res_skater_stats["assists"]
new_skater_stats.goals = res_skater_stats["goals"]
new_skater_stats.shots = res_skater_stats["shots"]
new_skater_stats.hits = res_skater_stats["hits"]
new_skater_stats.powerPlayGoals = res_skater_stats["powerPlayGoals"]
new_skater_stats.powerPlayAssists = res_skater_stats["powerPlayAssists"]
new_skater_stats.penaltyMinutes = res_skater_stats["penaltyMinutes"]
new_skater_stats.faceOffWins = res_skater_stats["faceOffWins"]
new_skater_stats.faceoffTaken = res_skater_stats["faceoffTaken"]
new_skater_stats.takeaways = res_skater_stats["takeaways"]
new_skater_stats.giveaways = res_skater_stats["giveaways"]
new_skater_stats.shortHandedGoals = res_skater_stats["shortHandedGoals"]
new_skater_stats.shortHandedAssists = res_skater_stats["shortHandedAssists"]
new_skater_stats.blocked = res_skater_stats["blocked"]
new_skater_stats.plusMinus = res_skater_stats["plusMinus"]
new_skater_stats.evenTimeOnIce = convert_string_to_time(res_skater_stats["evenTimeOnIce"])
new_skater_stats.powerPlayTimeOnIce = convert_string_to_time(res_skater_stats["powerPlayTimeOnIce"])
new_skater_stats.shortHandedTimeOnIce = convert_string_to_time(res_skater_stats["shortHandedTimeOnIce"])

session.add(new_skater_stats)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function hook_factory.add_game_stats_to_db refactored with the following changes:

Comment on lines 177 to 203
base = "https://statsapi.web.nhl.com/api/v1/game/"
urls = []

games_that_dont_have_to_be_updated = {}
games = session.query(Game).filter(Game.statusCode == 7)
for game in games:
games_that_dont_have_to_be_updated[game.gamePk] = game

games_that_dont_have_to_be_updated = {game.gamePk: game for game in games}
if "dates" in res:
base = "https://statsapi.web.nhl.com/api/v1/game/"
for date in res["dates"]:
for game in date["games"]:
if game["gameType"] == "R" or game["gameType"] == "P":
if game["gamePk"] not in games_that_dont_have_to_be_updated.keys():
if session.query(Game).filter(Game.gamePk == game["gamePk"]).first():
remove_gamePk(session, game["gamePk"])

add_game_to_db(session, game)
game_pk = game["gamePk"]
urls.append((base + str(game_pk) + "/boxscore", { "session": session, "gamePk": str(game_pk), "stats" : {game["teams"]["home"]["team"]["id"]: {"wins": game["teams"]["home"]["leagueRecord"]["wins"],
"losses": game["teams"]["home"]["leagueRecord"]["losses"],
"ot": game["teams"]["home"]["leagueRecord"]["ot"] if "ot" in game["teams"]["home"]["leagueRecord"] else "",
"type": game["teams"]["home"]["leagueRecord"]["type"],
"score": game["teams"]["home"]["score"]},
game["teams"]["away"]["team"]["id"]: {"wins": game["teams"]["away"]["leagueRecord"]["wins"],
"losses": game["teams"]["away"]["leagueRecord"]["losses"],
"ot": game["teams"]["away"]["leagueRecord"]["ot"] if "ot" in game["teams"]["away"]["leagueRecord"] else "",
"type": game["teams"]["away"]["leagueRecord"]["type"],
"score": game["teams"]["away"]["score"]}}}))
if (
game["gameType"] in ["R", "P"]
and game["gamePk"]
not in games_that_dont_have_to_be_updated.keys()
):
if session.query(Game).filter(Game.gamePk == game["gamePk"]).first():
remove_gamePk(session, game["gamePk"])

add_game_to_db(session, game)
game_pk = game["gamePk"]
urls.append((base + str(game_pk) + "/boxscore", { "session": session, "gamePk": str(game_pk), "stats" : {game["teams"]["home"]["team"]["id"]: {"wins": game["teams"]["home"]["leagueRecord"]["wins"],
"losses": game["teams"]["home"]["leagueRecord"]["losses"],
"ot": game["teams"]["home"]["leagueRecord"]["ot"] if "ot" in game["teams"]["home"]["leagueRecord"] else "",
"type": game["teams"]["home"]["leagueRecord"]["type"],
"score": game["teams"]["home"]["score"]},
game["teams"]["away"]["team"]["id"]: {"wins": game["teams"]["away"]["leagueRecord"]["wins"],
"losses": game["teams"]["away"]["leagueRecord"]["losses"],
"ot": game["teams"]["away"]["leagueRecord"]["ot"] if "ot" in game["teams"]["away"]["leagueRecord"] else "",
"type": game["teams"]["away"]["leagueRecord"]["type"],
"score": game["teams"]["away"]["score"]}}}))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fill_all_games_from_season refactored with the following changes:

Comment on lines 283 to 294
print(nhl_session.query(Person).filter(
func.lower(Person.fullName).contains(func.lower(name))))
raise "More than one id..."

if len(ids) == 0:
print("Cant find a player with that name for:")
print(name)
return
print(nhl_session.query(Person).filter(
func.lower(Person.fullName).contains(func.lower(name))))
raise "Cant find a player with that name..."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_person_nickname refactored with the following changes:

Comment on lines 313 to 321
raise "More than one id..."

if len(ids) == 0:
print("Cant find a team with that name for:")
print(name)
print(nhl_session.query(Team).filter(func.lower(Team.name).contains(func.lower(name))))
return
raise "Cant find a team with that name..."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_team_nickname refactored with the following changes:

res = []
for i in range(len(tmp)):
if tmp[i] == find:
res.append(i)

res = [i for i in range(len(tmp)) if tmp[i] == find]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_file refactored with the following changes:

Comment on lines 27 to 30
for match in matches:
for match, value in matches.items():
home_team = unidecode(match.split(" @ ")[1])
away_team = unidecode(match.split(" @ ")[0])
for i in range(0, len(matches[match]), 4):
for i in range(0, len(value), 4):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_file refactored with the following changes:

Comment on lines 27 to 30
for match in matches:
for match, value in matches.items():
home_team = unidecode(match.split(" - ")[0])
away_team = unidecode(match.split(" - ")[1])
for i in range(0, len(matches[match]), 3):
for i in range(0, len(value), 3):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_file refactored with the following changes:

Comment on lines 27 to 30
for match in matches:
for match, value in matches.items():
home_team = unidecode(match.split(" - ")[0])
away_team = unidecode(match.split(" - ")[1])
for i in range(0, len(matches[match]), 5):
for i in range(0, len(value), 5):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_file refactored with the following changes:

Comment on lines 27 to 30
for match in matches:
for match, value in matches.items():
home_team = unidecode(match.split(" - ")[0])
away_team = unidecode(match.split(" - ")[1])
for i in range(0, len(matches[match]), 5):
for i in range(0, len(value), 5):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_file refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Jul 25, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.95%.

Quality metrics Before After Change
Complexity 24.66 😞 22.48 😞 -2.18 👍
Method Length 169.45 😞 167.45 😞 -2.00 👍
Working memory 12.81 😞 12.84 😞 0.03 👎
Quality 33.95% 😞 34.90% 😞 0.95% 👍
Other metrics Before After Change
Lines 861 838 -23
Changed files Quality Before Quality After Quality Change
app/source/bets_handler.py 60.02% 🙂 61.17% 🙂 1.15% 👍
app/source/nhl_handler.py 27.63% 😞 28.60% 😞 0.97% 👍
app/source/predict.py 94.99% ⭐ 96.05% ⭐ 1.06% 👍
app/source/bet_parsers/parse_bet365.py 16.98% ⛔ 16.99% ⛔ 0.01% 👍
app/source/bet_parsers/parse_betsson.py 29.62% 😞 34.23% 😞 4.61% 👍
app/source/bet_parsers/parse_betway.py 40.10% 😞 40.05% 😞 -0.05% 👎
app/source/bet_parsers/parse_ss.py 38.09% 😞 38.04% 😞 -0.05% 👎
app/source/bet_parsers/parse_unibet.py 40.45% 😞 40.39% 😞 -0.06% 👎
app/source/bet_parsers/parse_wh.py 40.45% 😞 40.39% 😞 -0.06% 👎

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
app/source/nhl_handler.py hook_factory 47 ⛔ 809 ⛔ 14 😞 13.56% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
app/source/nhl_handler.py hook_factory.add_game_stats_to_db 36 ⛔ 803 ⛔ 14 😞 16.70% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
app/source/bet_parsers/parse_bet365.py read_file 39 ⛔ 398 ⛔ 14 😞 16.99% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
app/source/nhl_handler.py fill_all_games_from_season 26 😞 356 ⛔ 20 ⛔ 17.73% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
app/source/bet_parsers/parse_betsson.py read_file 14 🙂 318 ⛔ 13 😞 34.23% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant