def python_seq(start, stop, step, length):
    """
    Converts mathematica sequence tuple to python slice object.

    Based on David Mashburn's generic slice:
    https://gist.github.com/davidmashburn/9764309
    """
    if step == 0:
        return None
    if (start is not None and stop is not None and stop + 1 == start and 
        step > 0):
        return slice(0, 0, 1)
    if start == 0 or stop == 0:
        return None
    if start < 0:
        start += length
    else:
        start -= 1
    if stop is None:
        if step < 0:
            stop = 0
        else:
            stop = length - 1
    elif stop < 0:
        stop += length
    else:
        assert stop > 0
        stop -= 1
    if (not 0 <= start < length or not 0 <= stop < length or step > 0 and 
        start - stop > 1 or step < 0 and stop - start > 1):
        return None
    if step > 0:
        stop += 1
    else:
        stop -= 1
        if stop == -1:
            stop = None
        if start == 0:
            start = None
    return slice(start, stop, step)
