Example #1
0
    def finish(self):
        """Displays a message explaining the player that he finished.
           Also automatically progresses to the results screen."""
        self.player_finished = True

        # state.cup.set_results_for_current_track(self.results)

        player_position = self.results.index(self.stats[self.player_car]) + 1
        if player_position == 1:
            finished_text = 'You won!'
        elif player_position == len(self.stats):
            finished_text = 'You finished last'
        else:
            finished_text = 'You finished %s' % (
                util.ordinal(player_position), )

        label = util.Label(text=finished_text,
                           anchor_y='bottom',
                           font_size=40,
                           background=(0, 0, 0, 125))

        label.transform_anchor_x = label.width / 2
        label.x = director.window.width / 2 - label.width / 2
        label.y = director.window.height / 2
        self.add(label, z=100)

        label.scale = 0
        label.do(
            ScaleTo(1, 0.75) + Delay(3) + ScaleTo(0, 0.75) +
            CallFunc(self.remove, label) + CallFunc(self.show_results))
Example #2
0
def maybe_pick_suggestion(words: List[str],
                          linsugs: List[List[str]]) -> Optional[List[str]]:
    i = -1
    if len(linsugs) == 1 and words == ['yes']: i = 0
    if len(linsugs) > 1 and len(words) == 1 and words[0].isdigit():
        i = int(words[0]) - 1
    if len(words) == 3 and words[:2] == ["yes", "the"]:
        i = util.ordinal(words[2])
    if len(words) == 2 and words[0] == 'yes' and words[1].isdigit():
        i = int(words[1]) - 1
    return (linsugs[i] if 0 <= i and i < len(linsugs) else None)
def run(year):
    print("Creating timeline/" + year + "/individual")
    html = templates.get("timeline/year/individual")
    html = templates.initial_replace(html, 1)
    yeardata = t_db_y[year]
    html = html.replace("__YEAR__", year)
    html = html.replace("__NUMBER__", yeardata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"]))
    
    if year in previous_year:
        html = html.replace("__PREVIOUS_YEAR__", previous_year[year])
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_YEAR__", ".") # Google crawler fix
        
    if year in next_year:
        html = html.replace("__NEXT_YEAR__", next_year[year])
        html = html.replace("__NEXT_YEAR_STYLE__", "")
    else:
        html = html.replace("__NEXT_YEAR_STYLE__", "display: none;")
        html = html.replace("__NEXT_YEAR__", ".") # Google crawler fix
    
    tablehtml = ""
    if year in s_db_y:
        for row in s_db_y[year]:
            rowhtml = templates.get("timeline/year/individual_row")
            if row["code"] == "":
                rowhtml = rowhtml.replace("__CODE__", "TUR") # Yup, this is my hack
                rowhtml = rowhtml.replace("__COUNTRY__", "")
            else:
                rowhtml = rowhtml.replace("__CODE__", row["code"])
                rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[row["code"]])
            rowhtml = rowhtml.replace("__NAME__", row["name"])
            rowhtml = rowhtml.replace("__RANK__", ("&ge;" if row["rank>="] else "") + row["rank"])
            if row["medal"] == "G":
                rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_gold"))
            elif row["medal"] == "S":
                rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_silver"))
            elif row["medal"] == "B":
                rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_bronze"))
            elif row["medal"] == "H":
                rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_honourable"))
            else:
                rowhtml = rowhtml.replace("__MEDAL__", "")
            tablehtml += rowhtml
    html = html.replace("__TABLE__", tablehtml)
    
    html = templates.final_replace(html, "../..")
    util.writefile("../timeline/" + year + "/individual.html", html)
Example #4
0
def make_badge_io(entity):
    percentile_int = int(round(entity.impact_percentile * 100))
    percentile_str = ordinal(percentile_int)
    color = "brightgreen"

    url_template = "http://img.shields.io/badge/Depsy-{percentile}%20percentile-{color}.svg?style=flat"
    url = url_template.format(percentile=percentile_str, color=color)

    print "sending request for badge to this URL: ", url
    try:
        ret = requests.get(url).content
    except requests.exceptions.SSLError:
        # fake response to handle fact that SSL on OSX breaks for shields.io
        ret = '<svg xmlns="http://www.w3.org/2000/svg" width="260" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="260" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h158v20H0z"/><path fill="#4c1" d="M158 0h102v20H158z"/><path fill="url(#b)" d="M0 0h260v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="79" y="15" fill="#010101" fill-opacity=".3">Research software impact</text><text x="79" y="14">Research software impact</text><text x="208" y="15" fill="#010101" fill-opacity=".3">42nd percentile</text><text x="208" y="14">42nd percentile</text></g></svg>'

    return StringIO(ret)
