def slice_adjust_margin(x, maximum, MARGIN):
    """Return a slice defined by x-MARGIN and x+MARGIN.
    If x is below 0, it uses 0 instead of x-MARGIN.
    If x is above maximum, it uses maximum instead of x-MARGIN.

    Examples:

        >>> slice_adjust_margin(20, 50, 30)
        slice(0, 50, None)
    """
    x = int(x)
    LOW = x - MARGIN if x-MARGIN >= 0 else 0
    HIGH = x + MARGIN if x+MARGIN < maximum else maximum
    return slice(LOW, HIGH)