Exemplo n.º 1
0
def _add_header_and_get_markdown_table(names: Tuple[str],
                                       means: Tuple[Any],
                                       medians: Tuple[int],
                                       stddevs: Tuple[int],
                                       size_cut: Optional[int] = None) -> str:
    """Generates a markdown table with proper headers for benchmarks.

  Args:
  - size_cut: If not None, only show the top N results for each table.
  """
    total_size = len(names)
    if size_cut is not None:
        names = names[0:size_cut]
        means = means[0:size_cut]
        medians = medians[0:size_cut]
        stddevs = stddevs[0:size_cut]

    names = tuple([_make_benchmark_clickable(name) for name in names])
    names = ("Benchmark Name", ) + names
    means = ("Average Latency (ms)", ) + means
    medians = ("Median Latency (ms)", ) + medians
    stddevs = ("Latency Standard Deviation (ms)", ) + stddevs

    table_str = md.table([names, means, medians, stddevs])
    if size_cut is not None and size_cut < total_size:
        table_str += "\n\n"
        table_str += md.italics(
            f"[Top {size_cut} out of {total_size} benchmark results showed]")
    return table_str
Exemplo n.º 2
0
def test_centre_table():
    t = md.table([["foobar"] * 10] * 5)
    lines = t.split("\n")
    centred = osubot.markdown.centre_table(t)
    centred_lines = centred.split("\n")
    assert centred_lines[1] == ":-:|:-:|:-:|:-:|:-:"
    assert lines[0] == centred_lines[0]
    assert "\n".join(lines[2:]) == "\n".join(centred_lines[2:])
Exemplo n.º 3
0
def player_table(ctx):
    """Build a table with player information."""
    if not ctx.player:
        return None
    p = ctx.player
    if not p.pp_raw:  # Player is inactive so most stats are null.
        return None

    rank = "#%s (#%s %s)" % (sep(p.pp_rank), sep(p.pp_country_rank), p.country)

    player_url = "%s/u/%d" % (consts.osu_url, p.user_id)
    old_username = scrape.player_old_username(ctx)
    if old_username and old_username.lower() != p.username.lower():
        player_url += " \"Previously known as '%s'\"" % old_username
    player_link = md.link(nonbreaking(escape(p.username)), player_url)

    cols = [
        ["Player", player_link],
        ["Rank", nonbreaking(rank)],
        ["pp", sep(round(p.pp_raw))],
        ["Accuracy",
         "%s%%" % round_to_str(p.accuracy, 2, force=True)],
        ["Playcount", sep(p.playcount)],
    ]

    # There's no point getting playstyle for non-standard players.
    playstyle = scrape.playstyle(ctx) if ctx.mode == consts.std else None
    if playstyle is not None:
        cols.insert(4, ["Playstyle", playstyle])  # Place after acc.

    mode = ctx.mode if ctx.mode is not None else consts.std
    scores = safe_call(
        consts.osu_api.get_user_best,
        ctx.player.user_id,
        mode=consts.int2osuapimode[mode],
        limit=1,
    )
    if scores:
        score = scores[0]
        beatmaps = safe_call(
            consts.osu_api.get_beatmaps,
            beatmap_id=score.beatmap_id,
            mode=consts.int2osuapimode[mode],
            include_converted=True,
        )
        if beatmaps:
            bmap = beatmaps[0]
            map_url = "%s/b/%d" % (consts.osu_url, bmap.beatmap_id)
            if ctx.mode is not None:
                map_url += "?m=%d" % ctx.mode

            ctx_clone = copy.deepcopy(ctx)
            ctx_clone.beatmap = bmap
            ctx_clone.mods = score.enabled_mods.value
            ctx_clone.mode = mode
            hover = map_hover(ctx_clone, oldmap=ctx.beatmap, oldmods=ctx.mods)

            if hover:
                map_url += ' "%s"' % hover

            map_link = md.link(nonbreaking(escape(map_str(bmap))), map_url)

            mods = combine_mods(score.enabled_mods.value)
            buf = "%s %s " % (mods, consts.bar) if mods else ""

            buf += "%s%%" % round_to_str(accuracy(score, mode), 2, force=True)

            if score.pp:
                buf += " %s %spp" % (consts.bar, sep(round(score.pp)))

            cols.append(["Top Play", "%s %s" % (map_link, nonbreaking(buf))])

    return centre_table(md.table([[str(x) for x in col] for col in cols]))
Exemplo n.º 4
0
def map_table(ctx):
    """Build a table with map difficulty and pp values."""
    if not ctx.beatmap:
        return None

    nomod = diff.diff_vals(ctx, modded=False)
    if nomod is None:
        return None
    if ctx.mods == consts.nomod:
        modded = None
    else:
        modded = diff.diff_vals(ctx, modded=True)

    r = round_to_str
    if modded:
        cols = [
            ["", "NoMod", combine_mods(ctx.mods)],
            ["CS", r(nomod["cs"], 1),
             r(modded["cs"], 1)],
            ["AR", r(nomod["ar"], 1),
             r(modded["ar"], 1)],
            ["OD", r(nomod["od"], 1),
             r(modded["od"], 1)],
            ["HP", r(nomod["hp"], 1),
             r(modded["hp"], 1)],
            [
                "SR",
                r(nomod["sr"], 2, force=True),
                r(modded["sr"], 2, force=True),
            ],
            ["BPM", round(nomod["bpm"]),
             round(modded["bpm"])],
            [
                "Length",
                s_to_ts(nomod["length"]),
                s_to_ts(modded["length"]),
            ],
        ]
    else:
        cols = [
            ["CS", r(nomod["cs"], 1)],
            ["AR", r(nomod["ar"], 1)],
            ["OD", r(nomod["od"], 1)],
            ["HP", r(nomod["hp"], 1)],
            ["SR", r(nomod["sr"], 2, force=True)],
            ["BPM", round(nomod["bpm"])],
            ["Length", s_to_ts(nomod["length"])],
        ]

    pp_vals = {}
    for acc in filter(bool, set([95, 98, 99, 100, ctx.acc])):
        nomod_pp = pp.pp_val(ctx, acc, modded=False)
        if nomod_pp is None:
            continue

        if modded:
            modded_pp = pp.pp_val(ctx, acc, modded=True)
            if modded_pp is not None:
                pp_vals[acc] = nomod_pp, modded_pp
        else:
            pp_vals[acc] = nomod_pp, None

    accs_joined = (" %s " % consts.bar).join(
        ("%s%%" % (r(a, 2, force=True) if int(a) != a else str(a))
         for a in sorted(pp_vals.keys())))
    nomod_joined = (" %s " % consts.bar).join(
        (sep(round(pp_vals[acc][0])) for acc in sorted(pp_vals.keys())))

    if pp_vals:
        cols.append(["pp (%s)" % accs_joined, nomod_joined])
        if modded:
            modded_joined = (" % s " % consts.bar).join(
                (sep(round(pp_vals[acc][1]))
                 for acc in sorted(pp_vals.keys())))
            cols[-1].append(modded_joined)

    return centre_table(md.table([[str(x) for x in col] for col in cols]))