Example #5
0
def make_badge_io(entity):
    percentile_int = int(round(entity.impact_percentile * 100))
    percentile_str = ordinal(percentile_int)
    color = "brightgreen"

    url_template = "http://img.shields.io/badge/Depsy-{percentile}%20percentile-{color}.svg?style=flat"
    url = url_template.format(
        percentile=percentile_str,
        color=color
    )

    print "sending request for badge to this URL: ", url
    try:
        ret = requests.get(url).content
    except requests.exceptions.SSLError:
        # fake response to handle fact that SSL on OSX breaks for shields.io
        ret = '<svg xmlns="http://www.w3.org/2000/svg" width="260" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="260" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h158v20H0z"/><path fill="#4c1" d="M158 0h102v20H158z"/><path fill="url(#b)" d="M0 0h260v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="79" y="15" fill="#010101" fill-opacity=".3">Research software impact</text><text x="79" y="14">Research software impact</text><text x="208" y="15" fill="#010101" fill-opacity=".3">42nd percentile</text><text x="208" y="14">42nd percentile</text></g></svg>'


    return StringIO(ret)
Example #6
0
def run(month):
    print("Creating timeline/" + month + "/country")
    html = templates.get("timeline/month/country")
    html = templates.initial_replace(html, 1)
    monthdata = t_db_m[month]
    html = html.replace("__MONTH__", month)
    html = html.replace("__NUMBER__", monthdata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(monthdata["number"]))

    if month in previous_month:
        html = html.replace("__PREVIOUS_MONTH__", previous_month[month])
        html = html.replace("__PREVIOUS_MONTH_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_MONTH_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_MONTH__", ".")  # Google crawler fix

    if month in next_month:
        html = html.replace("__NEXT_MONTH__", next_month[month])
        html = html.replace("__NEXT_MONTH_STYLE__", "")
    else:
        html = html.replace("__NEXT_MONTH_STYLE__", "display: none;")
        html = html.replace("__NEXT_MONTH__", ".")  # Google crawler fix

    medals = {}
    if month in s_db_y:
        for row in s_db_y[month]:
            if row["name"] not in medals:
                medals[row["name"]] = {
                    "bestrank": int(row["rank"]),
                    "bestrank>=": "&ge;" if row["rank>="] else "",
                    "gold": 0,
                    "silver": 0,
                    "bronze": 0,
                    "honourable": 0
                }
            if row["medal"] == "G":
                medals[row["name"]]["gold"] += 1
            elif row["medal"] == "S":
                medals[row["name"]]["silver"] += 1
            elif row["medal"] == "B":
                medals[row["name"]]["bronze"] += 1
            elif row["medal"] == "H":
                medals[row["name"]]["honourable"] += 1

    def keyfn(code):
        m = medals[code]
        return (-m["gold"], -m["silver"], -m["bronze"], -m["honourable"],
                m["bestrank"], code)

    sortedcodes = sorted(medals, key=keyfn)

    tablehtml = ""
    prevcode = ""
    prevrank = 0
    for i, code in enumerate(sortedcodes):
        rowhtml = templates.get("timeline/month/country_row")
        rowhtml = rowhtml.replace("__CODE__", code)
        if prevcode != "" and keyfn(prevcode)[:-1] == keyfn(code)[:-1]:
            rowhtml = rowhtml.replace("__RANK__", prevrank)
        else:
            rowhtml = rowhtml.replace("__RANK__", str(i + 1))
            prevcode = code
            prevrank = str(i + 1)
        rowhtml = rowhtml.replace("__GOLD__", str(medals[code]["gold"]))
        rowhtml = rowhtml.replace("__SILVER__", str(medals[code]["silver"]))
        rowhtml = rowhtml.replace("__BRONZE__", str(medals[code]["bronze"]))
        rowhtml = rowhtml.replace("__HONOURABLE__",
                                  str(medals[code]["honourable"]))
        rowhtml = rowhtml.replace(
            "__BEST_RANK__",
            medals[code]["bestrank>="] + str(medals[code]["bestrank"]))
        tablehtml += rowhtml
    html = html.replace("__TABLE__", tablehtml)

    html = templates.final_replace(html, "../..")
    util.writefile("../dest/timeline/" + month + "/country.html", html)
