(s, strict=False)
Returns list of timedelta objects.
Returns list of timedelta objects.
Returns list of timedelta objects .
def stringToDurations(s, strict=False):
    """
    Returns list of timedelta objects.
    """
    def makeTimedelta(sign, week, day, hour, minute, sec):
        if sign == "-":
            sign = -1
        else:
            sign = 1
        week = int(week)
        day = int(day)
        hour = int(hour)
        minute = int(minute)
        sec = int(sec)
        return sign * datetime.timedelta(weeks=week, days=day, hours=hour,
                                         minutes=minute, seconds=sec)

    def error(msg):
        if strict:
            raise ParseError(msg)
        else:
            raise ParseError(msg)

    # vars which control state machine
    charIterator = enumerate(s)
    state = "start"

    durations = []
    current = ""
    sign = None
    week = 0
    day = 0
    hour = 0
    minute = 0
    sec = 0

    while True:
        try:
            charIndex, char = next(charIterator)
        except:
            char = "eof"

        if state == "start":
            if char == '+':
                state = "start"
                sign = char
            elif char == '-':
                state = "start"
                sign = char
            elif char.upper() == 'P':
                state = "read field"
            elif char == "eof":
                state = "error"
                error("got end-of-line while reading in duration: " + s)
            elif char in string.digits:
                state = "read field"
                current = current + char  # update this part when updating "read field"
            else:
                state = "error"
                error("got unexpected character {0} reading in duration: {1}"
                      .format(char, s))

        elif state == "read field":
            if (char in string.digits):
                state = "read field"
                current = current + char  # update part above when updating "read field"
            elif char.upper() == 'T':
                state = "read field"
            elif char.upper() == 'W':
                state = "read field"
                week = current
                current = ""
            elif char.upper() == 'D':
                state = "read field"
                day = current
                current = ""
            elif char.upper() == 'H':
                state = "read field"
                hour = current
                current = ""
            elif char.upper() == 'M':
                state = "read field"
                minute = current
                current = ""
            elif char.upper() == 'S':
                state = "read field"
                sec = current
                current = ""
            elif char == ",":
                state = "start"
                durations.append(makeTimedelta(sign, week, day, hour, minute,
                                               sec))
                current = ""
                sign = None
                week = None
                day = None
                hour = None
                minute = None
                sec = None
            elif char == "eof":
                state = "end"
            else:
                state = "error"
                error("got unexpected character reading in duration: " + s)

        elif state == "end":  # an end state
            if (sign or week or day or hour or minute or sec):
                durations.append(makeTimedelta(sign, week, day, hour, minute,
                                               sec))
            return durations

        elif state == "error":  # an end state
            error("in error state")
            return durations

        else:
            state = "error"
            error("unknown state: '{0!s}' reached in {1!s}".format(state, s))
def stringToDurations ( s , strict = False ) : def makeTimedelta ( sign , week , day , hour , minute , sec ) : if sign == "-" : sign = - 1 else : sign = 1 week = int ( week ) day = int ( day ) hour = int ( hour ) minute = int ( minute ) sec = int ( sec ) return sign * datetime . timedelta ( weeks = week , days = day , hours = hour , minutes = minute , seconds = sec ) def error ( msg ) : if strict : raise ParseError ( msg ) else : raise ParseError ( msg ) # vars which control state machine charIterator = enumerate ( s ) state = "start" durations = [ ] current = "" sign = None week = 0 day = 0 hour = 0 minute = 0 sec = 0 while True : try : charIndex , char = next ( charIterator ) except : char = "eof" if state == "start" : if char == '+' : state = "start" sign = char elif char == '-' : state = "start" sign = char elif char . upper ( ) == 'P' : state = "read field" elif char == "eof" : state = "error" error ( "got end-of-line while reading in duration: " + s ) elif char in string . digits : state = "read field" current = current + char # update this part when updating "read field" else : state = "error" error ( "got unexpected character {0} reading in duration: {1}" . format ( char , s ) ) elif state == "read field" : if ( char in string . digits ) : state = "read field" current = current + char # update part above when updating "read field" elif char . upper ( ) == 'T' : state = "read field" elif char . upper ( ) == 'W' : state = "read field" week = current current = "" elif char . upper ( ) == 'D' : state = "read field" day = current current = "" elif char . upper ( ) == 'H' : state = "read field" hour = current current = "" elif char . upper ( ) == 'M' : state = "read field" minute = current current = "" elif char . upper ( ) == 'S' : state = "read field" sec = current current = "" elif char == "," : state = "start" durations . append ( makeTimedelta ( sign , week , day , hour , minute , sec ) ) current = "" sign = None week = None day = None hour = None minute = None sec = None elif char == "eof" : state = "end" else : state = "error" error ( "got unexpected character reading in duration: " + s ) elif state == "end" : # an end state if ( sign or week or day or hour or minute or sec ) : durations . append ( makeTimedelta ( sign , week , day , hour , minute , sec ) ) return durations elif state == "error" : # an end state error ( "in error state" ) return durations else : state = "error" error ( "unknown state: '{0!s}' reached in {1!s}" . format ( state , s ) )