def select_by_year(year, D):
    """Select year from specification given in dictionary or list of ranges.

    Examples:
        >>> spec = {(..., 1990): 'foo',
        ...                1991: 'bar',
        ...        (1992, 2000): 'foobar',
        ...         (2001, ...): 'blah'}
        >>> select_by_year(1990, spec)
        'foo'
        >>> select_by_year(1991, spec)
        'bar'
        >>> select_by_year(1999, spec)
        'foobar'
        >>> select_by_year(2010, spec)
        'blah'
    """

    # Must have a list of specifications
    if hasattr(D, 'items'):
        D = list(D.items())

    # We want a sorted and normalized list
    spec = []
    for item, value in D:
        if isinstance(item, (tuple, list)):
            a, b = item
        else:
            a = b = item
        a = -float('inf') if a in [..., None] else a
        b = float('inf') if b in [..., None] else b
        spec.append((a, b, value))
    spec.sort()

    # Now let us test each interval
    for a, b, value in spec:
        if a <= year <= b:
            return value
    else:
        raise ValueError('no valid range for year %s: %s' % (year, spec))