def calc_subrange(range_max, sub_amount, weight):
    """
    return the start and stop points that are sub_amount distance apart and
    contain weight, without going outside the provided range
    """
    if weight > range_max or sub_amount > range_max:
        raise ValueError("sub_amount and weight must be less than range_max. range_max %s, sub_amount %s, weight %s" % (range_max, sub_amount, weight))
    half_amount = sub_amount / 2
    bottom = weight - half_amount
    top = bottom + sub_amount
    if top <= range_max and bottom >= 0:
        return (bottom, top)
    elif weight > range_max / 2:
        # weight is on the upper half, start at the max and go down
        return (range_max - sub_amount, range_max)
    else:
        # weight is on the lower have, start at 0 and go up
        return (0, sub_amount)