def swap_segments(seq, pos1, pos2):
    """Return a new sequence with segments at position ``pos1`` and ``pos2`` swapped.

    ``pos1``, ``pos2`` are both of the form (start1, end1), (start2, end2).
    """
    (start1, end1), (start2, end2) = sorted([pos1, pos2])
    return (
        seq[:start1]
        + seq[start2:end2]
        + seq[end1:start2]
        + seq[start1:end1]
        + seq[end2:]
    )