<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def granularity_to_time(s):
 """convert a named granularity into seconds. 300 """

    mfact = {
        'S': 1,
        'M': 60,
        'H': 3600,
        'D': 86400,
        'W': 604800,
    }
    try:
        f, n = re.match("(?P<f>[SMHDW])(?:(?P<n>\d+)|)", s).groups()
        n = n if n else 1
        return mfact[f] * int(n)

    except Exception as e:
        raise ValueError(e)