import numbers
from typing import Sequence
from typing import Type


def _parse_weight_decay_policy(policy):
    """Parse and verify weight decay policy"""
    if isinstance(policy, numbers.Number):
        policy = [policy]

    for p in policy[:-1]:
        assert (
            isinstance(p, Sequence) and len(p) == 2
        ), "weight decay policy should be a tuple of (value, pattern), get {p}"
        assert isinstance(p[0], numbers.Number), "value should be a number" f", get {p[0]}"
        assert isinstance(
            p[1], (str, Type, Sequence)
        ), "pattern should be string, type, or list of types, get {p[1]}"

    p = policy[-1]
    assert isinstance(
        p, numbers.Number
    ), f"weight decay should fallback into default value e.g. 1e-4, get {p}"

    return policy