def find_nearest_duration(dur, possible_durations, direction='<>'):
    """
    dur: a Dur or a float (will be converted to Dur via .fromfloat)
    possible_durations: a seq of Durs
    direction: "<"  -> find a dur from possible_durations which is lower than dur
               ">"  -> find a dur from possible_durations which is higher than dur
               "<>" -> find the nearest dur in possible_durations

    Example
    ~~~~~~~

    >>> possible_durations = [0.5, 0.75, 1]
    >>> find_nearest_duration(0.61, possible_durations, "<>")
    0.5

    """
    possdurs = sorted(possible_durations, key=lambda d: float(d))
    inf = float('inf')
    if dur < possible_durations[0]:
        return possible_durations[0] if direction != '<' else None
    elif dur > possible_durations[-1]:
        return possible_durations[-1] if direction != '>' else None
    if direction == '<':
        nearest = sorted(possdurs, key=lambda d: abs(dur - d) if d < dur else
            inf)[0]
        return nearest if nearest < inf else None
    elif direction == '>':
        nearest = sorted(possdurs, key=lambda d: abs(dur - d) if d > dur else
            inf)[0]
        return nearest if nearest < inf else None
    elif direction == '<>':
        nearest = sorted(possdurs, key=lambda d: abs(dur - d))[0]
        return nearest
    else:
        raise ValueError("direction should be one of '>', '<', or '<>'")
