def calculate_median(objects, field_name):
    """
    Calculates the median where obj.total is the distribution count and
    getattr(obj, field_name) is the distribution segment.
    Note: this function assumes the objects are sorted.
    """
    total = 0
    for obj in objects:
        total += obj.total
    half = total / 2.0

    counter = 0
    for i, obj in enumerate(objects):
        counter += obj.total
        if counter > half:
            if counter - half == 1:
                # total must be even (otherwise counter - half ends with .5)
                return (
                    float(getattr(objects[i - 1], field_name))
                    + float(getattr(obj, field_name))
                ) / 2.0
            return float(getattr(obj, field_name))
        elif counter == half:
            # total must be even (otherwise half ends with .5)
            return (
                float(getattr(obj, field_name))
                + float(getattr(objects[i + 1], field_name))
            ) / 2.0