Example #7
0
def run(year):
    print("Creating timeline/" + year + "/country")
    html = templates.get("timeline/year/country")
    html = templates.initial_replace(html, 1)
    yeardata = t_db_y[year]
    html = html.replace("__YEAR__", year)
    html = html.replace("__NUMBER__", yeardata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"]))

    if year in previous_year:
        html = html.replace("__PREVIOUS_YEAR__", previous_year[year])
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_YEAR__", ".")  # Google crawler fix

    if year in next_year:
        html = html.replace("__NEXT_YEAR__", next_year[year])
        html = html.replace("__NEXT_YEAR_STYLE__", "")
    else:
        html = html.replace("__NEXT_YEAR_STYLE__", "display: none;")
        html = html.replace("__NEXT_YEAR__", ".")  # Google crawler fix

    medals = {}
    if year in s_db_y:
        for row in s_db_y[year]:
            if row["code"] == "":
                # Country unknown
                continue
            if row["code"] not in medals:
                medals[row["code"]] = {
                    "bestrank": int(row["rank"]),
                    "bestrank>=": "&ge;" if row["rank>="] else "",
                    "gold": 0,
                    "silver": 0,
                    "bronze": 0,
                    "honourable": 0
                }
            if row["medal"] == "G":
                medals[row["code"]]["gold"] += 1
            elif row["medal"] == "S":
                medals[row["code"]]["silver"] += 1
            elif row["medal"] == "B":
                medals[row["code"]]["bronze"] += 1
            elif row["medal"] == "H":
                medals[row["code"]]["honourable"] += 1

    def keyfn(code):
        m = medals[code]
        return (m["gold"], m["silver"], m["bronze"], m["honourable"],
                -m["bestrank"])

    sortedcodes = reversed(sorted(medals, key=keyfn))

    tablehtml = ""
    prevcode = ""
    prevrank = 0
    for i, code in enumerate(sortedcodes):
        rowhtml = templates.get("timeline/year/country_row")
        rowhtml = rowhtml.replace("__CODE__", code)
        rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[code])
        if prevcode != "" and keyfn(prevcode) == keyfn(code):
            rowhtml = rowhtml.replace("__RANK__", prevrank)
        else:
            rowhtml = rowhtml.replace("__RANK__", str(i + 1))
            prevcode = code
            prevrank = str(i + 1)
        rowhtml = rowhtml.replace("__GOLD__", str(medals[code]["gold"]))
        rowhtml = rowhtml.replace("__SILVER__", str(medals[code]["silver"]))
        rowhtml = rowhtml.replace("__BRONZE__", str(medals[code]["bronze"]))
        rowhtml = rowhtml.replace("__HONOURABLE__",
                                  str(medals[code]["honourable"]))
        rowhtml = rowhtml.replace(
            "__BEST_RANK__",
            medals[code]["bestrank>="] + str(medals[code]["bestrank"]))
        tablehtml += rowhtml
    html = html.replace("__TABLE__", tablehtml)

    html = templates.final_replace(html, "../..")
    util.writefile("../timeline/" + year + "/country.html", html)
Example #8
0
 def percentile_value_string(self):
     try:
         return ordinal(self.percentile["value"])
     except TypeError:
         return None
