def overlap(a1, a2, b1, b2):
    """Test whether two value ranges overlap.
    
    This function is typically used with date values, but it also
    works with integers or other comparable values.  The following
    examples use integer values to be more readable.

    Unlike the test presented at
    <http://bytes.com/topic/python/answers/457949-determing-whether-two-ranges-overlap>,
    this works also with "open" ranges (the open end being indicated
    by a `None` value).
    
    Types of constellations::
    
      -   o---o  o---o
      -   o---o  o--->
      -   <---o  o---o
      -   <---o  o--->
                
      -   o------------->
                o---o
                
      -   o---o
            o---o
                
      -   o---o
            o--->
      -   <---------o
               o---o
    


    >>> overlap(1,2,3,4)
    False
    >>> overlap(3,4,1,2)
    False
    >>> overlap(1,3,2,4)
    True
    >>> overlap(2,4,1,3)
    True
    >>> overlap(1,4,2,3)
    True
    >>> overlap(2,3,1,4)
    True
    
    
    >>> overlap(1,None,3,4)
    True
    >>> overlap(3,4,1,None)
    True
    
    
    >>> overlap(1,2,3,None)
    False
    >>> overlap(3,None,1,2)
    False
    
    >>> overlap(None,2,3,None)
    False
    >>> overlap(3,None,None,2)
    False
    
    >>> overlap(1,3,2,None)
    True
    
    Ranges that "only touch" each other are not considered overlapping:
        
    >>> overlap(1,2,2,3)
    False
    >>> overlap(2,3,1,2)
    False

    """
    if a2:
        if b1:
            if b1 >= a2:
                return False
            elif b2 and a1:
                if a1 > a2:
                    raise ValueError('Range 1 ends before it started.')
                return b2 > a1
            else:
                return True
        elif b2 and a1:
            return b2 >= a1
        else:
            return True
    elif b2:
        if a1:
            return b2 > a1
        else:
            return True
    return True
