I've got a program that takes information about a song's chart run on a user's personal charts from its chart page and inputs that data into a Google spreadsheet, and as part of that I use a bunch of if statements both to initialise a dictionary based on how long the user's charts are and to determine if the chart run was consecutive or not.
The code that I have works, but feels very inefficient and messy. Is there any way to do this in a loop instead? (And yes, I have seen this question, but I don't feel like its answers are particularly useful for my situation.)
length_of_charts = int(input("Enter how many songs you have in each of your weekly charts: "))
weeks_in_range = {"Weeks at #1": [0, "Consecutive"]}
if length_of_charts > 3:
weeks_in_range["Weeks in Top 3"] = [0, "Consecutive"]
if length_of_charts > 5:
weeks_in_range["Weeks in Top 5"] = [0, "Consecutive"]
if length_of_charts > 10:
weeks_in_range["Weeks in Top 10"] = [0, "Consecutive"]
if length_of_charts > 20:
weeks_in_range["Weeks in Top 20"] = [0, "Consecutive"]
if length_of_charts > 40:
weeks_in_range["Weeks in Top 40"] = [0, "Consecutive"]
if length_of_charts > 75:
weeks_in_range["Weeks in Top 75"] = [0, "Consecutive"]
if length_of_charts > 100:
weeks_in_range["Weeks in Top 100"] = [0, "Consecutive"]
if length_of_charts != 1:
weeks_in_range["Weeks in Top " + str(length_of_charts)] = [0, "Consecutive"]
song_charts = driver1.find_elements_by_xpath(song_charts_path)
#finds all of the charts that the song is on
song_chart_runs = [[None for i in range(2)] for j in range(len(song_charts))]
for i, song_chart in enumerate(song_charts):
song_chart_runs[i][0] = int(song_chart.get_attribute("innerHTML").split("""<span class="history-position">""")[1].split("</span>")[0])
#variable will be assigned as the song's chart position on that week
song_chart_runs[i][1] = datetime.strptime(song_chart.get_attribute("innerHTML").split("</span></a></span>")[0].split(">")[-1], '%B %d, %Y')
#variable will be assigned as the date of the chart
if song_chart_runs[i][0] == 1:
weeks_in_range["Weeks at #1"][0] = weeks_in_range["Weeks at #1"][0] + 1
if i > 0 and weeks_in_range["Weeks at #1"][0] > 1 and (song_chart_runs[i-1][0] != 1 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks at #1"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= 3 and song_chart_runs[i][0] <= length_of_charts and length_of_charts > 3:
weeks_in_range["Weeks in Top 3"][0] = weeks_in_range["Weeks in Top 3"][0] + 1
if i > 0 and weeks_in_range["Weeks in Top 3"][0] > 1 and (song_chart_runs[i-1][0] > 3 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top 3"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= 5 and song_chart_runs[i][0] <= length_of_charts and length_of_charts > 5:
weeks_in_range["Weeks in Top 5"][0] = weeks_in_range["Weeks in Top 5"][0] + 1
if i > 0 and weeks_in_range["Weeks in Top 5"][0] > 1 and (song_chart_runs[i-1][0] > 5 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top 5"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= 10 and song_chart_runs[i][0] <= length_of_charts and length_of_charts > 10:
weeks_in_range["Weeks in Top 10"][0] = weeks_in_range["Weeks in Top 10"][0] + 1
if i > 0 and weeks_in_range["Weeks in Top 10"][0] > 1 and (song_chart_runs[i-1][0] > 10 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top 10"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= 20 and song_chart_runs[i][0] <= length_of_charts and length_of_charts > 20:
weeks_in_range["Weeks in Top 20"][0] = weeks_in_range["Weeks in Top 20"][0] + 1
if i > 0 and weeks_in_range["Weeks in Top 20"][0] > 1 and (song_chart_runs[i-1][0] > 20 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top 20"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= 40 and song_chart_runs[i][0] <= length_of_charts and length_of_charts > 40:
weeks_in_range["Weeks in Top 40"][0] = weeks_in_range["Weeks in Top 40"][0] + 1
if i > 0 and weeks_in_range["Weeks in Top 40"][0] > 1 and (song_chart_runs[i-1][0] > 40 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top 40"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= 75 and song_chart_runs[i][0] <= length_of_charts and length_of_charts > 75:
weeks_in_range["Weeks in Top 75"][0] = weeks_in_range["Weeks in Top 75"][0] + 1
if i > 0 and weeks_in_range["Weeks in Top 75"][0] > 1 and (song_chart_runs[i-1][0] > 75 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top 75"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= 100 and song_chart_runs[i][0] <= length_of_charts and length_of_charts > 100:
weeks_in_range["Weeks in Top 100"][0] = weeks_in_range["Weeks in Top 100"][0] + 1
if i > 0 and weeks_in_range["Weeks in Top 100"][0] > 1 and (song_chart_runs[i-1][0] > 100 or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top 100"][1] = "Non-Consecutive"
if song_chart_runs[i][0] <= length_of_charts and length_of_charts != 1:
weeks_in_range["Weeks in Top " + str(length_of_charts)][0] = weeks_in_range["Weeks in Top " + str(length_of_charts)][0] + 1
if i > 0 and weeks_in_range["Weeks in Top " + str(length_of_charts)][0] > 1 and (song_chart_runs[i-1][0] > length_of_charts or song_chart_runs[i-1][1] + timedelta(days=7) != song_chart_runs[i][1]):
weeks_in_range["Weeks in Top " + str(length_of_charts)][1] = "Non-Consecutive"