def run(year):
    print("Creating timeline/" + year + "/country")
    html = templates.get("timeline/year/country")
    html = templates.initial_replace(html, 1)
    yeardata = t_db_y[year]
    html = html.replace("__YEAR__", year)
    html = html.replace("__NUMBER__", yeardata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"]))
    
    if year in previous_year:
        html = html.replace("__PREVIOUS_YEAR__", previous_year[year])
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_YEAR__", ".") # Google crawler fix
        
    if year in next_year:
        html = html.replace("__NEXT_YEAR__", next_year[year])
        html = html.replace("__NEXT_YEAR_STYLE__", "")
    else:
        html = html.replace("__NEXT_YEAR_STYLE__", "display: none;")
        html = html.replace("__NEXT_YEAR__", ".") # Google crawler fix
    
    medals = {}
    if year in s_db_y:
        for row in s_db_y[year]:
            if row["code"] == "":
                # Country unknown
                continue
            if row["code"] not in medals:
                medals[row["code"]] = {
                    "bestrank": int(row["rank"]),
                    "bestrank>=": "&ge;" if row["rank>="] else "",
                    "gold": 0,
                    "silver": 0,
                    "bronze": 0,
                    "honourable": 0
                    }
            if row["medal"] == "G":
                medals[row["code"]]["gold"] += 1
            elif row["medal"] == "S":
                medals[row["code"]]["silver"] += 1
            elif row["medal"] == "B":
                medals[row["code"]]["bronze"] += 1
            elif row["medal"] == "H":
                medals[row["code"]]["honourable"] += 1
    
    def keyfn(code):
        m = medals[code]
        return (m["gold"], m["silver"], m["bronze"], m["honourable"],
                -m["bestrank"])

    sortedcodes = reversed(sorted(medals, key = keyfn))
    
    tablehtml = ""
    prevcode = ""
    prevrank = 0
    for i, code in enumerate(sortedcodes):
        rowhtml = templates.get("timeline/year/country_row")
        rowhtml = rowhtml.replace("__CODE__", code)
        rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[code])
        if prevcode != "" and keyfn(prevcode) == keyfn(code):
            rowhtml = rowhtml.replace("__RANK__", prevrank)
        else:
            rowhtml = rowhtml.replace("__RANK__", str(i + 1))
            prevcode = code
            prevrank = str(i + 1)
        rowhtml = rowhtml.replace("__GOLD__", str(medals[code]["gold"]))
        rowhtml = rowhtml.replace("__SILVER__", str(medals[code]["silver"]))
        rowhtml = rowhtml.replace("__BRONZE__", str(medals[code]["bronze"]))
        rowhtml = rowhtml.replace("__HONOURABLE__", str(medals[code]["honourable"]))
        rowhtml = rowhtml.replace("__BEST_RANK__", medals[code]["bestrank>="] + str(medals[code]["bestrank"]))
        tablehtml += rowhtml
    html = html.replace("__TABLE__", tablehtml)
    
    html = templates.final_replace(html, "../..")
    util.writefile("../timeline/" + year + "/country.html", html)
