from pathlib import Path


def get_parent(p: Path, name: str, offset: int = 0) -> Path:
    """Get Path parent by name
    - protects for max/min of parents length

    Parameters
    ----------
    p : Path
    name : str
        parent name to find
    offset : int
        offset from found parent

    Returns
    -------
    Path
        parent of original Path
    """
    parents = p.parents
    m = {k.name.lower(): i for i, k in enumerate(parents)}
    i = min(max(m[name.lower()] - offset, 0), len(parents) - 1)
    return p.parents[i]