import math


def get_pretty_value(ugly_value):
    """Taken directly from pbpy.

    Returns a number like 1, 2, 5, 20, 50, 100, 200, 500, 1000 which is
    closest to the input value (in log space).

    :param ugly_value: A number for which a nearby, aesthetically pleasing
        number will be found.
    """
    if ugly_value <= 0.0:
        return 0
    v_log10 = math.log(ugly_value) / math.log(10.0)
    iv = math.floor(v_log10)
    mantissa = v_log10 - iv
    targets = [1.0, 2.0, 5.0, 10.0]
    best_dist = 100.0
    best_target = 0
    for t in map(math.log10, targets):
        d = abs(mantissa - t)
        if d < best_dist:
            best_dist = d
            best_target = t
    return max(1, int(round(math.pow(10.0, iv + best_target))))