<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def timedelta_to_str(value: datetime.timedelta, fmt: str = None) -> str: """Display the timedelta formatted according to the given string. You should use global setting ``TIMEDELTA_FORMAT`` to specify default format to this function there (like ``DATE_FORMAT`` for builtin ``date`` template filter). Default value for ``TIMEDELTA_FORMAT`` is ``'G:i'``. Format uses the same policy as Django ``date`` template filter or PHP ``date`` function with several differences. Available format strings: | Format character | Description | Example output | +==================+=============================+========================+ | ``a`` | Not implemented. | | | ``A`` | Not implemented. | | | ``b`` | Not implemented. | | | ``B`` | Not implemented. | | | ``c`` | Not implemented. | | | ``d`` | Total days, 2 digits with | ``'01'``, ``'41'`` | | | leading zeros. Do not | | | | combine with ``w`` format. | | | ``D`` | Not implemented. | | | ``f`` | Magic "full" format with | ``'2w 4d 1:28:07'`` | | | short labels. | | | ``F`` | Magic "full" format with | ``'2 weeks, 4 days, | | | normal labels. | 1:28:07'`` | | ``g`` | Day, not total, hours | ``'0'`` to ``'23'`` | | | without leading zeros. To | | | | use with ``d``, ``j``, or | | | | ``w``. | | | ``G`` | Total hours without | ``'1'``, ``'433'`` | | | leading zeros. Do not | | | | combine with ``g`` or | | | | ``h`` formats. | | | ``h`` | Day, not total, hours with | ``'00'`` to ``'23'`` | | | leading zeros. To use with | | | | ``d`` or ``w``. | | | ``H`` | Total hours with leading | ``'01', ``'433'`` | | | zeros. Do not combine with | | | | ``g`` or ``h`` formats. | | | ``i`` | Hour, not total, minutes, 2 | ``00`` to ``'59'`` | | | digits with leading zeros | | | | To use with ``g``, ``G``, | | | | ``h`` or ``H`` formats. | | | ``I`` | Total minutes, 2 digits or | ``'01'``, ``'433'`` | | | more with leading zeros. Do | | | | not combine with ``i`` | | | | format. | | | ``j`` | Total days, one or 2 digits | ``'1'``, ``'41'`` | | | without leading zeros. Do | | | | not combine with ``w`` | | | | format. | | | ``J`` | Not implemented. | | | ``l`` | Days long label. | ``'day'`` or | | | Pluralized and localized. | ``'days'`` | | ``L`` | Weeks long label. | ``'week'`` or | | | Pluralized and localized. | ``'weeks'`` | | ``m`` | Week days long label. | ``'day'`` or | | | Pluralized and localized. | ``'days'`` | | ``M`` | Not implemented. | | | ``n`` | Not implemented. | | | ``N`` | Not implemented. | | | ``O`` | Not implemented. | | | ``P`` | Not implemented. | | | ``r`` | Standart Python timedelta | ``'18 d 1:28:07'`` | | | representation with short | | | | labels. | | | ``R`` | Standart Python timedelta | ``'18 days, 1:28:07'`` | | | representation with normal | | | | labels. | | | ``s`` | Minute, not total, seconds, | ``'00'`` to ``'59'`` | | | 2 digits with leading | | | | zeros. To use with ``i`` or | | | | ``I``. | | | ``S`` | Total seconds. 2 digits or | ``'00'``, ``'433'`` | | | more with leading zeros. Do | | | | not combine with ``s`` | | | | format. | | | ``t`` | Not implemented. | | | ``T`` | Not implemented. | | | ``u`` | Second, not total, | ``0`` to ``999999`` | | | microseconds. | | | ``U`` | Not implemented. | | | ``w`` | Week, not total, days, one | ``0`` to ``6`` | | | digit without leading | | | | zeros. To use with ``W``. | | | ``W`` | Total weeks, one or more | ``'1'``, ``'41'`` | | | digits without leading | | | | zeros. | | | ``y`` | Not implemented. | | | ``Y`` | Not implemented. | | | ``z`` | Not implemented. | | | ``Z`` | Not implemented. | | For example, :: Couple words about magic "full" formats. These formats show weeks number with week label, days number with day label and seconds only if weeks number, days number or seconds greater that zero. For example, :: :param value: Timedelta instance to convert to string. :param fmt: Format to use for conversion. """

    # Only ``datetime.timedelta`` instances allowed for this function
    if not isinstance(value, datetime.timedelta):
        raise ValueError(
            'Value should be a "datetime.timedelta" instance. You use {0}.'
            .format(type(value)))

    # Generate total data
    days = value.days
    microseconds = value.microseconds
    seconds = timedelta_seconds(value)

    hours = seconds // 3600
    minutes = seconds // 60
    weeks = days // 7

    # Generate collapsed data
    day_hours = hours - days * 24
    hour_minutes = minutes - hours * 60
    minute_seconds = seconds - minutes * 60
    week_days = days - weeks * 7

    days_label = 'day' if days % 10 == 1 else 'days'
    short_days_label = 'd'
    short_week_days_label = 'd'
    short_weeks_label = 'w'
    week_days_label = 'day' if week_days % 10 == 1 else 'days'
    weeks_label = 'week' if weeks % 10 == 1 else 'weeks'

    # Collect data
    data = locals()

    fmt = fmt or TIMEDELTA_FORMAT
    processed = ''

    for part in fmt:
        if part in TIMEDELTA_FORMATS:
            is_full_part = part in ('f', 'F')
            is_repr_part = part in ('r', 'R')

            part = TIMEDELTA_FORMATS[part][0]

            if is_full_part or is_repr_part:
                if is_repr_part and not days:
                    part = part.replace('%(days)d', '')
                    part = part.replace('%(days_label)s,', '')
                    part = part.replace('%(short_days_label)s', '')

                if is_full_part and not minute_seconds:
                    part = part.replace(':%(minute_seconds)02d', '')

                if is_full_part and not weeks:
                    part = part.replace('%(weeks)d', '')
                    part = part.replace('%(short_weeks_label)s', '')
                    part = part.replace('%(weeks_label)s,', '')

                if is_full_part and not week_days:
                    part = part.replace('%(week_days)d', '')
                    part = part.replace('%(short_week_days_label)s', '')
                    part = part.replace('%(week_days_label)s,', '')

                part = part.strip()
                part = ' '.join(part.split())

        processed += part

    return processed % data