Example #10
0
def run(year):
    print("Creating timeline/" + year + "/index")
    html = templates.get("timeline/year/index")
    html = templates.initial_replace(html, 1)
    yeardata = t_db_y[year]
    html = html.replace("__YEAR__", year)
    html = html.replace("__NUMBER__", yeardata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"]))
    html = html.replace("__DATE__", yeardata["date"])
    html = html.replace("__CODE__", yeardata["code"])
    html = html.replace("__COUNTRY__", code_to_country[yeardata["code"]])

    if "code2" in yeardata:
        html = html.replace("__CODE2__", yeardata["code2"])
        html = html.replace("__COUNTRY2__", code_to_country[yeardata["code2"]])
        html = html.replace("__CODE2_STYLE__", "")
    else:
        html = html.replace("__CODE2_STYLE__", "display: none;")
        html = html.replace("__CODE2__", ".")  # Google crawler fix

    if yeardata["city"] != "":
        html = html.replace("__CITY__", yeardata["city"] + ",")
    else:
        html = html.replace("__CITY__", "")

    if year in previous_year:
        html = html.replace("__PREVIOUS_YEAR__", previous_year[year])
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_YEAR__", ".")  # Google crawler fix

    if year in next_year:
        html = html.replace("__NEXT_YEAR__", next_year[year])
        html = html.replace("__NEXT_YEAR_STYLE__", "")
    else:
        html = html.replace("__NEXT_YEAR_STYLE__", "display: none;")
        html = html.replace("__NEXT_YEAR__", ".")  # Google crawler fix

    if yeardata["p_student"] != "":
        html = html.replace("__P_STUDENT_STYLE__", "")
        html = html.replace("__P_STUDENT__", yeardata["p_student"])
    else:
        html = html.replace("__P_STUDENT_STYLE__", "display: none;")

    if yeardata["p_country"] != "":
        html = html.replace("__P_COUNTRY_STYLE__", "")
        html = html.replace("__P_COUNTRY__", yeardata["p_country"])
    else:
        html = html.replace("__P_COUNTRY_STYLE__", "display: none;")

    if yeardata["homepage"] != "":
        html = html.replace("__HOMEPAGE_STYLE__", "")
        html = html.replace("__HOMEPAGE__", yeardata["homepage"])
    else:
        html = html.replace("__HOMEPAGE_STYLE__", "display: none;")
        html = html.replace("__HOMEPAGE__", ".")  # Google crawler fix

    gold = 0
    silver = 0
    bronze = 0
    honourable = 0
    if year in s_db_y:
        for studentdata in s_db_y[year]:
            if studentdata["medal"] == "G":
                gold += 1
            elif studentdata["medal"] == "S":
                silver += 1
            elif studentdata["medal"] == "B":
                bronze += 1
            elif studentdata["medal"] == "H":
                honourable += 1
        html = html.replace("__AWARDS_STYLE__", "")
        html = html.replace("__GOLD__", str(gold))
        html = html.replace("__SILVER__", str(silver))
        html = html.replace("__BRONZE__", str(bronze))
        html = html.replace("__HONOURABLE__", str(honourable))
    else:
        html = html.replace("__AWARDS_STYLE__", "display: none;")

    html = templates.final_replace(html, "../..")
    util.writefile("../timeline/" + year + "/index.html", html)
Example #11
0
 def num_profile_products_this_good_ordinal(self):
     return util.ordinal(self.num_profile_products_this_good)
Example #12
0
def run(month):
    print("Creating timeline/" + month + "/individual")
    html = templates.get("timeline/month/individual")
    html = templates.initial_replace(html, 1)
    monthdata = t_db_m[month]
    html = html.replace("__MONTH__", month)
    html = html.replace("__CONTEST_NAME__", monthdata["name"])
    html = html.replace("__NUMBER__", monthdata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(monthdata["number"]))

    if month in previous_month:
        html = html.replace("__PREVIOUS_MONTH__", previous_month[month])
        html = html.replace("__PREVIOUS_MONTH_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_MONTH_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_MONTH__", ".")  # Google crawler fix

    if month in next_month:
        html = html.replace("__NEXT_MONTH__", next_month[month])
        html = html.replace("__NEXT_MONTH_STYLE__", "")
    else:
        html = html.replace("__NEXT_MONTH_STYLE__", "display: none;")
        html = html.replace("__NEXT_MONTH__", ".")  # Google crawler fix

    tablehtml = ""
    anonymous_found = False
    if month in s_db_y:
        for row in s_db_y[month]:
            rowhtml = templates.get("timeline/month/individual_row_" +
                                    str(len(row["scores"])))
            rowhtml = rowhtml.replace("__TOTAL_SCORE__",
                                      str(row["total_score"]))
            rowhtml = rowhtml.replace("__RANK__", str(row["rank"]))

            for i, x in enumerate(row["scores"]):
                rowhtml = rowhtml.replace(f"__SCORE_{i+1}__", str(x))

            if row["medal"] == "G":
                rowhtml = rowhtml.replace(
                    "__MEDAL__",
                    templates.get("timeline/month/individual_gold"))
            elif row["medal"] == "S":
                rowhtml = rowhtml.replace(
                    "__MEDAL__",
                    templates.get("timeline/month/individual_silver"))
            elif row["medal"] == "B":
                rowhtml = rowhtml.replace(
                    "__MEDAL__",
                    templates.get("timeline/month/individual_bronze"))
            elif row["medal"] == "H":
                rowhtml = rowhtml.replace(
                    "__MEDAL__",
                    templates.get("timeline/month/individual_honourable"))
            else:
                rowhtml = rowhtml.replace("__MEDAL__", "")

            if row["is_anonymous"]:
                rowhtml = rowhtml.replace("__NAME__", "")
                rowhtml = rowhtml.replace("__USER_ID__", "")
                tablehtml += rowhtml
                anonymous_found = True
            else:
                rowhtml = rowhtml.replace("__NAME__", row["name"])
                rowhtml = rowhtml.replace("__USER_ID__", row["user-id"])
                tablehtml += rowhtml

    header = ""
    script = ""
    if month in s_db_y and len(s_db_y[month]) >= 1:
        header = templates.get("timeline/month/individual_header_" +
                               str(len(s_db_y[month][0]["scores"])))
        script = templates.get("timeline/month/individual_script_" +
                               str(len(s_db_y[month][0]["scores"])))

    html = html.replace("__TABLE__", tablehtml)
    html = html.replace("__TABLE_HEADER__", header)
    html = html.replace("__SORT_SCRIPT__", script)

    html = html.replace(
        "__ANONYMOUS_MSG__",
        "The names of anonymous contestants are not shown."
        if anonymous_found else "")

    html = templates.final_replace(html, "../..")
    util.writefile("../dest/timeline/" + month + "/individual.html", html)
Example #13
0
def run(month):
    print("Creating timeline/" + month + "/index")
    html = templates.get("timeline/month/index")
    html = templates.initial_replace(html, 1)
    monthdata = t_db_m[month]
    html = html.replace("__MONTH__", month)
    html = html.replace("__CONTEST_NAME__", monthdata["name"])
    html = html.replace("__NUMBER__", monthdata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(monthdata["number"]))
    html = html.replace("__DATE__", monthdata["date"])

    if month in previous_month:
        html = html.replace("__PREVIOUS_MONTH__", previous_month[month])
        html = html.replace("__PREVIOUS_MONTH_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_MONTH_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_MONTH__", ".")  # Google crawler fix

    if month in next_month:
        html = html.replace("__NEXT_MONTH__", next_month[month])
        html = html.replace("__NEXT_MONTH_STYLE__", "")
    else:
        html = html.replace("__NEXT_MONTH_STYLE__", "display: none;")
        html = html.replace("__NEXT_MONTH__", ".")  # Google crawler fix

    if monthdata["p_student"] != "":
        html = html.replace("__P_STUDENT_STYLE__", "")
        html = html.replace("__P_STUDENT__", monthdata["p_student"])
    else:
        html = html.replace("__P_STUDENT_STYLE__", "display: none;")

    gold = 0
    silver = 0
    bronze = 0
    honourable = 0
    if month in contest_results:
        for studentdata in contest_results[month]:
            if studentdata["medal"] == "G":
                gold += 1
            elif studentdata["medal"] == "S":
                silver += 1
            elif studentdata["medal"] == "B":
                bronze += 1
            elif studentdata["medal"] == "H":
                honourable += 1
        html = html.replace("__AWARDS_STYLE__", "")
        html = html.replace("__GOLD__", str(gold))
        html = html.replace("__SILVER__", str(silver))
        html = html.replace("__BRONZE__", str(bronze))
        html = html.replace("__HONOURABLE__", str(honourable))
    else:
        html = html.replace("__AWARDS_STYLE__", "display: none;")

    if month in contest_results:
        html = html.replace("__INDIVIDUAL_STYLE__", "")
        num_problems = len(contest_results[month][0]["scores"])
        header = templates.get(f"timeline/month/stats/header_{num_problems}")
        count = [[0 for _ in range(8)] for _ in range(num_problems)]
        for entry in contest_results[month]:
            for i, score in enumerate(entry["scores"]):
                count[i][score] += 1
        nums = ""
        for score in range(8):
            rowhtml = templates.get(f"timeline/month/stats/num_{num_problems}")
            rowhtml = rowhtml.replace("__SCORE__", str(score))
            for i in range(num_problems):
                rowhtml = rowhtml.replace(f"__P{i+1}__", str(count[i][score]))
            nums += rowhtml
        means = templates.get(f"timeline/month/stats/index_value_{num_problems}") \
                    .replace("__NAME__", "Mean") \
                    .replace("__STYLE__", "mean")
        maxs = templates.get(f"timeline/month/stats/index_value_{num_problems}") \
                    .replace("__NAME__", "Max") \
                    .replace("__STYLE__", "max")
        sigmas = templates.get(f"timeline/month/stats/index_value_{num_problems}") \
                    .replace("__NAME__", "σ") \
                    .replace("__STYLE__", "sigma")
        for i in range(num_problems):
            total = sum(count[i])
            total_score = sum(score * count[i][score] for score in range(8))
            mean = total_score / total
            means = means.replace(f"__P{i+1}__", "%.3f" % mean)
            max_score = max(score for score in range(8)
                            if count[i][score] or score == 0)
            maxs = maxs.replace(f"__P{i+1}__", str(max_score))
            variance = sum(
                pow((score - mean), 2) * count[i][score]
                for score in range(8)) / total
            sigmas = sigmas.replace(f"__P{i+1}__", "%.3f" % sqrt(variance))

        html = html.replace("__STATS_STYLE__", "") \
                   .replace("__TABLE_HEADER__", header) \
                   .replace("__TABLE_NUM__", nums) \
                   .replace("__TABLE_MEAN__", means) \
                   .replace("__TABLE_MAX__", maxs) \
                   .replace("__TABLE_SIGMA__", sigmas)
    else:
        html = html.replace("__INDIVIDUAL_STYLE__", "display: none;")
        html = html.replace("__STATS_STYLE__", "display: none;")

    html = templates.final_replace(html, "../..")
    util.writefile("../dest/timeline/" + month + "/index.html", html)
def run(year):
    print("Creating timeline/" + year + "/index")
    html = templates.get("timeline/year/index")
    html = templates.initial_replace(html, 1)
    yeardata = t_db_y[year]
    html = html.replace("__YEAR__", year)
    html = html.replace("__NUMBER__", yeardata["number"])
    html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"]))
    html = html.replace("__DATE__", yeardata["date"])
    html = html.replace("__CODE__", yeardata["code"])
    html = html.replace("__COUNTRY__", code_to_country[yeardata["code"]])
    
    if "code2" in yeardata:
        html = html.replace("__CODE2__", yeardata["code2"])
        html = html.replace("__COUNTRY2__", code_to_country[yeardata["code2"]])
        html = html.replace("__CODE2_STYLE__", "")
    else:
        html = html.replace("__CODE2_STYLE__", "display: none;")
        html = html.replace("__CODE2__", ".") # Google crawler fix
    
    if yeardata["city"] != "":
        html = html.replace("__CITY__", yeardata["city"] + ",")
    else:
        html = html.replace("__CITY__", "")
    
    if year in previous_year:
        html = html.replace("__PREVIOUS_YEAR__", previous_year[year])
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "")
    else:
        html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;")
        html = html.replace("__PREVIOUS_YEAR__", ".") # Google crawler fix
        
    if year in next_year:
        html = html.replace("__NEXT_YEAR__", next_year[year])
        html = html.replace("__NEXT_YEAR_STYLE__", "")
    else:
        html = html.replace("__NEXT_YEAR_STYLE__", "display: none;")
        html = html.replace("__NEXT_YEAR__", ".") # Google crawler fix
    
    if yeardata["p_student"] != "":
        html = html.replace("__P_STUDENT_STYLE__", "")
        html = html.replace("__P_STUDENT__", yeardata["p_student"])
    else:
        html = html.replace("__P_STUDENT_STYLE__", "display: none;")
    
    if yeardata["p_country"] != "":
        html = html.replace("__P_COUNTRY_STYLE__", "")
        html = html.replace("__P_COUNTRY__", yeardata["p_country"])
    else:
        html = html.replace("__P_COUNTRY_STYLE__", "display: none;")
    
    if yeardata["homepage"] != "":
        html = html.replace("__HOMEPAGE_STYLE__", "")
        html = html.replace("__HOMEPAGE__", yeardata["homepage"])
    else:
        html = html.replace("__HOMEPAGE_STYLE__", "display: none;")
        html = html.replace("__HOMEPAGE__", ".") # Google crawler fix
    
    gold = 0
    silver = 0
    bronze = 0
    honourable = 0
    if year in s_db_y:
        for studentdata in s_db_y[year]:
            if studentdata["medal"] == "G":
                gold += 1
            elif studentdata["medal"] == "S":
                silver += 1
            elif studentdata["medal"] == "B":
                bronze += 1
            elif studentdata["medal"] == "H":
                honourable += 1
        html = html.replace("__AWARDS_STYLE__", "")
        html = html.replace("__GOLD__", str(gold))
        html = html.replace("__SILVER__", str(silver))
        html = html.replace("__BRONZE__", str(bronze))
        html = html.replace("__HONOURABLE__", str(honourable))
    else:
        html = html.replace("__AWARDS_STYLE__", "display: none;")
    
    html = templates.final_replace(html, "../..")
    util.writefile("../timeline/" + year + "/index.html", html)