<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def str_to_timedelta(value: str, fmt: str = None) -> Optional[datetime.timedelta]: """ Convert string value to timedelta instance according to the given format. If format not set function tries to load timedelta using default ``TIMEDELTA_FORMAT`` and then both of magic "full" formats. You should also specify list of formats and function tries to convert to timedelta using each of formats in list. First matched format would return the converted timedelta instance. If user specified format, but function cannot convert string to new timedelta instance - ``ValueError`` would be raised. But if user did not specify the format, function would be fail silently and return ``None`` as result. :param value: String representation of timedelta. :param fmt: Format to use for conversion. """

    def timedelta_kwargs(data: DictStrInt) -> DictStrInt:
        """
        Convert day_hours, hour_minutes, minute_seconds, week_days and weeks to
        timedelta seconds.
        """
        seconds = data.get('seconds', 0)
        seconds += data.get('day_hours', 0) * 3600
        seconds += data.pop('hour_minutes', 0) * 60
        seconds += data.pop('minute_seconds', 0)
        seconds += data.pop('week_days', 0) * SECONDS_PER_DAY
        seconds += data.pop('weeks', 0) * SECONDS_PER_WEEK
        data.update({'seconds': seconds})
        return data

    if not isinstance(value, str):
        raise ValueError(
            'Value should be a "str" instance. You use {0}.'
            .format(type(value)))

    user_fmt = fmt

    if isinstance(fmt, (list, tuple)):
        formats = list(fmt)
    elif fmt is None:
        formats = [TIMEDELTA_FORMAT, 'F', 'f']
    else:
        formats = [fmt]

    locale_data = {
        'days_label': '({0}|{1})'.format('day', 'days'),
        'short_days_label': 'd',
        'short_week_days_label': 'd',
        'short_weeks_label': 'w',
        'week_days_label': '({0}|{1})'.format('day', 'days'),
        'weeks_label': '({0}|{1})'.format('week', 'weeks'),
    }
    regexps = []

    for item in formats:
        processed = r'^'

        for part in item:
            if part in TIMEDELTA_FORMATS:
                part = TIMEDELTA_FORMATS[part][1] % locale_data
            else:
                part = re.escape(part)
            processed += part

        processed += r'$'
        regexps.append(processed)

    for regexp in regexps:
        timedelta_re = re.compile(regexp)
        matched = timedelta_re.match(value)

        if matched:
            data = {
                key: to_int(value) or 0
                for key, value in matched.groupdict().items()}
            return datetime.timedelta(**timedelta_kwargs(data))

    if user_fmt:
        raise ValueError(
            'Cannot convert {0!r} to timedelta instance, using {1!r} format.'
            .format(value, user_fmt))

    return None